Echecs.Board (Echecs v0.1.5)

Copy Markdown View Source

Chess board as a 15-element tuple of 64-bit integer bitboards.

Layout

Elements 0..5 hold the white pawns, knights, bishops, rooks, queens and king; 6..11 the black pieces in the same order; 12, 13 and 14 cache the white, black and combined occupancies.

Square 0 is a8 and square 63 is h1 (1 <<< ((7 - rank) * 8 + file)), so white pawns advance toward lower indices. Use at_tuple/2 for piece lookup and make_move_on_board_tuple/3 for the allocation-free move application used by the move generator's fast path.

Summary

Functions

Fast piece lookup on a tuple board. Returns {color, type} or nil.

Returns true if the square sq is attacked by attacker_color. Accepts both tuple and struct boards.

Applies a move to the bitboards only. Used for fast legality checking. Returns the updated board tuple. Single-write: destructures once, constructs one new tuple.

Types

board_tuple()

@type board_tuple() ::
  {integer(), integer(), integer(), integer(), integer(), integer(), integer(),
   integer(), integer(), integer(), integer(), integer(), integer(), integer(),
   integer()}

piece()

@type piece() :: {atom(), atom()} | nil

square()

@type square() :: 0..63

t()

@type t() :: %Echecs.Board{
  all_pieces: term(),
  black_bishops: term(),
  black_king: term(),
  black_knights: term(),
  black_pawns: term(),
  black_pieces: term(),
  black_queens: term(),
  black_rooks: term(),
  white_bishops: term(),
  white_king: term(),
  white_knights: term(),
  white_pawns: term(),
  white_pieces: term(),
  white_queens: term(),
  white_rooks: term()
}

Functions

all_occ(board)

at(board, index)

@spec at(t() | board_tuple(), square()) :: piece()

at_tuple(board, index)

Fast piece lookup on a tuple board. Returns {color, type} or nil.

attacked?(board, sq, attacker_color)

Returns true if the square sq is attacked by attacker_color. Accepts both tuple and struct boards.

bb(board)

bk(board)

black_occ(board)

bn(board)

bp(board)

bq(board)

br(board)

from_struct(struct)

from_tuple(mailbox)

make_move_on_bitboards(board, move, turn)

make_move_on_board_tuple(board, move, turn)

Applies a move to the bitboards only. Used for fast legality checking. Returns the updated board tuple. Single-write: destructures once, constructs one new tuple.

move_bitboards(board, from, to, piece_type, piece_color, capture_type, promotion_type)

new()

new_tuple()

put(board, index, piece)

@spec put(t(), square(), piece()) :: t()

to_algebraic(index)

to_index(sq_str)

to_struct(tuple)

wb(board)

white_occ(board)

wk(board)

wn(board)

wp(board)

wq(board)

wr(board)