-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.hpp
117 lines (91 loc) · 2.27 KB
/
tictactoe.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef TICTACTOE_HPP
#define TICTACTOE_HPP
#include <vector>
#include "board.hpp"
#include "game.hpp"
using namespace std;
struct Move {
int tileNo;
Move(int t = -1)
: tileNo(t)
{}
};
struct TicTacToe {
using move_t = Move;
Board b;
int turnCount;
TicTacToe(int width)
: b(width),
turnCount(0)
{}
bool isTerminal() const {
return b.isWinner() || b.isFull();
}
int getTurnCount() const { return turnCount; }
int getTurnParity() const { return turnCount % 2; }
bool isWinner() const {
return b.isWinner();
}
void print() {
b.print(false);
}
void makeMove(const struct Move& mv) {
int tileNo = mv.tileNo;
b.makeMove(tileNo, turnCount % 2);
if (!b.isWinner())
turnCount++;
}
bool isValid(const struct Move& mv) {
int tileNo = mv.tileNo;
return b.isValid(tileNo);
}
void setup() {
cout << "Refer to moves using the following chart: " << endl;
b.print(true);
}
vector<move_t> getAvailableMoves() const {
vector<move_t> tiles;
for (const auto& t : b.getAvailableMoves()) {
tiles.push_back(Move{t});
}
return tiles;
}
bool operator==(const TicTacToe& other_ttt) const
{
return b.board == other_ttt.b.board;
}
friend ostream& operator<<(ostream& os, const TicTacToe& t);
friend ostream& operator<<(ostream& os, const move_t& mv);
friend istream& operator>> (istream&in, move_t& mv);
};
template<> // Explicit specialization of std::hash<T>
struct std::hash<TicTacToe> {
size_t operator() (const TicTacToe& ttt) const
{
string repr;
for (auto& row : ttt.b.board) {
for (auto& c : row) {
repr += c;
}
}
return hash<string>()(repr);
}
};
ostream& operator<<(ostream& os, const TicTacToe& t) {
os << t.b;
return os;
}
ostream& operator<<(ostream& os, const TicTacToe::move_t& mv) {
cout << "Tile #: ";
os << mv.tileNo;
return os;
}
istream& operator>> (istream&in, TicTacToe::move_t& mv) {
int m;
if (in >> m)
{
mv.tileNo = m;
}
return in;
}
#endif