make_move method

void make_move(
  1. Move move
)

Implementation

void make_move(Move move) {
  final us = turn;
  final them = swap_color(us);
  push(move);

  board[move.to] = board[move.from];
  board[move.from] = null;

  /* if ep capture, remove the captured pawn */
  if ((move.flags & BITS_EP_CAPTURE) != 0) {
    if (turn == BLACK) {
      board[move.to - 16] = null;
    } else {
      board[move.to + 16] = null;
    }
  }

  /* if pawn promotion, replace with new piece */
  if ((move.flags & BITS_PROMOTION) != 0) {
    board[move.to] = Piece(move.promotion!, us);
  }

  /* if we moved the king */
  if (board[move.to]!.type == KING) {
    kings[board[move.to]!.color] = move.to;

    /* if we castled, move the rook next to the king */
    if ((move.flags & BITS_KSIDE_CASTLE) != 0) {
      final castling_to = move.to - 1;
      final castling_from = move.to + 1;
      board[castling_to] = board[castling_from];
      board[castling_from] = null;
    } else if ((move.flags & BITS_QSIDE_CASTLE) != 0) {
      final castling_to = move.to + 1;
      final castling_from = move.to - 2;
      board[castling_to] = board[castling_from];
      board[castling_from] = null;
    }

    /* turn off castling */
    castling[us] = 0;
  }

  /* turn off castling if we move a rook */
  if (castling[us] != 0) {
    for (var i = 0,
        len = ROOKS[us]!.length; i < len; i++) {
      if (move.from == ROOKS[us]![i]['square'] && ((castling[us] & ROOKS[us]![i]['flag']) != 0)) {
        castling[us] ^= ROOKS[us]![i]['flag'];
        break;
      }
    }
  }

  /* turn off castling if we capture a rook */
  if (castling[them] != 0) {
    for (var i = 0,
        len = ROOKS[them]!.length; i < len; i++) {
      if (move.to == ROOKS[them]![i]['square'] && ((castling[them] & ROOKS[them]![i]['flag']) != 0)) {
        castling[them] ^= ROOKS[them]![i]['flag'];
        break;
      }
    }
  }

  /* if big pawn move, update the en passant square */
  if ((move.flags & BITS_BIG_PAWN) != 0) {
    if (turn == BLACK) {
      ep_square = move.to - 16;
    } else {
      ep_square = move.to + 16;
    }
  } else {
    ep_square = EMPTY;
  }

  /* reset the 50 move counter if a pawn is moved or a piece is captured */
  if (move.piece == PAWN) {
    half_moves = 0;
  } else if ((move.flags & (BITS_CAPTURE | BITS_EP_CAPTURE)) != 0) {
    half_moves = 0;
  } else {
    half_moves++;
  }

  if (turn == BLACK) {
    move_number++;
  }
  turn = swap_color(turn);
}