/
movegen.cpp
252 lines (210 loc) · 8.54 KB
/
movegen.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
#include <vector>
#include <algorithm>
#include <iostream>
#include "move.h"
#include "board.h"
#include "utilities.h"
#include "evaluate.h"
#include "nextmove.h"
#include "movegen.h"
using namespace std;
void nonCaptureGen(CHESSBOARD *b, MOVELIST * move_list ) {
/*************************************************************************
Todo: generate the furthest advanced piece's move first.
In order:
-Castles
-Knights
-Bishops
-Rook
-Queen
-King
-Pawn
*************************************************************************/
//vector<MOVE> move_list;
//move_list.reserve(MOVE_LIST_SIZE);
bool toMove = b->getActivePlayer();
bitboard pieceDB;
/* -------------------------------------
Castles:
------------------------------------- */
if(b->getCastlingRights(toMove, KING_SIDE) == true ) {
if(((b->getOccupiedBB(BOTH) & mask::kCastle_path[toMove])==0)
&& (mask::kRook_spawn[toMove] & b->getPieceBB(toMove,nRook))
&& (mask::king_spawn[toMove] & b->getPieceBB(toMove,nKing))
) {
if( !b->isInCheck()
&& !b->isAttacked(bitscan_msb(mask::kBishop_spawn[toMove]))
) {
unsigned char from = bitscan_msb(mask::king_spawn[toMove]);
unsigned char target = bitscan_msb(mask::kCastle[toMove]);
move_list->addMove(MOVE(from,target,toMove,nKing,0,0,K_CASTLE));
}
}
}
if(b->getCastlingRights(toMove, QUEEN_SIDE) == true ) {
if(((b->getOccupiedBB(BOTH) & mask::qCastle_path[toMove]) == 0)
&& (mask::qRook_spawn[toMove] & b->getPieceBB(toMove,nRook))
&& (mask::king_spawn[toMove] & b->getPieceBB(toMove,nKing))
) {
if( !b->isInCheck()
&& !b->isAttacked(bitscan_msb(mask::queen_spawn[toMove]))
) {
unsigned char from = bitscan_msb(mask::king_spawn[toMove]);
unsigned char target = bitscan_msb(mask::qCastle[toMove]);
move_list->addMove(MOVE(from,target,toMove,nKing,0,0,Q_CASTLE));
}
}
}
/* -------------------------------------
REMAINING PROMOTIONS (non-Queen):
------------------------------------- */
//Pawn Promotions To Queen or Capture Promotions:
pieceDB = (b->getPieceBB(toMove,nPawn) & mask::pawns_spawn[!toMove]);
while(pieceDB) {
unsigned char from = getMostAdvanced(toMove, &pieceDB);
bitboard promote_kills = (mask::pawn_captures[toMove][from] & b->getOccupiedBB(!toMove) );
bitboard promote = (mask::pawn_advances[toMove][from] & ~b->getOccupiedBB(BOTH) );
//Promotion via attacks for all possibilities:
while(promote_kills) {
unsigned char to = bitscan_msb(promote_kills);
//move_list->addMove( MOVE(from,to,toMove,nPawn,b->getBoard(to), nQueen, Q_PROMOTE_CAPTURE) );
move_list->addMove( MOVE(from,to,toMove,nPawn,b->getBoard(to), nKnight, N_PROMOTE_CAPTURE) );
move_list->addMove( MOVE(from,to,toMove,nPawn,b->getBoard(to), nBishop, B_PROMOTE_CAPTURE) );
move_list->addMove( MOVE(from,to,toMove,nPawn,b->getBoard(to), nRook, R_PROMOTE_CAPTURE) );
promote_kills ^= (((bitboard)1) << to);
}
pieceDB ^= (((bitboard)1) << from); //remove bit from DB
//Promotion via advancement to Queen only:
if(promote) {
unsigned char to = bitscan_msb(promote);
//move_list->addCapture( MOVE(from,to,toMove,nPawn,0, nQueen, Q_PROMOTE) );
move_list->addMove( MOVE(from,to,toMove,nPawn, 0, nKnight, N_PROMOTE) );
move_list->addMove( MOVE(from,to,toMove,nPawn, 0, nBishop, B_PROMOTE) );
move_list->addMove( MOVE(from,to,toMove,nPawn, 0, nRook, R_PROMOTE) );
}
}
/*
pieceDB = (b->getPieceBB(toMove,nPawn) & mask::pawns_spawn[!toMove]);
while(pieceDB) {
unsigned char from = getMostAdvanced(toMove, &pieceDB);
bitboard promote = (mask::pawn_advances[toMove][from] & ~b->getOccupiedBB(BOTH) );
//Promotion via advance for every piece except queen:
while(promote) {
unsigned char to = bitscan_msb(promote);
move_list->addMove( MOVE(from,to,toMove,nPawn, 0, nKnight, N_PROMOTE) );
move_list->addMove( MOVE(from,to,toMove,nPawn, 0, nBishop, B_PROMOTE) );
move_list->addMove( MOVE(from,to,toMove,nPawn, 0, nRook, R_PROMOTE) );
promote ^= (((bitboard)1) << to);
}
pieceDB ^= (((bitboard)1) << from); //remove bit from DB
}
*/
/* -------------------------------------
KNIGHT MOVES:
------------------------------------- */
pieceDB = b->getPieceBB(toMove,nKnight);
while(pieceDB) {
unsigned char from = getMostAdvanced(toMove, &pieceDB);
bitboard validMoveDB = mask::knight_moves[from] & ~b->getOccupiedBB(BOTH);
while(validMoveDB) {
unsigned char target = bitscan_msb(validMoveDB);
move_list->addMove(MOVE(from,target,toMove, nKnight, 0));
validMoveDB ^= (((bitboard)1) << target);
}
pieceDB ^= (((bitboard)1) << from); //remove bit from DB
}
/* -------------------------------------
BISHOP MOVES:
------------------------------------- */
pieceDB = b->getPieceBB(toMove, nBishop);// | b->getPieceBB(toMove, nQueen);
while(pieceDB) {
unsigned char from = getMostAdvanced(toMove, &pieceDB);
bitboard toDB = mask::MagicBishopMoves[from][((b->getOccupiedBB(BOTH)&mask::MagicBishopMask[from]) * mask::MagicBishopNumbers[from])>>mask::MagicBishopShift[from]]
& ~b->getOccupiedBB(BOTH);
while(toDB) {
unsigned char target = getMostAdvanced(toMove, &toDB);
move_list->addMove(MOVE(from,target,toMove,b->getBoard(from), 0));
toDB^=(((bitboard)1)<<target);
}
pieceDB^=(((bitboard)1)<<from);
}
/* -------------------------------------
ROOK MOVES:
------------------------------------- */
pieceDB = b->getPieceBB(toMove, nRook);// | b->getPieceBB(toMove, nQueen);
while(pieceDB) {
unsigned char from = getMostAdvanced(toMove, &pieceDB);
bitboard toDB = mask::MagicRookMoves[from][((b->getOccupiedBB(BOTH) & mask::MagicRookMask[from]) * mask::MagicRookNumbers[from])>>mask::MagicRookShift[from]]
& ~b->getOccupiedBB(BOTH);
while(toDB) {
unsigned char target = getMostAdvanced(toMove, &toDB);
move_list->addMove(MOVE(from,target,toMove,b->getBoard(from), 0));
toDB^=(((bitboard)1)<<target);
}
pieceDB^=(((bitboard)1)<<from);
}
/* -------------------------------------
QUEEN MOVES:
------------------------------------- */
pieceDB = b->getPieceBB(toMove, nQueen);
while(pieceDB) {
unsigned char from = getMostAdvanced(toMove, &pieceDB);
bitboard toDB = ( mask::MagicRookMoves[from][((b->getOccupiedBB(BOTH) & mask::MagicRookMask[from]) * mask::MagicRookNumbers[from])>>mask::MagicRookShift[from]]
| mask::MagicBishopMoves[from][((b->getOccupiedBB(BOTH)&mask::MagicBishopMask[from]) * mask::MagicBishopNumbers[from])>>mask::MagicBishopShift[from]] )
& ~b->getOccupiedBB(BOTH);
while(toDB) {
unsigned char target = getMostAdvanced(toMove, &toDB);
move_list->addMove(MOVE(from,target,toMove,nQueen, 0));
toDB^=(((bitboard)1)<<target);
}
/*
toDB = mask::MagicBishopMoves[from][((b->getOccupiedBB(BOTH)&mask::MagicBishopMask[from]) * mask::MagicBishopNumbers[from])>>mask::MagicBishopShift[]]
& ~b->getOccupiedBB(BOTH);
while(toDB) {
unsigned char target = getMostAdvanced(toMove, &toDB);
move_list->addMove(MOVE(from,target,toMove,nQueen, 0));
toDB^=(((bitboard)1)<<target);
}
*/
pieceDB^=(((bitboard)1)<<from);
}
/* -------------------------------------
KING MOVES:
------------------------------------- */
pieceDB = b->getPieceBB(toMove,nKing);
unsigned char from = bitscan_msb(pieceDB);
bitboard validMoveDB = mask::king_moves[from] & ~b->getOccupiedBB(BOTH);
while(validMoveDB) {
int target = bitscan_msb(validMoveDB);
move_list->addMove(MOVE(from,target,toMove,nKing, 0));
validMoveDB ^= (((bitboard)1) << target);
}
/* -------------------------------------
PAWN MOVES:
------------------------------------- */
pieceDB = b->getPieceBB(toMove,nPawn) & ~mask::pawns_spawn[!toMove];
while(pieceDB) {
//quit after exhausting all of the bits in the current database.
unsigned char from = getMostAdvanced(toMove, &pieceDB);
//Single Advance:
bitboard advance = mask::pawn_advances[toMove][from] & ~b->getOccupiedBB(BOTH) ;
if(advance) {
unsigned char to = bitscan_msb(advance);
move_list->addMove(MOVE(from,to,toMove, nPawn));
//Double Advance:
advance = mask::pawn_double_advances[toMove][from] & ~b->getOccupiedBB(BOTH);
if(advance) {
to = bitscan_msb(advance);
move_list->addMove(MOVE(from,to,toMove, nPawn));
}
}
pieceDB ^= (((bitboard)1) << from); //remove bit from DB
}
}
/*********************************************************************
uncheckGen is designed to generate moves that get the king out
of check.
*********************************************************************/
void uncheckGen(CHESSBOARD * b, MOVELIST * m) {
// First determine which pieces are attacking the king.
}