Echecs.Game (Echecs v0.1.5)

Copy Markdown View Source

Complete state of a chess game: board tuple, side to move, castling rights (bit flags wk = 1, wq = 2, bk = 4, bq = 8), en-passant square, half/fullmove clocks, position history, Zobrist hash and cached king squares.

Build positions with new/1 (FEN), advance them with make_move/2 (struct moves) or advance_position/6 (packed moves on raw tuples, used by perft), and query terminal state with checkmate?/1, stalemate?/1 and draw?/1.

Summary

Functions

Returns true if the game is over by checkmate.

Returns true if the game is drawn.

Returns true if the given castling right is present.

Returns true if the current side to move is in check.

Executes a move on the game state.

Executes a packed move on the game state.

Executes a packed move using the canonical transition pipeline without extending history.

Returns true if the game is over by stalemate.

Verifies if a pseudo-legal move leaves the mover in check.

Returns true if a packed pseudo-legal move is legal.

Types

king_pos()

@type king_pos() :: {Echecs.Board.square() | nil, Echecs.Board.square() | nil}

t()

@type t() :: %Echecs.Game{
  board: Echecs.Board.board_tuple(),
  castling: non_neg_integer(),
  en_passant: Echecs.Board.square() | nil,
  fullmove: pos_integer(),
  halfmove: non_neg_integer(),
  history: [non_neg_integer()],
  king_pos: king_pos(),
  turn: Echecs.Piece.color(),
  zobrist_hash: non_neg_integer()
}

Functions

checkmate?(game)

@spec checkmate?(t()) :: boolean()

Returns true if the game is over by checkmate.

draw?(game)

@spec draw?(t()) :: boolean()

Returns true if the game is drawn.

has_right?(castling, atom1, atom2)

@spec has_right?(non_neg_integer(), Echecs.Piece.color(), :kingside | :queenside) ::
  boolean()

Returns true if the given castling right is present.

in_check?(game)

@spec in_check?(t()) :: boolean()

Returns true if the current side to move is in check.

make_move(game, move)

@spec make_move(t(), Echecs.Move.t()) :: t()

Executes a move on the game state.

make_move_int(game, packed_move)

@spec make_move_int(t(), integer()) :: t()

Executes a packed move on the game state.

make_move_perft(game, packed_move)

@spec make_move_perft(t(), integer()) :: t()

Executes a packed move using the canonical transition pipeline without extending history.

new(fen \\ "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1")

@spec new(String.t()) :: t()

Creates a new game from a FEN string.

stalemate?(game)

@spec stalemate?(t()) :: boolean()

Returns true if the game is over by stalemate.

verify_move(game, move)

@spec verify_move(t(), Echecs.Move.t()) :: boolean()

Verifies if a pseudo-legal move leaves the mover in check.

verify_move_int(game, packed_move)

@spec verify_move_int(t(), integer()) :: boolean()

Returns true if a packed pseudo-legal move is legal.