- Create a chessboard using
let mut board = Board::new();
- Generate legal moves using
let moves = board.generate_legal_moves();
.- This returns a
Vec<Move>
of all moves available to be played from the current position. Move
is a struct consisting of the membersfrom: Position
,to: Position
andpromotion: Option<PieceType>
.from
is the position of the piece that will be moved andto
is the square that the piece will be moved to.promotion
needs to be set toSome(PieceType)
when a pawn is moving to the last rank. Promoting to a pawn will result in an error.PieceType
is an enum containing all possible chess roles, e.g.Pawn
,King
, and so on...
Position
is a struct consisting of the membersrow: i8
andcol: i8
. Both are in the range of 0 to 7 inclusive.row = 0
corresponds to row 8 on a chessboard androw = 7
corresponds to row 1.col = 0
corresponds to column A on a chessboard andcol = 7
corresponds to column H.
- This returns a
- Check if a move is legal by calling
board.is_legal(mv)
wheremv
is of typeMove
. - Get the current color to play by calling
board.whose_turn()
. This will return a value of typeColor
set to the color of the player that is to play the next move. - Use
board.get_board()
to get a copy of the current board state. This returns a[[SquareType; 8]; 8]
.SquareType
is a typedef forOption<Piece>
,Piece
is a struct containing the memberspiece_type: PieceType
andcolor: Color
.PieceType
is described above.Color
is an enum consisting ofWhite
orBlack
.- The board uses row for its first index and column for its second index, i.e.
board[row][column]
. - If the cell is
None
then the square is not occupied, and accordinglySome(Piece)
means that it is occupied by a piece with the attributes ofPiece
.
- Make move by calling
board.make_move(mv)
wheremv
is of typeMove
.- This will execute the move and return
Ok(())
if the move is legal and refuse to execute the move and returnErr(BoardError::IllegalMove)
otherwise. - Castling is an ordinary kings move. Set
from
to its current position andto
to its position after castling.
- This will execute the move and return
- Use
board.is_stalemate()
to check for stalemate andboard.is_checkmate()
to check for checkmate.- If
board.is_checkmate()
returns true then the player that made the last move (the checkmate) won.
- If
- Finally reset the board by calling
board = Board::new();