-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmove.c
executable file
·164 lines (129 loc) · 4.42 KB
/
move.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
#ifdef DEBUG
#include <stdio.h>
#endif
#include <string.h>
#include "checkers.h"
int generate_moves(bitboard *board, const bool color, struct move *move_list)
{
int n = 0;
#ifdef DEBUG
printf("MOVE: generate_moves, color = %s\n", color ? "black" : "white");
#endif
if ((n = try_capture(board, 0xffffffff, color, move_list)) > 0) {
return n;
}
return try_move(board, color, move_list);
}
int try_move(bitboard *board, const bool color, struct move *next_move)
{
bitboard next, to, from;
const bitboard empty = ~(board[BLACK] | board[WHITE]);
const bitboard move_up = color ? board[KING] : 0xffffffff;
const bitboard move_down = !color ? board[KING] : 0xffffffff;
int n = 0, i;
#ifdef DEBUG
printf("MOVE: try_move\n");
#endif
// check down neighbors
for (i = 0; i < N_DIRS; i++) {
to = DOWN_NEIGHBOR(board[(int)color] & move_down, i) & empty;
while (to) {
next = LAST_ONE(to);
from = UP_NEIGHBOR(next, i);
// modify move board
next_move->board[(int)color] = (board[(int)color] | next) & ~from;
next_move->board[(int)!color] = board[(int)!color];
next_move->board[KING] = ((board[KING] | (next & king_bits[(int)color])) & ~from) |
(DOWN_NEIGHBOR(board[KING], i) & next);
next_move++;
n++;
to ^= next;
}
}
// check up neighbors
for (i = 0; i < N_DIRS; i++) {
to = UP_NEIGHBOR(board[(int)color] & move_up, i) & empty;
while (to) {
next = LAST_ONE(to);
from = DOWN_NEIGHBOR(next, i);
// modify move board
next_move->board[(int)color] = (board[(int)color] | next) & ~from;
next_move->board[(int)!color] = board[(int)!color];
next_move->board[KING] = ((board[KING] | (next & king_bits[(int)color])) & ~from) |
(UP_NEIGHBOR(board[KING], i) & next);
next_move++;
n++;
to ^= next;
}
}
#ifdef DEBUG
printf("MOVE: %d possible moves\n", n);
#endif
return n;
}
int try_capture(bitboard *board, bitboard mask, const bool color, struct move *next_move)
{
bitboard to, cap, from, next;
const bitboard empty = ~(board[WHITE] | board[BLACK]);
const bitboard move_up = (color ? board[KING] : 0xffffffff) & mask;
const bitboard move_down = (!color ? board[KING] : 0xffffffff) & mask;
struct move cur_move;
int n = 0, i, leaves = 0;
#ifdef DEBUG
printf("MOVE: try_capture\n");
#endif
// check down neighbors
for (i = 0; i < N_DIRS; i++) {
to = DOWN_NEIGHBOR(DOWN_NEIGHBOR(board[(int)color] & move_down, i) & board[(int)!color], i) & empty;
while (to) {
next = LAST_ONE(to);
cap = UP_NEIGHBOR(next, i);
from = UP_NEIGHBOR(cap, i);
to ^= next;
cur_move.board[(int)color] = (board[(int)color] & ~from) | next;
cur_move.board[(int)!color] = board[(int)!color] & ~cap;
cur_move.board[KING] = ((board[KING] & ~from) & ~cap) |
(next & (king_bits[(int)color] | DOWN_NEIGHBOR(DOWN_NEIGHBOR(board[KING], i), i)));
/* must stop once we get a king */
if (!(from & board[KING]) && (next & cur_move.board[KING]))
next = 0;
if ((n = try_capture(cur_move.board, next, color, next_move+leaves)) == 0) {
(next_move+leaves)->board[WHITE] = cur_move.board[WHITE];
(next_move+leaves)->board[BLACK] = cur_move.board[BLACK];
(next_move+leaves)->board[KING] = cur_move.board[KING];
leaves++;
}
else
leaves += n;
}
}
// check up neighbors
for (i = 0; i < N_DIRS; i++) {
to = UP_NEIGHBOR(UP_NEIGHBOR(board[(int)color] & move_up, i) & board[(int)!color], i) & empty;
while (to) {
next = LAST_ONE(to);
cap = DOWN_NEIGHBOR(next, i);
from = DOWN_NEIGHBOR(cap, i);
to ^= next;
cur_move.board[(int)color] = (board[(int)color] & ~from) | next;
cur_move.board[(int)!color] = board[(int)!color] & ~cap;
cur_move.board[KING] = ((board[KING] & ~from) & ~cap) |
(next & (king_bits[(int)color] | UP_NEIGHBOR(UP_NEIGHBOR(board[KING], i), i)));
/* must stop once we get a king */
if (!(from & board[KING]) && (next & cur_move.board[KING]))
next = 0;
if ((n = try_capture(cur_move.board, next, color, next_move+leaves)) == 0) {
(next_move+leaves)->board[WHITE] = cur_move.board[WHITE];
(next_move+leaves)->board[BLACK] = cur_move.board[BLACK];
(next_move+leaves)->board[KING] = cur_move.board[KING];
leaves++;
}
else
leaves += n;
}
}
#ifdef DEBUG
printf("MOVE: leaves in capture tree = %d\n", leaves);
#endif
return leaves;
}