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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
typedef struct
{
uint16_t bm;
char icon;
} Player;
typedef struct
{
Player max_player;
Player min_player;
} Board;
const uint16_t wincombs[8] = {
0b111000000, 0b000111000, 0b000000111, 0b100100100,
0b010010010, 0b001001001, 0b100010001, 0b001010100};
void render(Board *board);
uint8_t askmove(Board *board, char *player);
uint8_t evaluate(Board *board);
void checkposition(Board *board);
uint8_t minimax_alpha_beta(Board *board, bool ismax, uint8_t alpha, uint8_t beta);
uint8_t best_move(Board *board, bool ismax);
unsigned int random_number(unsigned int limit);
int main(void)
{
Board activeBoard = {
.max_player = {.bm = 0b000000000, .icon = 'x'},
.min_player = {.bm = 0b000000000, .icon = 'o'}
};
uint8_t user_move;
uint8_t computer_move;
time_t time = clock();
computer_move = best_move(&activeBoard, false);
time = clock() - time;
double exec_time = ((double) time) / CLOCKS_PER_SEC;
printf("bestmove function took %f seconds\n", exec_time);
int scanvar;
scanf("%d", &scanvar);
while (true) {
checkposition(&activeBoard);
render(&activeBoard);
user_move = askmove(&activeBoard, &activeBoard.max_player.icon);
activeBoard.max_player.bm ^= 0b100000000 >> user_move;
checkposition(&activeBoard);
computer_move = best_move(&activeBoard, false);
activeBoard.min_player.bm ^= 0b100000000 >> computer_move;
}
return 0;
}
void render(Board *board)
{
uint8_t i;
uint16_t wincomb;
uint16_t color_bm = 0b000000000;
for (i = 0; i < 8; i++) {
wincomb = wincombs[i];
if ((wincomb & board->max_player.bm) == wincomb || (wincomb & board->min_player.bm) == wincomb) {
color_bm |= wincomb;
}
}
printf(
"\n %s%c\x1b[0m │ %s%c\x1b[0m │ %s%c\x1b[0m \n───┼───┼───\n %s%c\x1b[0m │ %s%c\x1b[0m │ %s%c\x1b[0m \n───┼───┼───\n %s%c\x1b[0m │ %s%c\x1b[0m │ %s%c\x1b[0m \n",
(0b100000000 & color_bm) ? "\x1b[7m" : "",
(0b100000000 & board->max_player.bm) ? board->max_player.icon : (0b100000000 & board->min_player.bm) ? board->min_player.icon : '1',
(0b010000000 & color_bm) ? "\x1b[7m" : "",
(0b010000000 & board->max_player.bm) ? board->max_player.icon : (0b010000000 & board->min_player.bm) ? board->min_player.icon : '2',
(0b001000000 & color_bm) ? "\x1b[7m" : "",
(0b001000000 & board->max_player.bm) ? board->max_player.icon : (0b001000000 & board->min_player.bm) ? board->min_player.icon : '3',
(0b000100000 & color_bm) ? "\x1b[7m" : "",
(0b000100000 & board->max_player.bm) ? board->max_player.icon : (0b000100000 & board->min_player.bm) ? board->min_player.icon : '4',
(0b000010000 & color_bm) ? "\x1b[7m" : "",
(0b000010000 & board->max_player.bm) ? board->max_player.icon : (0b000010000 & board->min_player.bm) ? board->min_player.icon : '5',
(0b000001000 & color_bm) ? "\x1b[7m" : "",
(0b000001000 & board->max_player.bm) ? board->max_player.icon : (0b000001000 & board->min_player.bm) ? board->min_player.icon : '6',
(0b000000100 & color_bm) ? "\x1b[7m" : "",
(0b000000100 & board->max_player.bm) ? board->max_player.icon : (0b000000100 & board->min_player.bm) ? board->min_player.icon : '7',
(0b000000010 & color_bm) ? "\x1b[7m" : "",
(0b000000010 & board->max_player.bm) ? board->max_player.icon : (0b000000010 & board->min_player.bm) ? board->min_player.icon : '8',
(0b000000001 & color_bm) ? "\x1b[7m" : "",
(0b000000001 & board->max_player.bm) ? board->max_player.icon : (0b000000001 & board->min_player.bm) ? board->min_player.icon : '9'
);
}
uint8_t askmove(Board *board, char *player)
{
static struct termios term_settings;
char c;
uint16_t empty_cells = ~(board->max_player.bm | board->min_player.bm) & 0b111111111;
uint8_t i;
uint16_t cell_bm;
/* changing term settings for special input */
tcgetattr(STDIN_FILENO, &term_settings);
term_settings.c_lflag ^= ICANON | ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &term_settings);
printf("\nWhere do you want to place your %c?\n", *player);
while ((c = getc(stdin)) != EOF) {
for (i = 0; i < 9; i++) {
cell_bm = 0b100000000 >> i;
if (empty_cells & cell_bm && c - '1' == i) {
/* restoring old term settings */
term_settings.c_lflag ^= ICANON | ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &term_settings);
return i;
}
}
}
exit(0);
}
uint8_t evaluate(Board *board)
{
uint8_t i;
uint16_t wincomb;
/*
0 -> position not terminal
1 reserved for -infinity
2 -> player2 wins
3 -> draw = noone wins
4 -> player1 wins
5 reserved for infinity
*/
for (i = 0; i < 8; i++) {
wincomb = wincombs[i];
if ((wincomb & board->max_player.bm) == wincomb) {
return 4;
}
if ((wincomb & board->min_player.bm) == wincomb) {
return 2;
}
}
if ((board->max_player.bm | board->min_player.bm) == 0b111111111) {
return 3;
}
return 0;
}
void checkposition(Board *board)
{
uint8_t eval = evaluate(board);
uint16_t color_bm;
uint8_t i;
uint16_t wincomb;
if (eval) {
render(board);
if (eval == 3) {
printf("\nDRAW\n\n");
} else if (eval == 4) {
printf("\n%c won the game!\n\n", board->max_player.icon);
} else if (eval == 2) {
printf("\n%c won the game!\n\n", board->min_player.icon);
}
exit(0);
}
}
uint8_t minimax_alpha_beta(Board *board, bool ismax, unsigned char alpha, unsigned char beta)
{
uint16_t empty_cells_bm = ~(board->max_player.bm | board->min_player.bm) & 0b111111111;
uint8_t eval = evaluate(board);
uint8_t bestvalue;
uint8_t i;
uint16_t cell_bm;
uint8_t minimax_value;
if (eval) {
return eval;
}
if (ismax) {
bestvalue = 1;
for (i = 0; i < 9; i++) {
cell_bm = 0b100000000 >> i;
if (cell_bm & empty_cells_bm) {
board->max_player.bm ^= cell_bm;
minimax_value = minimax_alpha_beta(board, false, alpha, beta);
board->max_player.bm ^= cell_bm;
if (minimax_value > bestvalue) {
bestvalue = minimax_value;
if (bestvalue > alpha) {
alpha = bestvalue;
}
if (alpha >= beta) {
break;
}
}
}
}
} else {
bestvalue = 5;
for (i = 0; i < 9; i++) {
cell_bm = 0b100000000 >> i;
if (cell_bm & empty_cells_bm) {
board->min_player.bm ^= cell_bm;
minimax_value = minimax_alpha_beta(board, true, alpha, beta);
board->min_player.bm ^= cell_bm;
if (minimax_value < bestvalue) {
bestvalue = minimax_value;
if (bestvalue < beta) {
beta = bestvalue;
}
if (alpha >= beta) {
break;
}
}
}
}
}
return bestvalue;
}
uint8_t best_move(Board *board, bool ismax)
{
uint8_t bestvalue;
uint8_t i;
uint16_t cell_bm;
uint16_t empty_cells_bm = ~(board->max_player.bm | board->min_player.bm) & 0b111111111;
uint8_t minimax_value;
uint8_t cell_values[9];
uint8_t ctr = 0;
uint8_t choice;
if (ismax) {
bestvalue = 1;
for (i = 0; i < 9; i++) {
cell_bm = 0b100000000 >> i;
if (cell_bm & empty_cells_bm) {
board->max_player.bm ^= cell_bm;
minimax_value = minimax_alpha_beta(board, !ismax, 1, 5);
board->max_player.bm ^= cell_bm;
if (minimax_value == bestvalue) {
cell_values[i] = minimax_value;
++ctr;
} else if (minimax_value > bestvalue) {
bestvalue = minimax_value;
cell_values[i] = minimax_value;
ctr = 1;
}
}
}
} else {
bestvalue = 5;
for (i = 0; i < 9; i++) {
cell_bm = 0b100000000 >> i;
if (cell_bm & empty_cells_bm) {
board->min_player.bm ^= cell_bm;
minimax_value = minimax_alpha_beta(board, !ismax, 1, 5);
board->min_player.bm ^= cell_bm;
if (minimax_value == bestvalue) {
cell_values[i] = minimax_value;
++ctr;
} else if (minimax_value < bestvalue) {
bestvalue = minimax_value;
cell_values[i] = minimax_value;
ctr = 1;
}
}
}
}
choice = random_number(ctr);
for (i = 0; i < 9; i++) {
if (cell_values[i] == bestvalue) {
if (!choice) {
return i;
} else {
--choice;
}
}
}
return 0b11111111;
}
unsigned int random_number(unsigned int limit)
{
srand(time(0));
return rand() % limit;
}
/* python exectime > 0.066766 */
/* C exectime > 0.003077 */
|