stellar

Stellar - Chess engine written in C
Log | Files | Refs

moves_make.c (3249B)


      1 #include "board.h"
      2 #include "moves.h"
      3 #include "zobrist.h"
      4 
      5 // clang-format off
      6 const int castling_rights[64] = {
      7     13, 15, 15, 15, 12, 15, 15, 14,
      8     15, 15, 15, 15, 15, 15, 15, 15,
      9     15, 15, 15, 15, 15, 15, 15, 15,
     10     15, 15, 15, 15, 15, 15, 15, 15,
     11     15, 15, 15, 15, 15, 15, 15, 15,
     12     15, 15, 15, 15, 15, 15, 15, 15,
     13     15, 15, 15, 15, 15, 15, 15, 15,
     14     7,  15, 15, 15, 3,  15, 15, 11,
     15 };
     16 // clang-format on
     17 
     18 void _piece_remove(Board *self, Piece piece, Square square) {
     19     board_piece_pop(self, piece, square);
     20     self->hash ^= zobrist_key_piece(piece, square);
     21 }
     22 
     23 void _piece_set(Board *self, Piece piece, Square square) {
     24     board_piece_set(self, piece, square);
     25     self->hash ^= zobrist_key_piece(piece, square);
     26 }
     27 
     28 void _piece_move(Board *self, Piece piece, Square source, Square target) {
     29     _piece_remove(self, piece, source);
     30     _piece_set(self, piece, target);
     31 }
     32 
     33 int move_make(Move move, Board *board, int flag) {
     34     if (flag) {
     35         if (move_capture(move)) return move_make(move, board, 0);
     36         return 0;
     37     } else {
     38         Piece piece = move_piece(move);
     39         eColor color = board_side(board);
     40         Square source = move_source(move);
     41         Square target = move_target(move);
     42         Square ntarget = target + (color == WHITE ? -8 : +8);
     43 
     44         if (!move_capture(move)) {
     45             if (move_promote(move)) {
     46                 _piece_remove(board, piece, source);
     47                 _piece_set(board, move_piece_promote(move), target);
     48             } else {
     49                 _piece_move(board, piece, source, target);
     50             }
     51         } else {
     52             if (move_enpassant(move)) {
     53                 _piece_move(board, piece, source, target);
     54                 _piece_remove(board, move_piece_capture(move), ntarget);
     55             } else if (move_promote(move)) {
     56                 _piece_remove(board, piece, source);
     57                 _piece_remove(board, move_piece_capture(move), target);
     58                 _piece_set(board, move_piece_promote(move), target);
     59             } else {
     60                 _piece_remove(board, piece, source);
     61                 _piece_remove(board, move_piece_capture(move), target);
     62                 _piece_set(board, piece, target);
     63             }
     64         }
     65 
     66         board_enpassant_set(board, move_double(move) ? ntarget : no_sq);
     67 
     68         if (move_castle(move)) {
     69             Piece Rook = piece_get(ROOK, board_side(board));
     70             switch (target) {
     71             case g1:
     72                 _piece_move(board, Rook, h1, f1);
     73                 break;
     74             case c1:
     75                 _piece_move(board, Rook, a1, d1);
     76                 break;
     77             case g8:
     78                 _piece_move(board, Rook, h8, f8);
     79                 break;
     80             case c8:
     81                 _piece_move(board, Rook, a8, d8);
     82                 break;
     83             default:
     84                 break;
     85             }
     86         }
     87 
     88         board->hash ^= zobrist_key_castle(board_castle(board));
     89         board_castle_and(board, castling_rights[source]);
     90         board_castle_and(board, castling_rights[target]);
     91         board->hash ^= zobrist_key_castle(board_castle(board));
     92 
     93         if (!board_isCheck(board)) {
     94             board_side_switch(board);
     95             return 1;
     96         }
     97         return 0;
     98     }
     99 }