12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- //
- // Created by nositelshtanov on 11/11/2023.
- //
- #include "ttt_meta_session.h"
- void ttt_meta_session::play() {
- draw_map();
- while (!game.is_game_end()) {
- size_t x, y;
- auto whose_step = game.get_whose_step();
- auto map_number = game.get_next_map_number();
- try {
- if (map_number == ttt_meta::any_map) {
- std::cout << (whose_step == mark::zero ? META_O_INVITATION_TWO_PARAMS : META_X_INVITATION_TWO_PARAMS);
- std::cin >> x >> y;
- game.step(x, y);
- }
- else {
- std::cout << (whose_step == mark::zero ? META_O_INVITATION_ONE_PARAM : META_X_INVITATION_ONE_PARAM);
- std::cin >> x;
- game.step(game.get_next_map_number(), x);
- }
- } catch (const invalid_coords_error&) {
- std::cout << META_INVALID_COORS_HNDL << std::endl;
- continue;
- } catch (const step_error& err) {
- if (err.get_reason() == step_error::Reason::the_field_is_occupied) { std::cout << META_FIELD_IS_OCCUPIED_HNDL << std::endl; }
- else { throw; }
- }
- draw_map();
- }
- auto winner = game.get_winner();
- if (winner == mark::nothing) { std::cout << META_DRAW << std::endl; }
- else if (winner == mark::cross) { std::cout << META_CROSS_WIN << std::endl; }
- else { std::cout << META_ZERO_WIN << std::endl; }
- }
- void ttt_meta_session::draw_map() {
- std::size_t i = 0;
- print_devider();
- while (i < ttt_meta::map_size) {
- print_ln(i);
- if ((i + 1) % 3 == 0) { print_devider(); }
- ++i;
- }
- }
- void ttt_meta_session::print_ln(std::size_t fields_row) {
- const auto mapIdxStart = 3 * (fields_row / 3);
- const auto mapIdxEnd = mapIdxStart + 3;
- const auto fieldIdxStart = (3 * fields_row) % 9;
- const auto fieldIdxEnd = fieldIdxStart + 3;
- std::cout << META_MAP_BORDER;
- // no reason for avoid a nested loop
- for (std::size_t i{ mapIdxStart }; i < mapIdxEnd; ++i) {
- for (std::size_t j{ fieldIdxStart }; j < fieldIdxEnd; ++j) {
- auto cur_mark = game.get_mark(i, j);
- auto winner = game.get_mark(i).get_winner();
- if (winner == mark::nothing) {
- std::cout << (
- cur_mark == mark::cross ?
- META_MAP_CROSS :
- cur_mark == mark::zero ?
- META_MAP_ZERO :
- i == game.get_next_map_number() || game.get_next_map_number() == ttt_meta::any_map ?
- META_MAP_MAY_SELECT:
- META_MAP_NOTHING
- );
- } else if (winner == mark::cross) {
- std::cout << META_CROSS_FILL;
- } else {
- std::cout << META_ZERO_FILL;
- }
- }
- std::cout << META_MAP_BORDER;
- }
- std::cout << std::endl;
- }
|