#club #piece #hexchess #bedard-hexchess

hexchess

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

12 releases (6 breaking)

new 2.0.0-beta.3 Apr 12, 2025
0.10.0 Sep 17, 2024

#182 in Game dev

Download history 36/week @ 2025-02-08 23/week @ 2025-02-15 154/week @ 2025-04-05

154 downloads per month

MIT license

300KB
5.5K SLoC

Rust 4.5K SLoC // 0.0% comments TypeScript 774 SLoC // 0.0% comments JavaScript 232 SLoC // 0.0% comments

@bedard/hexchess

Build Coverage NPM Crates.io License

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

Installation

Install this package via NPM.

Depending on your bundler, you may need plugins for Web Assembly and top-level await.

npm install @bedard/hexchess

pnpm install @bedard/hexchess

yarn add @bedard/hexchess

Basic usage

The Hexchess class is a deserialized version of Forsyth–Edwards Notation. It contains the board state, current turn, en passant, and move numbers. Since castling is not a part of hexagonal chess, that section is omitted. The board data is stored as an array of Piece | null, sorted in FEN-order.

To create a game at the starting position, use Hexchess.init.

import { Hexchess } from '@bedard/hexchess'

const hexchess = Hexchess.init()

Hexchess instances have the following shape. The board represents an array of position values, sorted in FEN-order.

{
  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 for interacting with the game. A pair of constants named initialPosition and positions are available as well.

apply

Apply a whitespace separated sequence of moves, or a single San object.

const hexchess = Hexchess.init()

hexchess.apply('g4g5 e7e6 f5f6 e6f6')

hexchess.apply(San.parse('g5f6'))

hexchess.toString() // 'b/qbk/n1b1n/r5r/ppppppppp/11/5PP4/4P6/3P1B1P3/2P2B2P2/1PRNQBKNRP1 b - 0 1'

currentMoves

Get all current legal moves.

const hexchess = new Hexchess('1/3/5/7/9/11/5P5/11/11/11/11 w - 0 1')

const moves = hexchess.currentMoves()

moves.map(String) // ['f5f6, 'f5f7', ...]

movesFrom

Get all legal moves from a position.

const hexchess = Hexchess.init()

const moves = hexchess.movesFrom('f6')

moves.map(String) // ['f6f7']

movesFromUnsafe

Get all moves from a position, including ones that result in self-check.

const hexchess = Hexchess.parse('1/3/5/7/4r4/5K5/11/11/11/11/11 w - 0 1')

const moves = hexchess.movesUnsafe()

moves.map(String) // ['f6f7', 'f6g7' ...]

toString

Serialize a Hexchess instance to string.

const hexchess = Hexchess.init()

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

Function API

The raw WASM bindings are available for a more functional API. These work with objects matching the Hexchess and San structs. The API of these functions matches the Rust library API, see here for more info →

  • applyMove
  • applySequence
  • createHexchess
  • currentMoves
  • initHexchess
  • movesFrom
  • movesFromUnsafe
  • parseHexchess
  • parseSan
  • stringifyHexchess
  • stringifySan

License

MIT

Copyright (c) 2024-present, Scott Bedard

Dependencies

~2–2.7MB
~46K SLoC