-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboard.cc
391 lines (345 loc) · 9.23 KB
/
board.cc
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
#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include "board.h"
namespace checkers {
Board::Board() :
black(0x00000FFFu),
white(0xFFF00000u),
kings(0x0u),
player(BLACK)
{}
Board::~Board() {}
void Board::new_board()
{
black = 0x00000FFFu;
white = 0xFFF00000u;
kings = 0x0u;
player = BLACK;
}
bool Board::load(const char* file)
{
std::ifstream is;
is.open(file);
unsigned int piece = 0x1;
if(is != NULL) {
char ch;
black = 0;
white = 0;
kings = 0;
while(is.get(ch) != NULL) {
if(ch == 'b') {
if(piece == 0) {
player = BLACK;
}
black |= piece;
white &= ~piece;
kings &= ~piece;
piece = piece<<1;
} else if(ch == 'B') {
black |= piece;
kings |= piece;
white &= ~piece;
piece = piece<<1;
} else if(ch == 'w') {
if(piece == 0) {
player = WHITE;
}
white |= piece;
black &= ~piece;
kings &= ~piece;
piece = piece<<1;
} else if(ch == 'W') {
white |= piece;
kings |= piece;
black &= ~piece;
piece = piece<<1;
} else if(ch == '.') {
black &= ~piece;
white &= ~piece;
kings &= ~piece;
piece = piece<<1;
}
}
is.close();
return true;
}
return false;
}
bool Board::save(const char* file)
{
std::ofstream os;
os.open(file);
if(os != NULL)
{
unsigned int piece = 1;
while(piece != 0)
{
if(piece & black & kings)
os << 'B';
else if(piece & black)
os << 'b';
else if(piece & white & kings)
os << 'W';
else if(piece & white)
os << 'w';
else
os << '.';
piece = piece<<1;
}
player == BLACK ? os << 'b' : os << 'w';
os.close();
return true;
}
return false;
}
unsigned int Board::getCaptureMoves(unsigned int piece)
{
unsigned int moves = 0x0u;
if(piece & white) { // white man
moves = up_left(up_left(piece) & black) | up_right(up_right(piece) & black);
if((piece & kings)) { // white king
moves |= down_left(down_left(piece) & black) | down_right(down_right(piece) & black);
}
}
else if(piece & black) { // black man
moves = down_left(down_left(piece) & white) | down_right(down_right(piece) & white);
if((piece & kings)) { // black pimp
moves |= up_left(up_left(piece) & white) | up_right(up_right(piece) & white);
}
}
return moves & (~(black|white));
}
unsigned int Board::getCaptureMoves() {
unsigned int moves = 0x0u;
if(player == BLACK) {
moves = down_left(down_left(black) & white) |
down_right(down_right(black) & white) |
up_left(up_left(black&kings) & white) |
up_right(up_right(black&kings) & white);
}
else {
moves = up_left(up_left(white) & black) |
up_right(up_right(white) & black) |
down_left(down_left(white&kings) & black) |
down_right(down_right(white&kings) & black);
}
return moves & (~(black|white));
}
unsigned int Board::getMoves(unsigned int piece) {
unsigned int allmask = ~(black|white);
if((piece & kings) != 0) {
return (up_left(piece) | up_right(piece) | down_left(piece) | down_right(piece)) & allmask;
} else if((piece & white) != 0) {
return (up_left(piece) | up_right(piece)) & allmask;
} else if((piece & black) != 0) {
return (down_left(piece) | down_right(piece)) & allmask;
}
return 0x0u;
}
/**
* up_left, up_right, down_left, down_right
* returns the bitmap after piece move one step
*/
inline unsigned int Board::up_left(unsigned const int& piece) {
return (((piece & 0x0F0F0F00)>>4) | ((piece & 0xE0E0E0E0)>>5));
}
inline unsigned int Board::up_right(unsigned const int& piece) {
return (((piece & 0xF0F0F0F0)>>4) | ((piece & 0x07070700)>>3));
}
inline unsigned int Board::down_left(unsigned const int& piece) {
return (((piece & 0x0F0F0F0F)<<4) | ((piece & 0x00E0E0E0)<<3));
}
inline unsigned int Board::down_right(unsigned const int& piece) {
return (((piece & 0x00F0F0F0)<<4) | ((piece & 0x07070707)<<5));
}
inline unsigned int Board::up(unsigned const int& piece) {
return (piece & 0xFFFFFF00)>>8;
}
inline unsigned int Board::down(unsigned const int& piece) {
return (piece & 0x00FFFFFF)<<8;
}
inline unsigned int Board::left(unsigned const int& piece) {
return (piece & 0xEEEEEEEE)>>1;
}
inline unsigned int Board::right(unsigned const int& piece) {
return (piece & 0x77777777)<<1;
}
bool Board::endOfGame() {
return ( getJumpPieces() | getMovePieces() ) == 0;
}
void Board::move(unsigned int from, unsigned int to) {
// regular move
unsigned int mask = player;
--mask; // 1-1=0, 0-1=2^32
black &= ~from;
black |= to & (~mask);
white &= ~from;
white |= to & mask;
mask = ((from & kings) == 0);
--mask;
kings &= ~from;
kings |= to & mask;
// capture
mask = (up_left(from) & down_right(to));
mask |= (up_right(from) & down_left(to));
mask |= (down_left(from) & up_right(to));
mask |= (down_right(from) & up_left(to));
mask = ~mask;
white &= mask;
black &= mask;
kings &= mask;
}
int Board::validateMove(std::vector<unsigned int>& movements)
{
unsigned int moves = 0x0u;
unsigned int men = 0x0u;
std::vector<unsigned int> tmp;
unsigned int from = movements[0];
unsigned int to = movements[1];
if(movements.size() < 2)
return -3;
if(getJumpPieces() != 0x0u)
{
int result = validateCapture(movements, 0, from, tmp);
if(result == 0)
{
std::reverse(tmp.begin(), tmp.end());
movements = tmp;
}
return result;
}
if(movements.size() != 2)
return -1;
player == WHITE ? men = white : men = black;
moves = getMoves(from);
if(((moves & to) != 0) && ((men & from) != 0))
return 0;
else return -1;
}
int Board::validateCapture(std::vector<unsigned int> movements, unsigned int pos, unsigned int from, std::vector<unsigned int>& tmp) {
unsigned int moves = getCaptureMoves(from);
int result = -1;
if(movements.size()-1 == pos)
{
if(moves != 0)
return -2;
else
{
tmp.push_back(from);
return 0;
}
}
unsigned int to = movements[pos+1];
if((moves & to) != 0)
{
Board newboard = *this;
newboard.move(from, to);
result = newboard.validateCapture(movements, pos+1, to, tmp);
if(result == 0)
{
tmp.push_back(from);
}
return result;
}
while(moves != 0)
{
to = (moves & (moves-1)) ^ moves;
moves &= moves-1;
Board newboard = *this;
newboard.move(from, to);
result = newboard.validateCapture(movements, pos, to, tmp);
if(result == 0)
{
tmp.push_back(from);
return 0;
}
}
return result;
}
/**
* get the position between two squares
* OPTIMIZE
*/
unsigned int Board::getCaptureBit(unsigned int from, unsigned int to) {
if(from < to) {
if(down_left(down_left(from)) == to) {
return down_left(from);
} else if(down_right(down_right(from)) == to) {
return down_right(from);
}
} else {
if(up_left(up_left(from)) == to) {
return up_left(from);
} else if(up_right(up_right(from)) == to) {
return up_right(from);
}
}
return 0x0u;
}
void Board::changePlayer() {
player == WHITE ? player = BLACK : player = WHITE;
}
inline bool Board::empty(unsigned int piece) {
return (piece & (white|black)) == 0;
}
void Board::updateKings() {
kings |= (white & 0x0000000Fu);
kings |= (black & 0xF0000000u);
}
/**
* TODO: remove if-statement
*/
unsigned int Board::getJumpPieces() {
unsigned int pieces = 0x0u;
unsigned int allMask = ~(black | white);
if(player == WHITE) {
pieces |= down_right(down_right(up_left((up_left(white) & black)) & allMask));
pieces |= down_left(down_left(up_right((up_right(white) & black)) & allMask));
pieces |= up_right(up_right(down_left((down_left(white & kings) & black)) & allMask));
pieces |= up_left(up_left(down_right((down_right(white & kings) & black)) & allMask));
} else {
pieces |= up_right(up_right(down_left((down_left(black) & white)) & allMask));
pieces |= up_left(up_left(down_right((down_right(black) & white)) & allMask));
pieces |= down_right(down_right(up_left((up_left(black & kings) & white)) & allMask));
pieces |= down_left(down_left(up_right((up_right(black & kings) & white)) & allMask));
}
return pieces;
}
/**
* TODO: remove if-statement
*/
unsigned int Board::getMovePieces() {
unsigned int pieces = 0x0u;
unsigned int allMask = ~(black | white);
if(player == WHITE) {
pieces |= down_right((up_left(white) & allMask));
pieces |= down_left((up_right(white) & allMask));
pieces |= up_right((down_left(white & kings) & allMask));
pieces |= up_left((down_right(white & kings) & allMask));
} else {
pieces |= up_right((down_left(black) & allMask));
pieces |= up_left((down_right(black) & allMask));
pieces |= down_right((up_left(black & kings) & allMask));
pieces |= down_left((up_right(black & kings) & allMask));
}
return pieces;
}
bool Board::operator==(const Board& b) {
return player == b.player && kings == b.kings &&
black == b.black && white == b.white;
}
unsigned int Board::box(unsigned int pieces) {
return up(pieces) |
up_left(up_left(pieces)) |
left(pieces) |
down_left(down_left(pieces)) |
down(pieces) |
down_right(down_right(pieces)) |
right(pieces) |
up_right(up_right(pieces));
}
}