Skip to content

Commit 1e5db33

Browse files
authored
Update Board.cpp
1 parent 0e57ba6 commit 1e5db33

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

Board.cpp

+32-22
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@
8484
return board[idx];
8585
}
8686

87-
bool is_promotion = false;
8887
bool Board::can_promotion(int16_t from, int16_t to) {
89-
int16_t promotion_row = side == White ? 7 : 0;
90-
int16_t cur_row = to / 8;
91-
return board[from] == Pawn && cur_row == promotion_row;
88+
const int16_t promotion_row = side == White ? 7 : 0;
89+
const int16_t cur_row = to >> 3;
90+
auto base = side == White ? 1 : -1;
91+
return board[from] == base * Pawn && cur_row == promotion_row;
9292
}
9393

9494
bool Board::is_valid_move(int16_t position){
@@ -125,6 +125,7 @@
125125
cnt_move += 1;
126126
side ^= 1;
127127
}
128+
128129
void Board::kingside_castle(){
129130
auto king_position = side == White ? 60 : 4;
130131
auto rook_position = side == White ? 63 : 7;
@@ -135,6 +136,7 @@
135136
board[rook_position] = Empty;
136137
score += side == White ? 40 : -40;
137138
Board::update_move_info();
139+
hash_value ^= castling_values[side][Kingside];
138140
}
139141

140142
void Board::queenside_castle(){
@@ -147,10 +149,11 @@
147149
board[rook_position] = Empty;
148150
score += side == White ? 40 : -40;
149151
Board::update_move_info();
152+
hash_value ^= castling_values[side][Queenside];
150153
}
151154

152155
void Board::set_castled() {
153-
if(side == White)
156+
if(side == Black)
154157
white_can_kingside_castle = false,
155158
white_can_queenside_castle = false;
156159
else
@@ -169,12 +172,12 @@
169172
std::vector<int16_t> Board::get_all_move_pos(int16_t position) {
170173
const int16_t absolute_piece_value = abs(board[position]);
171174
switch (absolute_piece_value) {
172-
case King: return king_move(board, position);
173-
case Queen: return queen_move(board, position);
174-
case Rook: return rook_move(board, position);
175+
case Pawn: return pawn_move(board, position);
175176
case Knight: return knight_move(board, position);
176177
case Bishop: return bishop_move(board, position);
177-
case Pawn: return pawn_move(board, position);
178+
case Rook: return rook_move(board, position);
179+
case Queen: return queen_move(board, position);
180+
case King: return king_move(board, position);
178181
default:
179182
mes_error("There are no pieces in this position.");
180183
}
@@ -184,14 +187,14 @@
184187
auto& Board::get_evaluate_board(int16_t position) {
185188
auto absolute_piece_value = abs(board[position]);
186189
switch (absolute_piece_value) {
187-
case King: return Evaluate::king;
188-
case Queen: return Evaluate::queen;
189-
case Rook: return Evaluate::rook;
190-
case Bishop: return Evaluate::bishop;
191-
case Knight: return Evaluate::knight;
192-
case Pawn: return Evaluate::pawn;
193-
default:
194-
return Evaluate::empty;
190+
case Pawn: return Evaluate::pawn;
191+
case Knight: return Evaluate::knight;
192+
case Bishop: return Evaluate::bishop;
193+
case Rook: return Evaluate::rook;
194+
case Queen: return Evaluate::queen;
195+
case King: return Evaluate::king;
196+
default:
197+
return Evaluate::empty;
195198
}
196199
}
197200

@@ -206,7 +209,7 @@
206209
hash_value ^= piece_values[side][get_piece_index(board[from])][from];
207210
hash_value ^= piece_values[side][get_piece_index(board[from])][to];
208211

209-
if (!is_empty(to)) {
212+
if (board[to] != 0) {
210213
evaluate -= board[to] + base * get_piece_score(evaluate_board_to_pos, to);
211214
hash_value ^= piece_values[!side][get_piece_index(board[to])][to];
212215
}
@@ -237,18 +240,17 @@
237240
/**
238241
* @brief Changes the position of a piece on the board.
239242
*/
240-
void Board::change_piece_position(int16_t from, int16_t to) {
243+
void Board::change_piece_position(int16_t from, int16_t to) {
241244
score += evaluate_score(from, to);
242245

243246
// castle check king or rook move, if move, set castle info is false
244247
change_castle_info(from);
245248

246-
if (can_promotion(from, to)) {
247-
auto base = side == White ? 1 : -1;
249+
if (can_promotion(from, to)) {
250+
int16_t base = side == White ? 1 : -1;
248251
score += base * get_piece_score(Evaluate::queen, to);
249252
score += base * (Queen - Pawn);
250253
board[to] = base * Queen;
251-
is_promotion = true;
252254
}
253255
else {
254256
board[to] = board[from];
@@ -263,6 +265,14 @@
263265
for (int i = 0; i < command.size(); i += 2) {
264266
auto from = command[i];
265267
auto to = command[i + 1];
268+
if(from == -1) {
269+
kingside_castle(); set_castled();
270+
continue;
271+
}
272+
if(from == -2) {
273+
queenside_castle(); set_castled();
274+
continue;
275+
}
266276

267277
if (!is_valid_move(from)) mes_error("Invalid move");
268278

0 commit comments

Comments
 (0)