/
reversi.c
427 lines (363 loc) · 8.57 KB
/
reversi.c
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
*
* Written by Rasmus A. X. Persson.
*
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <allegro.h>
#define LOWEST (-10000000)
int parity; // equal to 1 if depth is even, and -1 if depth is odd
// evaluate board
int evalb(int *board, int recur, int side);
// calculate next move
int findmv(int side, int *board, int *en, int recur);
int move(int *board, int en, int side);
// what rank is square i?
inline int rank(int i)
{
if (i < 20) return 0; if (i < 30) return 1; if (i < 40) return 2;
if (i < 50) return 3; if (i < 60) return 4; if (i < 70) return 5;
if (i < 80) return 6;
return 7;
}
// what file is square i?
inline int file(int i) { return (i % 10); }
// read the x and y coordinates and convert them into the internal move
// representation
void readmv(int x, int y, int *en)
{
int ef, er;
ef = (int)nearbyintf(((float)x - 50) / 50.f);
er = (int)nearbyintf(((float)y - 50) / 50.f);
*en = ef + 10*er + 11;
}
// print a move in coordinate notation (with "move" prefixed).
void printmv(int en)
{
char move[3];
move[0] = (file(en) - 1) + 97;
move[1] = (7 - rank(en)) + 49;
move[2] = 0;
printf("%s\n", move);
}
// copy board from src to dst
inline void cpboard(int *src, int *dst)
{
unsigned int i;
for (i = 0; i < 100; ++i) dst[i] = src[i];
}
// return a character for each piece or square
inline char pchar(int i)
{
if (i == 1) return 'X';
if (i == -1) return 'O';
if (i == 2) return '|';
return '.';
}
// highlight: animate the placement of the stone
void highlight(int en, int side)
{
int i;
scare_mouse();
if (side == 1)
{
for (i = 0; i < 20; ++i)
{
// go from blackground color to black
circlefill(screen, 50*file(en), 45 + 50*rank(en), 15,
makecol(0, 200 - i*10, 0));
rest(10);
}
}
else
{
for (i = 0; i < 20; ++i)
{
// go from background color to white
circlefill(screen, 50*file(en), 45 + 50*rank(en), 15,
makecol(i*10, 200 + 55*i/20, i*10));
rest(10);
}
}
unscare_mouse();
}
// print board
void printbd(int *board)
{
int i;
scare_mouse();
line(screen, 20, 20, 420, 20, makecol(0, 0, 0));
line(screen, 20, 20, 20, 420, makecol(0, 0, 0));
line(screen, 420, 20, 420, 420, makecol(0, 0, 0));
line(screen, 20, 420, 420, 420, makecol(0, 0, 0));
// vertical lines
for (i = 0; i < 8; ++i)
line(screen, 70 + 50*i, 20, 70 + 50*i, 420, makecol(0, 0, 0));
// horizontal lines
for (i = 0; i < 8; ++i)
line(screen, 20, 70 + 50*i, 420, 70 + 50*i, makecol(0, 0, 0));
// pieces
for (i = 0; i < 100; ++i)
{
if (board[i] == 1)
{
circlefill(screen, 50*file(i), 45 + 50*rank(i), 15,
makecol(0, 0, 0));
}
if (board[i] == -1)
{
circlefill(screen, 50*file(i), 45 + 50*rank(i), 15,
makecol(255, 255, 255));
}
}
unscare_mouse();
}
// print status line beneath board
void printst(int *board, int flag)
{
int iblack = 0, iwhite = 0;
int i;
for (i = 0; i < 100; ++i)
{
if (board[i] == 1) ++iblack;
if (board[i] == -1) ++iwhite;
}
scare_mouse();
if (!flag)
textprintf_centre_ex(screen, font, SCREEN_W/2, 440,
makecol(0, 0, 0), makecol(0, 200, 0),
"Black: %i | White: %i", iblack, iwhite);
else
textprintf_centre_ex(screen, font, SCREEN_W/2, 440,
makecol(0, 0, 0), makecol(0, 200, 0),
"Black: %i [GAME OVER] White: %i", iblack, iwhite);
unscare_mouse();
}
int main(int argc, char *argv[])
{
int x, y, en, side, depth, score;
// true reversi starts with empty board, but that is not implemented yet!
// therefore, here is the othello starting arrangement.
int board[100] =
{
2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0,-1, 1, 0, 0, 0, 2,
2, 0, 0, 0, 1,-1, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 0, 0, 0, 0, 0, 0, 0, 0, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};
if (argc > 1)
{
side = atoi(argv[1]);
}
else
{
printf("Insufficient arguments\n\n");
printf("Syntax: %s {computer's side} [difficulty]\n", argv[0]);
return 0;
}
if (argc == 3)
{
// side is the color that the computer plays
// '1' is black, '-1' white
// depth is the difficulty, the number of plies.
depth = atoi(argv[2]);
}
else
{
depth = 4;
}
if (depth % 2 == 0) parity = 1; else parity = -1;
printf("Computer playing as %c\n", pchar(side));
printf("Difficulty %i\n", depth);
printf("Right-click to quit\n");
if (allegro_init() != 0) return 1;
install_keyboard();
install_mouse();
install_timer();
if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 440, 440 + 20, 0, 0))
{
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Unable to enter graphics mode\n%s\n", allegro_error);
return 1;
}
set_palette(desktop_palette);
clear_to_color(screen, makecol(0, 200, 0)); // green background
textout_centre_ex(screen, font, "REVERSI", SCREEN_W/2, 10,
makecol(0, 0, 0), -1);
printbd(board);
printst(board, 0);
show_mouse(screen);
if (side == 1)
{
// make first move
findmv(side, board, &en, depth);
move(board, en, side);
highlight(en, side);
printbd(board);
printst(board, 0);
}
// game loop
while (1)
{
if (mouse_b & 1)
{
// the user has clicked to place a move
x = mouse_pos >> 16;
y = mouse_pos & 0x0000ffff;
readmv(x, y, &en);
// perform user move
if (move(board, en, -side))
{
// highlight user move
highlight(en, -side);
printbd(board);
printst(board, 0);
// find reply
score = findmv(side, board, &en, depth);
if (move(board, en, side))
{
highlight(en, side);
printbd(board);
printst(board, 0);
}
}
else
{
// check and see if user CAN move at all
en = -1;
score = findmv(-side, board, &en, depth);
if (en == -1)
{
// computer move instead
score = findmv(side, board, &en, depth);
if (move(board, en, side))
{
highlight(en, side);
printbd(board);
printst(board, 0);
}
else
{
// Game over
printst(board, 1);
}
}
}
}
if (mouse_b & 2) return 0; // exit on right-click
}
return 0;
}
END_OF_MAIN() // Macro needed by Allegro.
// returns a positive number if side 'side' is winning (-1 for black).
// if recur = 0, then no recursion.
int evalb(int *board, int recur, int side)
{
unsigned int i;
int t, s, temp[100], val;
val = 0;
cpboard(board, temp); // make a safety copy
// is anything dangerous about to happen?
if (recur > 1)
{
// find possible replies for opposite side
if (recur % 2 == 0) s = parity; else s = -parity;
val = s*findmv(s*side, temp, &t, recur - 1);
}
else
{
// we've reached the end of the ply cycle
// count material
for (i = 0; i < 100; ++i)
if (temp[i] != 2) val += side*temp[i];
}
return val;
}
// makes a search and returns the best move for side 'side' (= 1 or -1)
// the move is reported as square 'en'. it also reports the evaluation score
// expected after this move.
int findmv(int side, int *board, int *en, int recur)
{
int i, temp[100];
int k, best;
cpboard(board, temp);
best = LOWEST;
// find all legal moves, evaluate each one and pick the best one.
for (i = 0; i < 100; ++i)
{
if (move(temp, i, side))
{
k = evalb(temp, recur, side);
if (k > best)
{
*en = i;
best = k;
}
cpboard(board, temp);
}
}
return best;
}
// will perform the move. returns 0 for illegal move, 1 for legal move.
int move(int *board, int en, int side)
{
int i, j, k, l, f, vec[8], vm[8];
// the eight possible adjacent squares in relative coordinates
vec[0] = -11; vec[1] = -10;
vec[2] = -9; vec[3] = -1;
vec[4] = 1; vec[5] = 9;
vec[6] = 10; vec[7] = 11;
// the maximum steps in each of these directions
for (i = 0; i < 8; ++i) vm[i] = 0;
f = 0;
// the legal move has (1) an adjacent square of opposite color
// and (2) a square of the same color along the direction given
// by (1) at most 7 squares distant.
if (en < 0) return 0;
if (board[en]) return 0; // illegal: square occupied
for (i = 0; i < 8; ++i)
{
k = en + vec[i];
if (k < 0 || k > 99) continue;
if (board[k] == -side)
{
// search along this direction for square of same side
l = k;
for (j = 0; j < 7; ++j)
{
l += vec[i];
if (l < 0 || l > 99 || board[l] == 2 || !board[l]) break;
if (board[l] == side)
{
f = 1;
vm[i] = j + 1;
break;
}
}
}
}
if (!f) return 0; // illegal move: no pairing possible
// now flip the pieces
for (i = 0; i < 8; ++i)
{
k = en + vec[i];
l = k;
for (j = 0; j < vm[i]; ++j)
{
if (l < 0 || l > 99 || board[l] == 2) break;
board[l] = side;
l += vec[i];
}
}
board[en] = side;
return 1;
}