ttt_simple_session.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // Created by nositelshtanov on 11/11/2023.
  3. //
  4. #include <iostream>
  5. #include "ttt_simple_session.h"
  6. void ttt_simple_session::play() {
  7. mark winner;
  8. while ((winner = game.get_winner()) == mark::nothing && !game.is_game_end()) {
  9. size_t field_num;
  10. auto whose_step = game.get_whose_step();
  11. draw_map();
  12. if (whose_step == mark::cross) { std::cout << SMPL_INVITATION_X; }
  13. else { std::cout << SMPL_INVITATION_O; }
  14. std::cin >> field_num;
  15. try {
  16. game.step(field_num);
  17. } catch (const invalid_coords_error& err) {
  18. std::cout << SMPL_INVALID_COORDS_HNDL << std::endl;
  19. } catch (const step_error& err) {
  20. if (err.get_reason() == step_error::Reason::the_field_is_occupied) { std::cout << SMPL_FIELD_IS_OCCUPIED_HNDL << std::endl; }
  21. else { throw; }
  22. }
  23. }
  24. draw_map();
  25. if (winner == mark::cross) { std::cout << SMPL_WIN_PHRASE_X << std::endl; }
  26. else if (winner == mark::zero) { std::cout << SMPL_WIN_PHRASE_O << std::endl; }
  27. else { std::cout << SMPL_DRAW_PHRASE << std::endl; }
  28. }
  29. void ttt_simple_session::draw_map() {
  30. for (std::size_t i{0}; i < 9; ++i) {
  31. auto cur = game.get_mark(i);
  32. std::cout << (cur == mark::cross ? SMPL_CROSS_CELL : cur == mark::zero ? SMPL_ZERO_CELL : SMPL_NOTHING_CELL );
  33. if ((i+1) % 3 == 0) { std::cout << std::endl; }
  34. }
  35. }