-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.cpp
More file actions
197 lines (172 loc) · 6.44 KB
/
Copy pathutilities.cpp
File metadata and controls
197 lines (172 loc) · 6.44 KB
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
#include <vector>
#include <string>
#include <sstream>
#include "utilities.h"
#include "bitboards.h"
using namespace std;
string index_to_alg(int index) {
char file = 97 + (7-(index%8));
char rank = (index / 8) + 49;
string s = "";
s += file;
s += rank;
return(s);
}
int alg_to_index(string alg) {
char file = alg.at(0);
char rank = alg.at(1);
int index = ((rank-48) * 8) - (file-96);
return index;
}
int str_to_int(string str) {
int numb;
istringstream ( str ) >> numb;
return numb;
}
// floor_2n returns the closest power of 2 not greater than l
unsigned long long floor_2n(unsigned long long l) {
unsigned char biggest_bit = bitscan_msb(l);
if(biggest_bit == 64) {
return 0;
}
return ((unsigned long long)1 << biggest_bit);
}
/*
void backupMakeMove(MOVE m) {
// m.from - index of piece location.
// m.to - index of target square.
// TODO: -change enpassant m.to only 1 variariable instead of 2.
// -does capture work correctly for enpassant?
// -pawn promotion!!!!
// -optimise the way castling and enpassant is done.
//
// --------->Put a counter for each condition in the if/else chain. order accordingly.
//
// Q: is ~ or ! faster?
// Q: could bitboards in place of m.from/m.to be faster?
//Move count:
if(active_player == WHITE)
full_moves++;
//50 half move rule:
half_moves++;
//reset en passant possibility:
short old_en_passant_index = en_passant_index;
if(en_passant_index != 64 ) {
//only XOR the zobrist key if it actually has a value.
zobrist_key ^= zobrist::enPassant[en_passant_index];
en_passant_index = 64;
}
//Adjust the mailbox board representation:
board[m.to] = m.active_piece_id;
board[m.from] = 0;
//Adjust the bitboard:
bitboard mask = ((i << m.to) | (i << m.from));
pieceBB[active_player][m.active_piece_id] ^= mask;
occupiedBB[active_player] ^= mask;
//Update Zobrist Key:
zobrist_key ^= zobrist::pieces[active_player][m.active_piece_id][m.from];
zobrist_key ^= zobrist::pieces[active_player][m.active_piece_id][m.to];
if (m.captured_piece != 0) {
//adjust BBs for a capture occuring.
pieceBB[!active_player][m.captured_piece] ^= (i << m.to);
occupiedBB[!active_player] ^= (i << m.to);
//Remove the captured piece from the Zobrist Key:
zobrist_key ^= zobrist::pieces[!active_player][m.captured_piece][m.to];
//Adjust material score:
if(active_player == WHITE)
material_score += nPieceValue[m.captured_piece];
else
material_score -= nPieceValue[m.captured_piece];
//Reset the 50 half move rule:
half_moves = 0;
//clearHistory();
}
if(m.active_piece_id == nPawn) {
//set enPassant if needed.
if( ((i << m.from) & mask::pawns_spawn[active_player])
&& ((i << m.to) & mask::pawn_double_jump[active_player]) ) {
en_passant_index = bitscan_lsb(mask::pawn_enPassant[active_player][m.from]);
zobrist_key ^= zobrist::enPassant[en_passant_index];
}
//check for an enPassant capture:
else if (m.to == old_en_passant_index) {
pieceBB[!active_player][nPawn] ^= mask::pawn_advances[!active_player][m.to];
occupiedBB[!active_player] ^= mask::pawn_advances[!active_player][m.to];
int removedPieceIndex = bitscan_lsb(mask::pawn_advances[!active_player][m.to]);
board[removedPieceIndex] = 0;
zobrist_key ^= zobrist::pieces[!active_player][nPawn][removedPieceIndex];
}
//Check for pawn promotion:
else if (m.promote_piece_to != 0 ) {
board[m.to] = m.promote_piece_to;
pieceBB[active_player][nPawn] ^= (i << m.to);
pieceBB[active_player][m.promote_piece_to] ^= (i << m.to);
zobrist_key ^= zobrist::pieces[active_player][nPawn][m.to];
zobrist_key ^= zobrist::pieces[active_player][m.promote_piece_to][m.to];
//Adjust material score:
if(active_player == WHITE)
material_score += nPieceValue[m.promote_piece_to] - nPieceValue[nPawn];
else
material_score -= nPieceValue[m.promote_piece_to] + nPieceValue[nPawn];
}
//Reset the 50 half move rule:
half_moves = 0;
//clearHistory();
}
else if(m.active_piece_id == nRook) {
//Cancel some castling posibilities:
if(~occupiedBB[active_player] & mask::kRook_spawn[active_player]) {
if(kingCastle[active_player] == true) { //this check is m.to prevent un-needed application of XOR'ing the key
kingCastle[active_player] = false;
zobrist_key ^= zobrist::castling[active_player][KING_SIDE];
}
}
if(~occupiedBB[active_player] & mask::qRook_spawn[active_player]) {
if(queenCastle[active_player] == true) { //this check is m.to prevent un-needed application of XOR'ing the key
queenCastle[active_player] = false;
zobrist_key ^= zobrist::castling[active_player][QUEEN_SIDE];
}
}
}
else if(m.active_piece_id == nKing) {
//Cancel castling posibilities:
if(kingCastle[active_player] == true ) { //this check is to prevent un-needed application of XOR'ing the key
kingCastle[active_player] = false;
zobrist_key ^= zobrist::castling[active_player][KING_SIDE];
}
if(queenCastle[active_player] == true ) { //this check is to prevent un-needed application of XOR'ing the key
queenCastle[active_player] = false;
zobrist_key ^= zobrist::castling[active_player][QUEEN_SIDE];
}
//Was this a Castle?
if(mask::king_spawn[active_player] & (i << m.from)) {
if((mask::kCastle[active_player] & (i << m.to)) ) {
//king side castle! So move the rook.
bitboard mask = ( mask::kBishop_spawn[active_player] | mask::kRook_spawn[active_player] );
board[bitscan_lsb(mask::kBishop_spawn[active_player])] = nRook;
board[bitscan_lsb(mask::kRook_spawn[active_player])] = 0;
pieceBB[active_player][nRook] ^= mask;
occupiedBB[active_player] ^= mask;
//update key:
zobrist_key ^= zobrist::pieces[active_player][nRook][bitscan_lsb(mask)];
zobrist_key ^= zobrist::pieces[active_player][nRook][bitscan_msb(mask)];
}
else if(mask::qCastle[active_player] & (i << m.to)) {
//queen side castle! So move the rook.
bitboard mask = ( mask::queen_spawn[active_player] | mask::qRook_spawn[active_player] );
board[bitscan_lsb(mask::queen_spawn[active_player])] = nRook;
board[bitscan_lsb(mask::qRook_spawn[active_player])] = 0;
pieceBB[active_player][nRook] ^= mask;
occupiedBB[active_player] ^= mask;
//update key:
zobrist_key ^= zobrist::pieces[active_player][nRook][bitscan_lsb(mask)];
zobrist_key ^= zobrist::pieces[active_player][nRook][bitscan_msb(mask)];
}
}
}
occupiedBB[2] = occupiedBB[0] | occupiedBB[1]; //done separately for the sake of castling.
toggleActivePlayer();
//For 3 fold repetition:
//addHistory(zobrist_key);
}
*/