Struct chess::Board

source ·
pub struct Board { /* private fields */ }
Expand description

A representation of a chess board that implement FEN notation (Board::from_str).

Examples

use chess::{Square, Board, Color, Piece, ChessMove};

let mut board = Board::default();
// 8 | r n b q k b n r
// 7 | p p p p p p p p
// 6 | . . . . . . . .
// 5 | . . . . . . . .
// 4 | . . . . . . . .
// 3 | . . . . . . . .
// 2 | P P P P P P P P
// 1 | R N B Q K B N R
//   +----------------
//     A B C D E F G H

assert_eq!(board.on(Square::E8), Some((Piece::King, Color::Black)));

// White move the pawn from E2 to E4
let m = ChessMove::new(Square::E2, Square::E4);
board.update(m);
// 8 | r n b q k b n r
// 7 | p p p p p p p p
// 6 | . . . . . . . .
// 5 | . . . . . . . .
// 4 | . . . . P . . .
// 3 | . . . . . . . .
// 2 | P P P P . P P P
// 1 | R N B Q K B N R
//   +----------------
//     A B C D E F G H

assert_eq!(board.on(Square::E4), Some((Piece::Pawn, Color::White)));
assert_eq!(board.side_to_move(), Color::Black);

Implementations

Create a new empty board.

Consider using the Default trait to initialize the board.

Get the Color of the player who has to play.

Get the CastleRights for a given side.

Get the Square (if exist) of the En Passant.

Get the halfmoves number.

Get the fullmoves number.

Get the GameState of the Board.

Check if the ChessMove is valid. Legality is not verified.

Check if the ChessMove is legal.

Update the chessboard according to the chess rules.

Assume that the ChessMove is legal.

Examples
use chess::{Board, ChessMove, Square};

let mut board = Board::default();
let m = ChessMove::new(Square::E2, Square::E4);

board.update(m);

Remove CastleRights for a particular side.

Examples
use chess::{Board, CastleRights, Color};
let mut board = Board::default();

assert_eq!(board.castle_rights(Color::White), CastleRights::Both);
assert_eq!(board.castle_rights(Color::Black), CastleRights::Both);

board.remove_castle_rights(Color::White, CastleRights::QueenSide);
board.remove_castle_rights(Color::Black, CastleRights::Both);

assert_eq!(board.castle_rights(Color::White), CastleRights::KingSide);
assert_eq!(board.castle_rights(Color::Black), CastleRights::NoRights);

Get the Piece at a given Square.

Verify if the Square is occupied by the given Piece.

Get the Color at a given Square.

Verify if the Square is occupied by the given Color.

Get the Color at a given Square.

Verify if the Square is occupied by the given Piece and Color.

Verify if the given Square is pinned for the current side.

This implementation returns true only if the Piece has more than one valid move, if not or if the Square is empty, then it returns false.

Get the piece pinned for the current side.

Get the Square of the Piece::King of the given Color.

Verify if the Square is empty (i.e. not occupied).

Verify if the Square is occupied.

Verify if the Piece::King is in check.

Verify if a Square can be taken by the given Color in the current Board.

Reciprocal: see Board::is_not_targeted.

Verify if a Square cannot be taken by the given Color in the current Board.

Reciprocal: see Board::is_targeted.

Verify if the Piece on the Square has one or more valid moves.

If no Piece exist on the Square, then return false.

Note: The legality is not verify, if you want to: use has_legal_move.

Verify if the Piece on the Square has one or more legal moves.

If no Piece exist on the Square, then return false.

Verify if the player has one or more legal moves in all the Board.

If no Piece exist on the Square, then return false.

Compute and return all the valid moves for a Piece (if exist) at a given Square.

If no Piece exist on the Square, then return an empty Vec.

Note: The legality is not verify, if you want to: use get_legal_moves.

Compute and return all the legal moves for a Piece (if exist) at a given Square.

If no Piece exist on the Square, then return an empty Vec.

Construct a Vec of Square from a Square (exclusive) to the first Piece (inclusive) with a given direction.

Examples
use chess::{Board, ChessMove, Direction, Square};

let mut board = Board::default();
board.update(ChessMove::new(Square::D2, Square::D3));
board.update(ChessMove::new(Square::G7, Square::G5));
// 8 | r n b q k b n r
// 7 | p p p p p p . p
// 6 | . . . . . . . .
// 5 | . . . . . . p .
// 4 | . . . . . . . .
// 3 | . . . P . . . .
// 2 | P P P . P P P P
// 1 | R N B Q K B N R
//   +----------------
//     A B C D E F G H

assert_eq!(
    board.get_line(Square::C1, Direction::UpRight),
    vec![Square::D2, Square::E3, Square::F4, Square::G5]
)

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Default board is his initial state at the beginning of a chess game.

8 | r n b q k b n r
7 | p p p p p p p p
6 | . . . . . . . .
5 | . . . . . . . .
4 | . . . . . . . .
3 | . . . . . . . .
2 | P P P P P P P P
1 | R N B Q K B N R
  +----------------
    A B C D E F G H
Formats the value using the given formatter. Read more

From Forsyth-Edwards Notation (FEN).

https://www.chess.com/terms/fen-chess

The associated error which can be returned from parsing.
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.