hexchess
A library for Gliński's hexagonal chess, and the brain of hexchess.club.
Installation
Run the following Cargo command in your project directory:
cargo add hexchess
Or add hexchess as a dependency to your Cargo.toml file.
Basic usage
The Hexchess struct represents a deserialized version of Forsyth–Edwards Notation. Since castling is not a part of hexchess, that section is omitted. The following is a generalized version of the struct. The board is stored as an array of Option<Piece> values, sorted by FEN index.
{
board: [
'b', 'q', 'b', 'k', 'n', null, 'b', null, 'n', 'r',
null, null, null, null, null, 'r', 'p', 'p', 'p', 'p',
'p', 'p', 'p', 'p', 'p', null, null, null, null, null,
null, null, null, null, null, null, null, null, null, null,
null, 'P', null, null, null, null, null, null, null, null,
null, 'P', null, 'P', null, null, null, null, null, null,
null, 'P', null, 'B', null, 'P', null, null, null, null,
null, 'P', null, null, 'B', null, null, 'P', null, null,
null, 'P', 'R', 'N', 'Q', 'B', 'K', 'N', 'R', 'P',
null
],
turn: 'w',
ep: null,
halfmove: 0,
fullmove: 1
}
The following methods are available,
apply
Apply a whitespace separated sequence of moves.
let mut hexchess = init;
let _ = hexchess.apply;
hexchess.to_string // b/qbk/n1b1n/r5r/ppp1ppppp/5p5/6P4/4P6/3P1B1P3/2P2B2P2/1PRNQBKNRP1 w - 0 3
apply_move
Apply a single move by San.
let san = from.unwrap;
let mut hexchess = init;
let _ = hexchess.apply_move;
hexchess.to_string // b/qbk/n1b1n/r5r/ppppppppp/11/5PP4/4P6/3P1B1P3/2P2B2P2/1PRNQBKNRP1 b - 0 1
apply_move_unsafe
Apply a single by San, regardless of turn or legality.
let san = San::from("b1b6").unwrap();
let mut hexchess = Hexchess::init();
let _ = hexchess.apply_move_unsafe(&san)
hexchess.to_string() // b/qbk/n1b1n/r5r/ppppppppp/1P9/5P5/4P1P4/3P1B1P3/2P2B2P2/2RNQBKNRP1 b - 0 1
current_moves
Get all current legal moves.
let hexchess = init;
for san in hexchess.current_moves
find_king
Find FEN index for king belonging to Color.
let hexchess = init;
hexchess.find_king; // Some(3)
hexchess.find_king; // Some(86)
get
Get board value from position name.
let hexchess = init;
hexchess.get // Some(WhiteQueen)
get_color
Get all board indices occupied by Color pieces.
let hexchess = init;
hexchess.get_color // [0, 1, 2, ...]
is_check
Test if the board is in check.
let hexchess = init;
hexchess.is_check // false
is_checkmate
Test if the game is in checkmate.
let hexchess = init;
hexchess.is_checkmate // false
is_stalemate
Test if the game is in stalemate.
let hexchess = Hexchess::init();
hexchess.is_stalemate() // false
moves_from
Get all legal moves from a position.
let hexchess = init;
hexchess.moves_from // [San { from: 41, promotion: None, to: 30 }]
moves_from_unsafe
Get all moves from a position, including ones that result in self-check.
let hexchess = parse.unwrap;
hexchess.moves_from_unsafe // [San, San, San, ...]
to_string
Serialize Hexchess to string.
let hexchess = Hexchess::init();
hexchess.to_string() // b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1 w - 0 1
License
Copyright (c) 2024-present, Scott Bedard