|
84 | 84 | return board[idx];
|
85 | 85 | }
|
86 | 86 |
|
87 |
| - bool is_promotion = false; |
88 | 87 | 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; |
92 | 92 | }
|
93 | 93 |
|
94 | 94 | bool Board::is_valid_move(int16_t position){
|
|
125 | 125 | cnt_move += 1;
|
126 | 126 | side ^= 1;
|
127 | 127 | }
|
| 128 | + |
128 | 129 | void Board::kingside_castle(){
|
129 | 130 | auto king_position = side == White ? 60 : 4;
|
130 | 131 | auto rook_position = side == White ? 63 : 7;
|
|
135 | 136 | board[rook_position] = Empty;
|
136 | 137 | score += side == White ? 40 : -40;
|
137 | 138 | Board::update_move_info();
|
| 139 | + hash_value ^= castling_values[side][Kingside]; |
138 | 140 | }
|
139 | 141 |
|
140 | 142 | void Board::queenside_castle(){
|
|
147 | 149 | board[rook_position] = Empty;
|
148 | 150 | score += side == White ? 40 : -40;
|
149 | 151 | Board::update_move_info();
|
| 152 | + hash_value ^= castling_values[side][Queenside]; |
150 | 153 | }
|
151 | 154 |
|
152 | 155 | void Board::set_castled() {
|
153 |
| - if(side == White) |
| 156 | + if(side == Black) |
154 | 157 | white_can_kingside_castle = false,
|
155 | 158 | white_can_queenside_castle = false;
|
156 | 159 | else
|
|
169 | 172 | std::vector<int16_t> Board::get_all_move_pos(int16_t position) {
|
170 | 173 | const int16_t absolute_piece_value = abs(board[position]);
|
171 | 174 | 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); |
175 | 176 | case Knight: return knight_move(board, position);
|
176 | 177 | 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); |
178 | 181 | default:
|
179 | 182 | mes_error("There are no pieces in this position.");
|
180 | 183 | }
|
|
184 | 187 | auto& Board::get_evaluate_board(int16_t position) {
|
185 | 188 | auto absolute_piece_value = abs(board[position]);
|
186 | 189 | 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; |
195 | 198 | }
|
196 | 199 | }
|
197 | 200 |
|
|
206 | 209 | hash_value ^= piece_values[side][get_piece_index(board[from])][from];
|
207 | 210 | hash_value ^= piece_values[side][get_piece_index(board[from])][to];
|
208 | 211 |
|
209 |
| - if (!is_empty(to)) { |
| 212 | + if (board[to] != 0) { |
210 | 213 | evaluate -= board[to] + base * get_piece_score(evaluate_board_to_pos, to);
|
211 | 214 | hash_value ^= piece_values[!side][get_piece_index(board[to])][to];
|
212 | 215 | }
|
|
237 | 240 | /**
|
238 | 241 | * @brief Changes the position of a piece on the board.
|
239 | 242 | */
|
240 |
| - void Board::change_piece_position(int16_t from, int16_t to) { |
| 243 | + void Board::change_piece_position(int16_t from, int16_t to) { |
241 | 244 | score += evaluate_score(from, to);
|
242 | 245 |
|
243 | 246 | // castle check king or rook move, if move, set castle info is false
|
244 | 247 | change_castle_info(from);
|
245 | 248 |
|
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; |
248 | 251 | score += base * get_piece_score(Evaluate::queen, to);
|
249 | 252 | score += base * (Queen - Pawn);
|
250 | 253 | board[to] = base * Queen;
|
251 |
| - is_promotion = true; |
252 | 254 | }
|
253 | 255 | else {
|
254 | 256 | board[to] = board[from];
|
|
263 | 265 | for (int i = 0; i < command.size(); i += 2) {
|
264 | 266 | auto from = command[i];
|
265 | 267 | 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 | + } |
266 | 276 |
|
267 | 277 | if (!is_valid_move(from)) mes_error("Invalid move");
|
268 | 278 |
|
|
0 commit comments