Skip to content

A Rust / TypeScript library for Gliński's hexagonal chess, and the brain of hexchess.club

License

Notifications You must be signed in to change notification settings

scottbedard/hexchess.rs

Repository files navigation

Build Coverage NPM Crates.io License

A Rust / TypeScript library for Gliński's hexagonal chess, and the brain of hexchess.club

Basic usage

Execute hexchess to open the following command line interface.

Usage: hexchess <COMMAND>

Commands:
  apply        Apply sequence of moves to a position
  get-status   Get game status (w, b, stalemate, checkmate)
  get-targets  Get legal moves
  parse        Parse hexchess fen to JSON
  test-move    Test if a move is legal
  help         Print this message or the help of the given subcommand(s)

Options:
  -h, --help     Print help
  -V, --version  Print version

TypeScript

A collection of wasm bindings available via @bedard/hexchess, listed below are the available methods.

Note: Depending on the bundler, plugins may be required for Web Assembly and top-level await.

applyNotation

Create a new Hexchess object and apply a single Notation.

import { applyNotation } from '@bedard/hexchess'

applyNotation(hexchess, notation)

// { board: { ... }, enPassant, turn, fullmove, halfmove }

applySequence

Create a new Hexchess object and apply a whitespace-separated sequence of moves. An error is thrown if a piece of notation is not valid or a move is illegal.

import { applySequence } from '@bedard/hexchess'

applySequence(hexchess, 'g4g5 e7e6')

// { board: { ... }, enPassant, turn, fullmove, halfmove }

createHexchess

Create an empty Hexchess object.

import { createHexchess } from '@bedard/hexchess'

createHexchess()

// { board: { ... }, enPassant, turn, fullmove, halfmove }

createHexchessInitial

Create a Hexchess object with the initial game state.

import { createHexchessInitial } from '@bedard/hexchess'

createHexchessInitial()

// { board: { ... }, enPassant, turn, fullmove, halfmove }

findKing

Find a player's king.

import { findKing } from '@bedard/hexchess'

findKing(hexchess, 'b')

getColor

Get the color of a piece.

import { getColor } from '@bedard/hexchess'

getColor('p') // 'b'
getColor('P') // 'w'
getColor('?') // null

getPositionColor

Get color of a piece by board position. If no piece is present, null will be returned.

import { getPositionColor } from '@bedard/hexchess'

getPositionColor(hexchess, 'f5') // 'w'

getTargets

Find all legal moves from a position and return an array of Notation objects.

import { getTargets } from '@bedard/hexchess'

targets(hexchess, 'g4')

// [{ from, to, promotion }, ...]

isCheckmate

Test if the board is in checkmate.

import { isCheckmate } from '@bedard/hexchess'

isCheckmate(hexchess) // true / false

isThreatened

Test if a position is threatened.

import { isThreatened } from '@bedard/hexchess'

isThreatened(hexchess, 'g10') // false

parseHexchess

Create a Hexchess object from it's string representation. If hexchess is invalid, undefined will be returned.

import { parseHexchess } from '@bedard/hexchess'

parseHexchess('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1 w - 0 1')

// { board: { ... }, enPassant, turn, fullmove, halfmove }

parseNotation

Create a Notation object from it's string representation. If notation is invalid, undefined will be returned.

import { parseNotation } from '@bedard/hexchess'

parseNotation('e4e5')

// { from: 'e4', to: 'e5', promotion: null }

stringifyHexchess

Convert a Hexchess object to it's string representation.

import { stringifyHexchess } from '@bedard/hexchess'

stringifyHexchess(hexchess)

// 'b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1 w - 0 1'

License

MIT

Copyright (c) 2024-present, Scott Bedard

About

A Rust / TypeScript library for Gliński's hexagonal chess, and the brain of hexchess.club

Resources

License

Stars

Watchers

Forks

Packages

No packages published