-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.h
287 lines (243 loc) · 9.84 KB
/
engine.h
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
/*
* ENGINE.H: Tic-Tac-Toe AI MiniMax AI Core
* ---------
* Coded by Trinh D.D. Nguyen
* Last updates: May, 2024
*/
#ifndef _TICTACTOE_MINIMAX_ENGINE_H_
#define _TICTACTOE_MINIMAX_ENGINE_H_
#include <stdio.h>
#include "defs.h"
#define MIN_INF -1000
#define MAX_INF +1000
/* =============== PROTOTYPES ==================== */
bool is_playable(game_board g, int c, int r);
bool is_occupied(game_board g, int c, int r);
bool has_move(game_board g);
void init_board(game_board g);
void show_board(game_board g, bool final);
int evaluate(game_board g);
#ifdef _USE_ALPHA_BETA_PRUNE_
int minimax(game_board g, int depth, bool ismax, int alpha, int beta);
#else
int minimax(game_board g, int depth, bool ismax);
#endif
bool human_move(game_board g, int c, int r);
void computer_move(game_board g);
/* =============================================== */
/* check if a cell is empty */
bool is_playable(game_board g, int c, int r) {
return g[r][c] == CELL_E;
}
/* check if a cell is occupied */
bool is_occupied(game_board g, int c, int r) {
return g[r][c] != CELL_E;
}
/* check if the board is still playable */
bool has_move(game_board g) {
for (int r = 0; r < BOARD_SIZE; r++)
for (int c = 0; c < BOARD_SIZE; c++)
if (g[r][c] == CELL_E) return true;
return false;
}
/* initialize game board */
void init_board(game_board g) {
for (int r = 0; r < BOARD_SIZE; r++)
for (int c = 0; c < BOARD_SIZE; c++)
g[r][c] = CELL_E;
}
/* display game board */
void show_board(game_board g, bool final) {
#define HBAR for (int c = 0; c < BOARD_SIZE; c++) \
printf(C_DARK"+-----"C_RESET); \
printf(C_RESET"+\n");
HBAR;
for (int r = 0; r < BOARD_SIZE; r++) {
for (int c = 0; c < BOARD_SIZE; c++) {
if (is_occupied(g, c, r)) {
if (g[r][c] == CELL_X)
printf(C_DARK"| "C_X"%2c "C_DARK, g[r][c]);
else
printf(C_DARK"| "C_O"%2c "C_DARK, g[r][c]);
}
else {
if (!final)
printf(C_DARK"| "C_EMPTY"%2d "C_DARK, r * BOARD_SIZE + c);
else
printf(C_DARK"| ");
}
}
printf(C_DARK"|\n");
HBAR;
}
}
/* ---------------------- */
/* for internal use only */
bool match_row(game_board g, int r, char sym) {
bool match = true;
for (int c = 0; c < BOARD_SIZE && match; c++)
match = match && g[r][c] == sym;
return match;
}
bool match_column(game_board g, int c, char sym) {
bool match = true;
for (int r = 0; r < BOARD_SIZE && match; r++)
match = match && (g[r][c] == sym);
return match;
}
bool match_primary_diagonal(game_board g, char sym) {
bool match = true;
for (int i = 0; i < BOARD_SIZE && match; i++)
match = match && (g[i][i] == sym);
return match;
}
bool match_secondary_diagonal(game_board g, char sym) {
bool match = true;
for (int i = 0; i < BOARD_SIZE && match; i++)
match = match && (g[i][BOARD_SIZE-1-i] == sym);
return match;
}
/* ---------------------- */
/* board evaluate function: X wins = +1, O wins = -1, tie = 0 */
int evaluate(game_board g) {
/* row checks */
for (int r = 0; r < BOARD_SIZE; r++) {
if (match_row(g, r, CELL_X)) return SCORE_X;
if (match_row(g, r, CELL_O)) return SCORE_O;
}
/* column checks */
for (int c = 0; c < BOARD_SIZE; c++) {
if (match_column(g, c, CELL_X)) return SCORE_X;
if (match_column(g, c, CELL_O)) return SCORE_O;
}
/* primary diagonal check */
if (match_primary_diagonal(g, CELL_X)) return SCORE_X;
if (match_primary_diagonal(g, CELL_O)) return SCORE_O;
/* secondary diagonal check */
if (match_secondary_diagonal(g, CELL_X)) return SCORE_X;
if (match_secondary_diagonal(g, CELL_O)) return SCORE_O;
return SCORE_TIE;
}
int mini(int a, int b) {
return a < b ? a : b;
}
int maxi(int a, int b) {
return a > b ? a : b;
}
#ifdef _USE_ALPHA_BETA_PRUNE_
/* the minimax algorithm: assuming player is on the minimizer side */
int minimax(game_board g, int depth, bool ismax, int alpha, int beta) {
int r, c, best;
int score = evaluate(g); /* evaluating the board */
if (score != SCORE_TIE) return score; /* return score if a player won */
if (!has_move(g)) return SCORE_TIE; /* no more move? it is a tie */
if (depth >= game_depth) return score;
progress_show(); /* show progress bar */
states++; /* explored a search state */
if (ismax) { /* evaluating the maximizer player */
int best = MIN_INF; /* for finding max */
for (r = 0; r < BOARD_SIZE; r++) /* scan the game board */
for (c = 0; c < BOARD_SIZE; c++)
if (g[r][c] == CELL_E) { /* found an empty cell */
g[r][c] = computer; /* assuming computer move on that cell */
/* recursively explore down the state space */
score = minimax(g, depth+1, false, alpha, beta);
g[r][c] = CELL_E; /* undo that move */
best = maxi(score, best); /* obtain the maximum score */
/* alpha-beta pruning */
alpha = maxi(alpha, best);
if (beta <= alpha) break;
}
return best; /* and return it */
}
else { /* the minimizer's turn */
int best = MAX_INF; /* for finding min */
for (r = 0; r < BOARD_SIZE; r++) /* scan the game board */
for (c = 0; c < BOARD_SIZE; c++)
if (g[r][c] == CELL_E) { /* found an empty cell */
g[r][c] = human; /* assuming human move on that cell */
/* recursively explore down the state space */
score = minimax(g, depth+1, true, alpha, beta);
g[r][c] = CELL_E; /* undo that move */
best = mini(score, best); /* obtain the minimum score */
/* alpha-beta pruning */
beta = mini(beta, best);
if (beta <= alpha) break;
}
return best; /* also return it */
}
}
#else
/* the minimax algorithm: assuming player is on the minimizer side */
int minimax(game_board g, int depth, bool ismax) {
int r, c, best;
int score = evaluate(g); /* evaluating the board */
if (score != SCORE_TIE) return score; /* return score if a player won */
if (!has_move(g)) return SCORE_TIE; /* no more move? it is a tie */
if (depth >= game_depth) return score;
progress_show(); /* show progress bar */
states++; /* explored a search state */
if (ismax) { /* evaluating the maximizer player */
int best = -1000; /* for finding max */
for (r = 0; r < BOARD_SIZE; r++) /* scan the game board */
for (c = 0; c < BOARD_SIZE; c++)
if (g[r][c] == CELL_E) { /* found an empty cell */
g[r][c] = computer; /* assuming computer move on that cell */
/* recursively explore down the state space */
score = minimax(g, depth+1, false);
g[r][c] = CELL_E; /* undo that move */
best = maxi(score, best); /* obtain the maximum score */
}
return best; /* and return it */
}
else { /* the minimizer's turn */
int best = 1000; /* for finding min */
for (r = 0; r < BOARD_SIZE; r++) /* scan the game board */
for (c = 0; c < BOARD_SIZE; c++)
if (g[r][c] == CELL_E) { /* found an empty cell */
g[r][c] = human; /* assuming human move on that cell */
/* recursively explore down the state space */
score = minimax(g, depth+1, true);
g[r][c] = CELL_E; /* undo that move */
best = mini(score, best); /* obtain the minimum score */
}
return best; /* also return it */
}
}
#endif
/* human make his move */
bool human_move(game_board g, int c, int r) {
if (is_playable(g, c, r)) { /* check if the cell is empty */
g[r][c] = human; /* set the piece */
current = computer; /* and switch turn to computer */
return true; /* human made a move */
}
return false; /* human could not make a move */
}
/* AI select its best move */
void computer_move(game_board g) {
int best = -1000; /* for finding the best move */
int score, r, c;
move mv = {-1, -1};
states = 0; /* reset state counter */
for (r = 0; r < BOARD_SIZE; r++) /* board scan */
for (c = 0; c < BOARD_SIZE; c++)
if (g[r][c] == CELL_E) { /* found an empty cell */
g[r][c] = computer; /* assuming the move */
/* search the search space */
#ifdef _USE_ALPHA_BETA_PRUNE_
score = minimax(g, 0, false, MIN_INF, MAX_INF);
#else
score = minimax(g, 0, false);
#endif
g[r][c] = CELL_E; /* and undo it */
if (score > best) { /* find the minimum score */
best = score; /* and save it */
mv.r = r; /* also the coordinates */
mv.c = c; /* of that move */
}
}
g[mv.r][mv.c] = computer; /* computer make a move */
current = human; /* turn is now back to human */
}
#endif