forked from hoshir/zebra
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmoves.c
415 lines (326 loc) · 9.65 KB
/
moves.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
/*
File: moves.c
Created: June 30, 1997
Modified: April 24, 2001
Author: Gunnar Andersson ([email protected])
Contents: The move generator.
*/
#include <stdio.h>
#include <stdlib.h>
#include "cntflip.h"
#include "constant.h"
#include "doflip.h"
#include "globals.h"
#include "hash.h"
#include "macros.h"
#include "moves.h"
#include "patterns.h"
#include "search.h"
#include "texts.h"
#include "unflip.h"
/* Global variables */
int disks_played;
int move_count[MAX_SEARCH_DEPTH];
int move_list[MAX_SEARCH_DEPTH][64];
int *first_flip_direction[100];
int flip_direction[100][16]; /* 100 * 9 used */
int **first_flipped_disc[100];
int *flipped_disc[100][8];
const int dir_mask[100] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 81, 81, 87, 87, 87, 87, 22, 22, 0,
0, 81, 81, 87, 87, 87, 87, 22, 22, 0,
0, 121, 121, 255, 255, 255, 255, 182, 182, 0,
0, 121, 121, 255, 255, 255, 255, 182, 182, 0,
0, 121, 121, 255, 255, 255, 255, 182, 182, 0,
0, 121, 121, 255, 255, 255, 255, 182, 182, 0,
0, 41, 41, 171, 171, 171, 171, 162, 162, 0,
0, 41, 41, 171, 171, 171, 171, 162, 162, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
const int move_offset[8] = { 1, -1, 9, -9, 10, -10, 11, -11 };
/* Local variables */
static int flip_count[65];
static int sweep_status[MAX_SEARCH_DEPTH];
/*
INIT_MOVES
Initialize the move generation subsystem.
*/
void
init_moves( void ) {
int i, j, k;
int pos;
int feasible;
for ( i = 1; i <= 8; i++ )
for ( j = 1; j <= 8; j++ ) {
pos = 10 * i + j;
for ( k = 0; k <= 8; k++ )
flip_direction[pos][k] = 0;
feasible = 0;
for ( k = 0; k < 8; k++ )
if ( dir_mask[pos] & (1 << k) ) {
flip_direction[pos][feasible] = move_offset[k];
feasible++;
}
first_flip_direction[pos] = &flip_direction[pos][0];
}
}
/*
RESET_GENERATION
Prepare for move generation at a given level in the tree.
*/
INLINE static void
reset_generation( int side_to_move ) {
sweep_status[disks_played] = 0;
}
/*
GENERATE_SPECIFIC
*/
INLINE int
generate_specific( int curr_move, int side_to_move ) {
return AnyFlips_compact( board, curr_move, side_to_move,
OPP( side_to_move ) );
}
/*
GENERATE_MOVE
side_to_move = the side to generate moves for
Generate the next move in the ordering. This way not all moves possible
in a position are generated, only those who need be considered.
*/
INLINE int
generate_move(int side_to_move) {
int move;
int move_index = 0;
move_index = sweep_status[disks_played];
while ( move_index < MOVE_ORDER_SIZE ) {
move = sorted_move_order[disks_played][move_index];
if ( (board[move] == EMPTY) &&
generate_specific( move, side_to_move ) ) {
sweep_status[disks_played] = move_index + 1;
return move;
}
else
move_index++;
}
sweep_status[disks_played] = move_index;
return ILLEGAL;
}
/*
GENERATE_ALL
Generates a list containing all the moves possible in a position.
*/
INLINE void
generate_all( int side_to_move ) {
int count, curr_move;
reset_generation( side_to_move );
count = 0;
curr_move = generate_move( side_to_move );
while ( curr_move != ILLEGAL ) {
move_list[disks_played][count] = curr_move;
count++;
curr_move = generate_move( side_to_move );
}
move_list[disks_played][count] = ILLEGAL;
move_count[disks_played] = count;
}
/*
COUNT_ALL
Counts the number of moves for one player.
*/
INLINE int
count_all( int side_to_move, int empty ) {
int move;
int move_index;
int mobility;
int found_empty;
mobility = 0;
found_empty = 0;
for ( move_index = 0; move_index < MOVE_ORDER_SIZE; move_index++ ) {
move = sorted_move_order[disks_played][move_index];
if ( board[move] == EMPTY ) {
if ( generate_specific( move, side_to_move ) )
mobility++;
found_empty++;
if ( found_empty == empty )
return mobility;
}
}
return mobility;
}
/*
GAME_IN_PROGRESS
Determines if any of the players has a valid move.
*/
int
game_in_progress( void ) {
int black_count, white_count;
generate_all( BLACKSQ );
black_count = move_count[disks_played];
generate_all( WHITESQ );
white_count = move_count[disks_played];
return (black_count > 0) || (white_count > 0);
}
/*
MAKE_MOVE
side_to_move = the side that is making the move
move = the position giving the move
Makes the necessary changes on the board and updates the
counters.
*/
INLINE int
make_move( int side_to_move, int move, int update_hash ) {
if (board[move] == 0 || board[move] == 2) {
// This should be unreachable, but fuzzer found an instance where it happens:
// -r 0 -l 9 6 3 5 19 0 -repeat 4 -p 1 -b 1 -w 0 -h 19 -dev 17 75 94.33498 -g tests/resources/board.txt -time 40 48 6 24
// continuing here can lead to index out of bounds somewhere else, so we return 0 here.
// That only causes fatal error in PV completion later
return 0;
}
// we could replace the above if with these asserts, too
// assert(board[move] != 0);
// assert(board[move] != 2);
int flipped;
unsigned int diff1, diff2;
if ( update_hash ) {
flipped = DoFlips_hash( move, side_to_move );
if ( flipped == 0 )
return 0;
diff1 = hash_update1 ^ hash_put_value1[side_to_move][move];
diff2 = hash_update2 ^ hash_put_value2[side_to_move][move];
hash_stored1[disks_played] = hash1;
hash_stored2[disks_played] = hash2;
hash1 ^= diff1;
hash2 ^= diff2;
}
else {
flipped = DoFlips_no_hash( move, side_to_move );
if ( flipped == 0 )
return 0;
hash_stored1[disks_played] = hash1;
hash_stored2[disks_played] = hash2;
}
flip_count[disks_played] = flipped;
board[move] = side_to_move;
if ( side_to_move == BLACKSQ ) {
piece_count[BLACKSQ][disks_played + 1] =
piece_count[BLACKSQ][disks_played] + flipped + 1;
piece_count[WHITESQ][disks_played + 1] =
piece_count[WHITESQ][disks_played] - flipped;
}
else { /* side_to_move == WHITESQ */
piece_count[WHITESQ][disks_played + 1] =
piece_count[WHITESQ][disks_played] + flipped + 1;
piece_count[BLACKSQ][disks_played + 1] =
piece_count[BLACKSQ][disks_played] - flipped;
}
disks_played++;
return flipped;
}
/*
MAKE_MOVE_NO_HASH
side_to_move = the side that is making the move
move = the position giving the move
Makes the necessary changes on the board. Note that the hash table
is not updated - the move has to be unmade using UNMAKE_MOVE_NO_HASH().
*/
INLINE int
make_move_no_hash( int side_to_move, int move ) {
int flipped;
flipped = DoFlips_no_hash( move, side_to_move );
if ( flipped == 0 )
return 0;
flip_count[disks_played] = flipped;
board[move] = side_to_move;
#if 1
if ( side_to_move == BLACKSQ ) {
piece_count[BLACKSQ][disks_played + 1] =
piece_count[BLACKSQ][disks_played] + flipped + 1;
piece_count[WHITESQ][disks_played + 1] =
piece_count[WHITESQ][disks_played] - flipped;
}
else { /* side_to_move == WHITESQ */
piece_count[WHITESQ][disks_played + 1] =
piece_count[WHITESQ][disks_played] + flipped + 1;
piece_count[BLACKSQ][disks_played + 1] =
piece_count[BLACKSQ][disks_played] - flipped;
}
#else
piece_count[side_to_move][disks_played + 1] =
piece_count[side_to_move][disks_played] + flipped + 1;
piece_count[OPP( side_to_move )][disks_played + 1] =
piece_count[OPP( side_to_move )][disks_played] - flipped;
#endif
disks_played++;
return flipped;
}
/*
UNMAKE_MOVE
Takes back a move.
*/
INLINE void
unmake_move( int side_to_move, int move ) {
board[move] = EMPTY;
if (disks_played < 1 || disks_played > MAX_SEARCH_DEPTH) {
return;
}
disks_played--;
hash1 = hash_stored1[disks_played];
hash2 = hash_stored2[disks_played];
UndoFlips_inlined( flip_count[disks_played], OPP( side_to_move ) );
}
/*
UNMAKE_MOVE_NO_HASH
Takes back a move. Only to be called when the move was made without
updating hash table, preferrable through MAKE_MOVE_NO_HASH().
*/
INLINE void
unmake_move_no_hash( int side_to_move, int move ) {
board[move] = EMPTY;
disks_played--;
UndoFlips_inlined( flip_count[disks_played], OPP( side_to_move ) );
}
/*
VALID_MOVE
Determines if a move is legal.
*/
int
valid_move( int move, int side_to_move ) {
int i, pos, count;
if ( (move < 11) || (move > 88) || (board[move] != EMPTY) )
return FALSE;
for ( i = 0; i < 8; i++ )
if ( dir_mask[move] & (1 << i) ) {
for ( pos = move + move_offset[i], count = 0;
board[pos] == OPP( side_to_move ); pos += move_offset[i], count++ )
;
if ( board[pos] == side_to_move ) {
if ( count >= 1 )
return TRUE;
}
}
return FALSE;
}
/*
GET_MOVE
Prompts the user to enter a move and checks if the move is legal.
*/
int
get_move( int side_to_move ) {
char buffer[255];
int ready = 0;
int curr_move;
while ( !ready ) {
if ( side_to_move == BLACKSQ )
printf( "%s: ", BLACK_PROMPT );
else
printf( "%s: ", WHITE_PROMPT );
scanf( "%s", buffer );
curr_move = atoi( buffer );
ready = valid_move( curr_move, side_to_move );
if ( !ready ) {
curr_move = (buffer[0] - 'a' + 1) + 10 * (buffer[1] - '0');
ready = valid_move( curr_move, side_to_move );
}
}
return curr_move;
}