-
Notifications
You must be signed in to change notification settings - Fork 4
/
ttt.cpp
345 lines (269 loc) · 7.8 KB
/
ttt.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//hotshott_22
//SillyGirl_1998
//CONTROL 2.32.0
//01-06-2020 ; 2220HR
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>
using std::cout;
using std::cin;
using std::endl;
#define WIN 1000
#define DRAW 0
#define LOSS -1000
#define AI_MARKER 'O'
#define PLAYER_MARKER 'X'
#define EMPTY_SPACE '-'
#define START_DEPTH 0
// Print game state
void print_game_state(int state)
{
if (WIN == state) { cout << " WIN :) " << endl; }
else if (DRAW == state) { cout << " DRAW - LETS PLAY ONCE MORE :0 " << endl; }
else if (LOSS == state) { cout << " CAN'T BEAT A SILLY AI -_- " << endl; }
}
// All possible winning states
std::vector<std::vector<std::pair<int, int>>> winning_states
{
// Every row
{ std::make_pair(0, 0), std::make_pair(0, 1), std::make_pair(0, 2) },
{ std::make_pair(1, 0), std::make_pair(1, 1), std::make_pair(1, 2) },
{ std::make_pair(2, 0), std::make_pair(2, 1), std::make_pair(2, 2) },
// Every column
{ std::make_pair(0, 0), std::make_pair(1, 0), std::make_pair(2, 0) },
{ std::make_pair(0, 1), std::make_pair(1, 1), std::make_pair(2, 1) },
{ std::make_pair(0, 2), std::make_pair(1, 2), std::make_pair(2, 2) },
// Every diagonal
{ std::make_pair(0, 0), std::make_pair(1, 1), std::make_pair(2, 2) },
{ std::make_pair(2, 0), std::make_pair(1, 1), std::make_pair(0, 2) }
};
// Print the current board state
void print_board(char board[3][3])
{
cout << endl;
cout << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << endl;
cout << "----------" << endl;
cout << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << endl;
cout << "----------" << endl;
cout << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << endl << endl;
}
// Get all available legal moves (spaces that are not occupied)
std::vector<std::pair<int, int>> get_legal_moves(char board[3][3])
{
std::vector<std::pair<int, int>> legal_moves;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (board[i][j] != AI_MARKER && board[i][j] != PLAYER_MARKER)
{
legal_moves.push_back(std::make_pair(i, j));
}
}
}
return legal_moves;
}
// Check if a position is occupied
bool position_occupied(char board[3][3], std::pair<int, int> pos)
{
std::vector<std::pair<int, int>> legal_moves = get_legal_moves(board);
for (int i = 0; i < legal_moves.size(); i++)
{
if (pos.first == legal_moves[i].first && pos.second == legal_moves[i].second)
{
return false;
}
}
return true;
}
// Get all board positions occupied by the given marker
std::vector<std::pair<int, int>> get_occupied_positions(char board[3][3], char marker)
{
std::vector<std::pair<int, int>> occupied_positions;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (marker == board[i][j])
{
occupied_positions.push_back(std::make_pair(i, j));
}
}
}
return occupied_positions;
}
// Check if the board is full
bool board_is_full(char board[3][3])
{
std::vector<std::pair<int, int>> legal_moves = get_legal_moves(board);
if (0 == legal_moves.size())
{
return true;
}
else
{
return false;
}
}
// Check if the game has been won
bool game_is_won(std::vector<std::pair<int, int>> occupied_positions)
{
bool game_won;
for (int i = 0; i < winning_states.size(); i++)
{
game_won = true;
std::vector<std::pair<int, int>> curr_win_state = winning_states[i];
for (int j = 0; j < 3; j++)
{
if (!(std::find(std::begin(occupied_positions), std::end(occupied_positions), curr_win_state[j]) != std::end(occupied_positions)))
{
game_won = false;
break;
}
}
if (game_won)
{
break;
}
}
return game_won;
}
char get_opponent_marker(char marker)
{
char opponent_marker;
if (marker == PLAYER_MARKER)
{
opponent_marker = AI_MARKER;
}
else
{
opponent_marker = PLAYER_MARKER;
}
return opponent_marker;
}
// Check if someone has won or lost
int get_board_state(char board[3][3], char marker)
{
char opponent_marker = get_opponent_marker(marker);
std::vector<std::pair<int, int>> occupied_positions = get_occupied_positions(board, marker);
bool is_won = game_is_won(occupied_positions);
if (is_won)
{
return WIN;
}
occupied_positions = get_occupied_positions(board, opponent_marker);
bool is_lost = game_is_won(occupied_positions);
if (is_lost)
{
return LOSS;
}
bool is_full = board_is_full(board);
if (is_full)
{
return DRAW;
}
return DRAW;
}
// Apply the minimax game optimization algorithm
std::pair<int, std::pair<int, int>> minimax_optimization(char board[3][3], char marker, int depth, int alpha, int beta)
{
// Initialize best move
std::pair<int, int> best_move = std::make_pair(-1, -1);
int best_score = (marker == AI_MARKER) ? LOSS : WIN;
// If we hit a terminal state (leaf node), return the best score and move
if (board_is_full(board) || DRAW != get_board_state(board, AI_MARKER))
{
best_score = get_board_state(board, AI_MARKER);
return std::make_pair(best_score, best_move);
}
std::vector<std::pair<int, int>> legal_moves = get_legal_moves(board);
for (int i = 0; i < legal_moves.size(); i++)
{
std::pair<int, int> curr_move = legal_moves[i];
board[curr_move.first][curr_move.second] = marker;
// Maximizing player's turn
if (marker == AI_MARKER)
{
int score = minimax_optimization(board, PLAYER_MARKER, depth + 1, alpha, beta).first;
// Get the best scoring move
if (best_score < score)
{
best_score = score - depth * 10;
best_move = curr_move;
// Check if this branch's best move is worse than the best
// option of a previously search branch. If it is, skip it
alpha = std::max(alpha, best_score);
board[curr_move.first][curr_move.second] = EMPTY_SPACE;
if (beta <= alpha)
{
break;
}
}
} // Minimizing opponent's turn
else
{
int score = minimax_optimization(board, AI_MARKER, depth + 1, alpha, beta).first;
if (best_score > score)
{
best_score = score + depth * 10;
best_move = curr_move;
// Check if this branch's best move is worse than the best
// option of a previously search branch. If it is, skip it
beta = std::min(beta, best_score);
board[curr_move.first][curr_move.second] = EMPTY_SPACE;
if (beta <= alpha)
{
break;
}
}
}
board[curr_move.first][curr_move.second] = EMPTY_SPACE; // Undo move
}
return std::make_pair(best_score, best_move);
}
// Check if the game is finished
bool game_is_done(char board[3][3])
{
if (board_is_full(board))
{
return true;
}
if (DRAW != get_board_state(board, AI_MARKER))
{
return true;
}
return false;
}
int main()
{
char board[3][3] = { EMPTY_SPACE };
cout << "********************************\n\n\tTic Tac Toe AI\n\n********************************" << endl << endl;
cout << "Player = X\t AI Computer = O" << endl << endl;
print_board(board);
while (!game_is_done(board))
{
int row, col;
cout << "Row play: ";
cin >> row;
cout << "Col play: ";
cin >> col;
cout << endl << endl;
if (position_occupied(board, std::make_pair(row, col)))
{
cout << "The position (" << row << ", " << col << ") is occupied. Try another one..." << endl;
continue;
}
else
{
board[row][col] = PLAYER_MARKER;
}
std::pair<int, std::pair<int, int>> ai_move = minimax_optimization(board, AI_MARKER, START_DEPTH, LOSS, WIN);
board[ai_move.second.first][ai_move.second.second] = AI_MARKER;
print_board(board);
}
cout << "********** GAME OVER **********" << endl << endl;
int player_state = get_board_state(board, PLAYER_MARKER);
cout << "PLAYER "; print_game_state(player_state);
return 0;
}