promotePawn method

void promotePawn(
  1. Position position,
  2. PieceType type
)

Promotes PieceType.pawn to the specified type.

Implementation

void promotePawn(Position position, PieceType type) {
  ChessPiece? piece = getPiece(position);
  // Ensure the piece is a pawn on the final rank.
  if (piece == null || piece.type != PieceType.pawn) return;
  if ((piece.color == PieceColor.white && position.row != 0) ||
      (piece.color == PieceColor.black && position.row != 7)) {
    return;
  }

  // Promote the pawn.
  board[position.row][position.col] = ChessPiece(
    type: type,
    color: piece.color,
  );
}