123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- //
- // Created by nositelshtanov on 11/11/2023.
- //
- #ifndef TTT_TTT_ENGINE_H
- #define TTT_TTT_ENGINE_H
- #include <array>
- #include <stdexcept>
- enum class mark {
- nothing,
- zero,
- cross
- };
- class invalid_coords_error: public std::runtime_error {
- public:
- invalid_coords_error(): std::runtime_error("") {}
- explicit invalid_coords_error(const char* msg): std::runtime_error(msg) {}
- };
- class step_error: public std::runtime_error {
- public:
- enum Reason {
- game_already_end,
- the_field_is_occupied
- };
- explicit step_error(Reason reason): std::runtime_error(""), reason(reason) {}
- step_error(const char* msg, Reason reason): std::runtime_error(msg), reason(reason) {}
- Reason get_reason() const { return reason; }
- private:
- Reason reason;
- };
- class ttt_engine {
- public:
- using combination = std::array<std::size_t, 3>;
- using ttt_map = std::array<mark, 9>;
- const std::array<combination, 8> win_combinations;
- ttt_engine();
- mark get_mark(std::size_t field) const { return map[field]; }
- mark get_winner() const { return winner; }
- void step(mark whose_step, std::size_t field_num);
- bool is_game_end() const { return step_count >= 9 || winner != mark::nothing; }
- protected:
- static bool is_same_marks(mark m1, mark m2, mark m3) { return m1 == m2 && m1 == m3 && m2 == m3; }
- mark who_is_winner() const;
- private:
- ttt_map map;
- mark winner;
- int step_count;
- };
- #endif //TTT_TTT_ENGINE_H
|