-
Notifications
You must be signed in to change notification settings - Fork 0
/
movegen.h
184 lines (154 loc) · 6.79 KB
/
movegen.h
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
#ifndef movegen_h
#define movegen_h
#include "board.h"
#include "nextmove.h"
#include "parameters.h"
#include "macros.h"
//void CaptureGen(CHESSBOARD *b, MOVELIST * m);
//void CaptureGen(CHESSBOARD *b, QMOVELIST * capture_list);
void nonCaptureGen(CHESSBOARD *b, MOVELIST * m);
void uncheckGen(CHESSBOARD * b, MOVELIST * m);
template <class LIST> void CaptureGen(CHESSBOARD * b, LIST * capture_list) {
//void CaptureGen(CHESSBOARD *b, MOVELIST * capture_list) {
/*************************************************************************
Generates captures and promotions. In order:
-Knights
-Bishops
-Rook
-Queen
-King
-Pawn
*************************************************************************/
//vector<MOVE> capture_list;
//capture_list.reserve(MOVE_LIST_SIZE);
bool toMove = b->getActivePlayer();
bool opponent = !toMove;
/* -------------------------------------
KNIGHT CAPTURES:
------------------------------------- */
bitboard pieceDB = b->getPieceBB(toMove,nKnight);
while(pieceDB) {
unsigned char from = getMostAdvanced(toMove, &pieceDB);
bitboard attackMoveDB = mask::knight_moves[from] & b->getOccupiedBB(opponent);
while(attackMoveDB) {
unsigned char target = bitscan_msb(attackMoveDB);
capture_list->addCapture(MOVE(from,target,toMove, nKnight, b->getBoard(target)));
attackMoveDB ^= (((bitboard)1) << target);
}
pieceDB ^= (((bitboard)1) << from); //remove bit from DB
}
/* -------------------------------------
BISHOP:
------------------------------------- */
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(!toMove);
while(toDB) {
unsigned char target = getMostAdvanced(toMove, &toDB);
capture_list->addCapture(MOVE(from,target,toMove,nBishop, b->getBoard(target)));
toDB^=(((bitboard)1)<<target);
}
pieceDB^=(((bitboard)1)<<from);
}
/* -------------------------------------
ROOK CAPTURES:
------------------------------------- */
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(!toMove);
while(toDB) {
unsigned char target = getMostAdvanced(toMove, &toDB);
capture_list->addCapture(MOVE(from,target,toMove,nRook, b->getBoard(target)));
toDB^=(((bitboard)1)<<target);
}
pieceDB^=(((bitboard)1)<<from);
}
/* -------------------------------------
QUEEN CAPTURES:
------------------------------------- */
pieceDB = 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]]
| mask::MagicRookMoves[from][((b->getOccupiedBB(BOTH) & mask::MagicRookMask[from]) * mask::MagicRookNumbers[from])>>mask::MagicRookShift[from]] )
& b->getOccupiedBB(!toMove);
while(toDB) {
unsigned char target = getMostAdvanced(toMove, &toDB);
capture_list->addCapture(MOVE(from,target,toMove,nQueen, b->getBoard(target)));
toDB^=(((bitboard)1)<<target);
}
/*
toDB = mask::MagicRookMoves[from][((b->getOccupiedBB(BOTH) & mask::MagicRookMask[from]) * mask::MagicRookNumbers[from])>>mask::MagicRookShift[]]
& b->getOccupiedBB(!toMove);
while(toDB) {
unsigned char target = getMostAdvanced(toMove, &toDB);
capture_list->addCapture(MOVE(from,target,toMove,nQueen, b->getBoard(target)));
toDB^=(((bitboard)1)<<target);
}
*/
pieceDB^=(((bitboard)1)<<from);
}
/* -------------------------------------
KING CAPTURES:
------------------------------------- */
unsigned char from = bitscan_msb(b->getPieceBB(toMove,nKing));
bitboard validMoveDB = mask::king_moves[from] & b->getOccupiedBB(opponent);
while(validMoveDB) {
unsigned char target = bitscan_msb(validMoveDB);
capture_list->addCapture(MOVE(from ,target, toMove, nKing, b->getBoard(target)));
validMoveDB ^= (((bitboard)1) << target);
}
/* -------------------------------------
PAWN CAPTURES:
------------------------------------- */
//Pawn kills:
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);
//Check for normal attacks (non promote kills):
bitboard kill_list = (mask::pawn_captures[toMove][from] & b->getOccupiedBB(opponent)) & ~(RankMask(1)|RankMask(8));
while(kill_list) {
int index = bitscan_msb(kill_list);
capture_list->addCapture(MOVE(from,index,toMove, nPawn,b->getBoard(index)) );
kill_list ^= (((bitboard)1) << index);
}
//En passant kills:
kill_list = mask::pawn_captures[toMove][from] & mask::square[b->getEnPassantIndex()];
if(kill_list) {
int index = bitscan_msb(kill_list);
capture_list->addCapture(MOVE(from,index,toMove, nPawn,0,0,nPieceValue[nPawn]));
}
pieceDB ^= (((bitboard)1) << from); //remove bit from DB
}
//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);
capture_list->addCapture( MOVE(from,to,toMove,nPawn,b->getBoard(to), nQueen, Q_PROMOTE_CAPTURE) );
//capture_list->addCapture( MOVE(from,to,toMove,nPawn,b->getBoard(to), nKnight, N_PROMOTE_CAPTURE) );
//capture_list->addCapture( MOVE(from,to,toMove,nPawn,b->getBoard(to), nBishop, B_PROMOTE_CAPTURE) );
//capture_list->addCapture( 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);
capture_list->addCapture( MOVE(from,to,toMove,nPawn,0, nQueen, Q_PROMOTE) );
//capture_list->addCapture( MOVE(from,to,toMove,nPawn, 0, nKnight, N_PROMOTE) );
//capture_list->addCapture( MOVE(from,to,toMove,nPawn, 0, nBishop, B_PROMOTE) );
//capture_list->addCapture( MOVE(from,to,toMove,nPawn, 0, nRook, R_PROMOTE) );
}
}
}
#endif