-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pieces.cpp
288 lines (218 loc) · 8.42 KB
/
pieces.cpp
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
/**
* pieces.cpp
*
* the MicroChess project: https://github.com/ripred/MicroChess
*
* Move generation functions for all pieces
*
*/
#include <Arduino.h>
#ifndef ESP32
#include <avr/pgmspace.h>
#endif
#include <stdint.h>
#include "MicroChess.h"
// structure to hold the offsets that a piece can move to
struct offset_t {
index_t x, y;
};
static offset_t constexpr knight_offsets[8] PROGMEM = {
{ -1, +2 }, { -1, -2 }, { -2, +1 }, { -2, -1 },
{ +1, +2 }, { +1, -2 }, { +2, +1 }, { +2, -1 }
};
static offset_t constexpr rook_offsets[4] PROGMEM = {
{ 0, 1 }, { 0, -1 }, { -1, 0 }, { 1, 0 }
};
static offset_t const bishop_offsets[4] PROGMEM = {
{ -1, -1 }, { -1, 1 }, { 1, -1 }, { 1, 1 }
};
// Function to check for forward moves
index_t check_fwd(piece_gen_t &gen, index_t const col, index_t const row) {
if (!isValidPos(col, row)) { return 0; }
gen.move.to = col + row * 8;
if (!isEmpty(board.get(gen.move.to))) { return 0; }
gen.callme(gen);
return 1;
};
index_t add_pawn_moves(piece_gen_t &gen) {
// Stack Management
// DECLARE ALL LOCAL VARIABLES USED IN THIS CONTEXT HERE AND
// DO NOT MODIFY ANYTHING BEFORE CHECKING THE AVAILABLE STACK
index_t to_col, to_row, count, i;
// index_t last_move_to_col, last_move_to_row, last_move_from_row;
Piece op;
// Check for low stack space
if (check_mem(ADD_MOVES)) { return 0; }
// Now we can alter local variables! 😎
// See if we can move 1 spot in front of this pawn
to_col = gen.col;
to_row = gen.row + (gen.whites_turn ? -1 : +1);
gen.move.to = to_col + to_row * 8;
// Count the number of available moves
count = 0;
// Check 1 row ahead
count += check_fwd(gen, to_col, to_row);
if (timeout()) { return count; }
// Check 2 rows ahead
if (!hasMoved(board.get(gen.move.from))) {
to_row += (gen.whites_turn ? -1 : +1);
count += check_fwd(gen, to_col, to_row);
}
if (timeout()) { return count; }
// See if we can capture a piece diagonally
for (i = -1; i <= 1; i += 2) {
// See if the turn has timed out
if (timeout()) { return count; }
to_col = gen.col + i;
to_row = gen.row + (gen.whites_turn ? -1 : +1);
gen.move.to = to_col + to_row * 8;
if (isValidPos(to_col, to_row)) {
// Check diagonal piece
op = board.get(gen.move.to);
if (!isEmpty(op) && getSide(op) != gen.side) {
gen.callme(gen);
count++;
}
// // Check for en-passant
// last_move_from_row = game.last_move.from / 8;
// last_move_to_col = game.last_move.to % 8;
// last_move_to_row = game.last_move.to / 8;
// if (last_move_to_col == to_col && last_move_to_row == gen.row) {
// if (abs(int(last_move_from_row) - int(last_move_to_row)) > 1) {
// op = board.get(last_move_to_col + gen.row * 8);
// if (Pawn == getType(op) && getSide(op) != gen.side) {
// gen.move.to = to_col + (gen.row + (gen.whites_turn ? -1 : 1)) * 8;
// gen.callme(gen);
// count++;
// }
// }
// }
}
}
return count;
} // add_pawn_moves(piece_gen_t &gen)
index_t gen_moves(piece_gen_t &gen, offset_t const * const ptr, index_t const num_dirs, index_t const num_iter) {
// Stack Management
// DECLARE ALL LOCAL VARIABLES USED IN THIS CONTEXT HERE AND
// DO NOT MODIFY ANYTHING BEFORE CHECKING THE AVAILABLE STACK
index_t count;
index_t i;
index_t x;
index_t y;
index_t iter;
Piece other_piece;
// Check for low stack space
if (check_mem(ADD_MOVES)) { return 0; }
// Now we can alter local variables! 😎
// Count the number of available moves
count = 0;
for (i = 0; i < num_dirs; i++) {
x = gen.col + pgm_read_byte(&ptr[i].x);
y = gen.row + pgm_read_byte(&ptr[i].y);
for (iter = 0; iter < num_iter && isValidPos(x, y); iter++) {
// See if the turn has timed out
if (timeout()) {
return count;
}
gen.move.to = x + y * 8;
other_piece = board.get(gen.move.to);
if (isEmpty(other_piece)) {
gen.callme(gen);
count++;
}
else if (getSide(other_piece) != gen.side) {
gen.callme(gen);
count++;
break;
}
else {
break;
}
x += pgm_read_byte(&ptr[i].x);
y += pgm_read_byte(&ptr[i].y);
}
}
return count;
} // gen_moves(...)
index_t add_knight_moves(piece_gen_t &gen) {
// Stack Management
// DECLARE ALL LOCAL VARIABLES USED IN THIS CONTEXT HERE AND
// DO NOT MODIFY ANYTHING BEFORE CHECKING THE AVAILABLE STACK
// Check for low stack space
if (check_mem(ADD_MOVES)) { return 0; }
// Now we can alter local variables! 😎
return gen_moves(gen, (offset_t *) pgm_get_far_address(knight_offsets), ARRAYSZ(knight_offsets), 1);
} // add_knight_moves(piece_gen_t &gen)
index_t add_rook_moves(piece_gen_t &gen) {
// Stack Management
// DECLARE ALL LOCAL VARIABLES USED IN THIS CONTEXT HERE AND
// DO NOT MODIFY ANYTHING BEFORE CHECKING THE AVAILABLE STACK
// Check for low stack space
if (check_mem(ADD_MOVES)) { return 0; }
// Now we can alter local variables! 😎
return gen_moves(gen, (offset_t *) pgm_get_far_address(rook_offsets), ARRAYSZ(rook_offsets), 7);
} // add_rook_moves(piece_gen_t &gen)
index_t add_bishop_moves(piece_gen_t &gen) {
// Stack Management
// DECLARE ALL LOCAL VARIABLES USED IN THIS CONTEXT HERE AND
// DO NOT MODIFY ANYTHING BEFORE CHECKING THE AVAILABLE STACK
// Check for low stack space
if (check_mem(ADD_MOVES)) { return 0; }
// Now we can alter local variables! 😎
return gen_moves(gen, (offset_t *) pgm_get_far_address(bishop_offsets), ARRAYSZ(bishop_offsets), 7);
} // add_bishop_moves(piece_gen_t &gen)
index_t add_king_moves(piece_gen_t &gen) {
// Stack Management
// DECLARE ALL LOCAL VARIABLES USED IN THIS CONTEXT HERE AND
// DO NOT MODIFY ANYTHING BEFORE CHECKING THE AVAILABLE STACK
index_t count;
Piece rook;
Bool empty_knight;
Bool empty_bishop;
Bool empty_queen;
// Check for low stack space
if (check_mem(ADD_MOVES)) { return 0; }
// Now we can alter local variables! 😎
// Count the number of available moves
count = 0;
count += gen_moves(gen, (offset_t *) pgm_get_far_address(rook_offsets), ARRAYSZ(rook_offsets), 1);
count += gen_moves(gen, (offset_t *) pgm_get_far_address(bishop_offsets), ARRAYSZ(bishop_offsets), 1);
// check for castling
if (!hasMoved(gen.piece)) {
// check King's side (right-hand side from white's view)
rook = board.get(7 + gen.row * 8);
empty_knight = isEmpty(board.get(1 + gen.row * 8));
empty_bishop = isEmpty(board.get(2 + gen.row * 8));
if (!isEmpty(rook) && !hasMoved(rook)) {
if (empty_knight && empty_bishop) {
// We can castle on the King's side
gen.move.to = 2 + gen.row * 8;
gen.callme(gen);
count++;
}
}
// check Queen's side (left-hand side from white's view)
rook = board.get(0 + gen.row * 8);
if (!isEmpty(rook) && !hasMoved(rook)) {
empty_queen = isEmpty(board.get(4 + gen.row * 8));
empty_knight = isEmpty(board.get(6 + gen.row * 8));
empty_bishop = isEmpty(board.get(5 + gen.row * 8));
if (empty_knight && empty_bishop && empty_queen) {
// We can castle on the Queens's side
gen.move.to = 6 + gen.row * 8;
gen.callme(gen);
count++;
}
}
}
return count;
} // add_king_moves(piece_gen_t &gen)
index_t add_queen_moves(piece_gen_t &gen) {
// Stack Management
// DECLARE ALL LOCAL VARIABLES USED IN THIS CONTEXT HERE AND
// DO NOT MODIFY ANYTHING BEFORE CHECKING THE AVAILABLE STACK
// Check for low stack space
if (check_mem(ADD_MOVES)) { return 0; }
// Now we can alter local variables! 😎
return add_rook_moves(gen) + add_bishop_moves(gen);
} // add_queen_moves(piece_gen_t &gen)