-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtic_tac_toe.cpp
239 lines (211 loc) · 7.22 KB
/
tic_tac_toe.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "../gtsa.hpp"
const int SIDE = 3;
const char PLAYER_1 = 'X';
const char PLAYER_2 = 'O';
const char EMPTY = '_';
struct TicTacToeMove : public Move<TicTacToeMove> {
unsigned x;
unsigned y;
TicTacToeMove() { }
TicTacToeMove(unsigned x, unsigned y) : x(x), y(y) { }
void read(istream &stream = cin) override {
if (&stream == &cin) {
cout << "Enter space separated X and Y of your move: ";
}
stream >> x >> y;
}
ostream &to_stream(ostream &os) const override {
return os << x << " " << y;
}
bool operator==(const TicTacToeMove &rhs) const override {
return x == rhs.x && y == rhs.y;
}
size_t hash() const override {
using boost::hash_value;
using boost::hash_combine;
size_t seed = 0;
hash_combine(seed, hash_value(x));
hash_combine(seed, hash_value(y));
return seed;
}
};
const vector<vector<TicTacToeMove>> LINES = [] {
vector<vector<TicTacToeMove>> lines;
for (int y = 0; y < SIDE; ++y) {
lines.emplace_back(); // add a new line to back()
for (int x = 0; x < SIDE; ++x)
lines.back().emplace_back(x, y); // add a new coord to that line
}
for (int x = 0; x < SIDE; ++x) {
lines.emplace_back();
for (int y = 0; y < SIDE; ++y)
lines.back().emplace_back(x, y);
}
lines.emplace_back();
for (int i = 0; i < SIDE; ++i) {
lines.back().emplace_back(i, i);
}
lines.emplace_back();
for (int i = 0; i < SIDE; ++i) {
lines.back().emplace_back(SIDE - i - 1, i);
}
return lines;
}();
const unsigned long LINES_SIZE = 2 * SIDE + 2;
struct TicTacToeState : public State<TicTacToeState, TicTacToeMove> {
vector<char> board;
TicTacToeState() : State({0, 1}) { }
TicTacToeState(const string &init_string) : State({0, 1}) {
const unsigned long length = init_string.length();
const unsigned long correct_length = SIDE * SIDE;
if (length != correct_length) {
throw invalid_argument("Initialization string length must be " + to_string(correct_length));
}
for (int i = 0; i < length; i++) {
const char c = init_string[i];
if (c != PLAYER_1 && c != PLAYER_2 && c != EMPTY) {
throw invalid_argument(string("Undefined symbol used: '") + c + "'");
}
}
board = vector<char>(init_string.begin(), init_string.end());
}
TicTacToeState clone() const override {
TicTacToeState clone = TicTacToeState();
clone.board = board;
clone.player_to_move = player_to_move;
return clone;
}
int get_goodness() const override {
int goodness = 0;
const auto &counts = count_players_on_lines(player_to_move);
for (int i = 0; i < LINES_SIZE; ++i) {
const int player_places = counts[2 * i];
const int enemy_places = counts[2 * i + 1];
if (player_places == SIDE) {
goodness += SIDE * SIDE;
}
else if (enemy_places == SIDE) {
goodness -= SIDE * SIDE;
}
else if (player_places == SIDE - 1 and enemy_places == 0) {
goodness += SIDE;
}
else if (enemy_places == SIDE - 1 and player_places == 0) {
goodness -= SIDE;
}
else if (player_places == SIDE - 2 and enemy_places == 0) {
++goodness;
}
else if (enemy_places == SIDE - 2 and player_places == 0) {
--goodness;
}
}
return goodness;
}
vector<TicTacToeMove> get_legal_moves(int max_moves = INF) const override {
int available_moves = SIDE * SIDE;
if (max_moves > available_moves) {
max_moves = available_moves;
}
vector<TicTacToeMove> moves(max_moves);
int i = 0;
for (int y = 0; y < SIDE; ++y) {
for (int x = 0; x < SIDE; ++x) {
if (board[y * SIDE + x] == EMPTY) {
moves[i++] = TicTacToeMove(x, y);
if (i >= max_moves) {
return moves;
}
}
}
}
moves.resize(i);
return moves;
}
bool is_terminal() const override {
if (!has_empty_space()) {
return true;
}
const auto &counts = count_players_on_lines(player_to_move);
for (int i = 0; i < LINES_SIZE; ++i) {
if (counts[2 * i] == SIDE || counts[2 * i + 1] == SIDE) {
return true;
}
}
return false;
}
bool is_winner(int player) const override {
const auto &counts = count_players_on_lines(player);
for (int i = 0; i < LINES_SIZE; ++i) {
if (counts[2 * i] == SIDE) {
return true;
}
}
return false;
}
int player_char_to_index(char player) const override {
return (player == PLAYER_1) ? 0 : 1;
}
char player_index_to_char(int index) const override {
return (index == 0) ? PLAYER_1 : PLAYER_2;
}
void make_move(const TicTacToeMove &move) override {
board[move.y * SIDE + move.x] = player_index_to_char(player_to_move);
player_to_move = get_next_player(player_to_move);
}
void undo_move(const TicTacToeMove &move) override {
board[move.y * SIDE + move.x] = EMPTY;
player_to_move = get_next_player(player_to_move);
}
bool has_empty_space() const {
for (unsigned y = 0; y < SIDE; ++y) {
for (unsigned x = 0; x < SIDE; ++x) {
if (board[y * SIDE + x] == EMPTY) {
return true;
}
}
}
return false;
}
vector<int> count_players_on_lines(int player) const {
vector<int> counts(2 * LINES_SIZE);
const auto enemy = player_index_to_char(get_next_player(player));
for (int i = 0; i < LINES_SIZE; ++i) {
int player_places = 0;
int enemy_places = 0;
for (int j = 0; j < SIDE; ++j) {
const TicTacToeMove &coord = LINES[i][j];
const int board_index = coord.y * SIDE + coord.x;
if (board[board_index] == player_index_to_char(player)) {
++player_places;
}
else if (board[board_index] == enemy) {
++enemy_places;
}
}
counts[2 * i] = player_places;
counts[2 * i + 1] = enemy_places;
}
return counts;
}
ostream &to_stream(ostream &os) const override {
for (int y = 0; y < SIDE; ++y) {
for (int x = 0; x < SIDE; ++x) {
os << board[y * SIDE + x];
}
os << "\n";
}
os << player_index_to_char(player_to_move) << endl;
return os;
}
bool operator==(const TicTacToeState &other) const override {
return board == other.board;
}
size_t hash() const override {
using boost::hash_value;
using boost::hash_combine;
size_t seed = 0;
hash_combine(seed, hash_value(board));
return seed;
}
};