hexchess 2.0.0-alpha.3

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

@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
npm install @bedard/hexchess

# pnpm
pnpm install @bedard/hexchess

# yarn
yarn add @bedard/hexchess

Basic usage

The Hexchess object is a deserialized version of Forsyth–Edwards Notation. It contains the board state, current turn, en passant position, and move numbers. Note that since castling is not a part of hexagonal chess, that section is omitted from the notation.

import { initHexchess } from '@bedard/hexchess'

const hexchess = initHexchess()

The board positions coorelate to their position in a FEN string, null represents an unoccupied position.

{
  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
}

Below are the available functions to interact with these objects.

applyMove

Apply San object to a Hexchess object.

import { applyMove } from '@bedard/hexchess'

applyMove(hexchess, { from: 53, to: 42, promotion: null })  // { board: [ ... ], ep, turn, fullmove, halfmove }

applySequence

Apply a whitespace separated sequence of move to a Hexchess object.

import { applySequence } from '@bedard/hexchess'

applySequence(hexchess, 'g4g6 f7g6 f5f7 g6f6') // { board: [ ... ], ep, turn, fullmove, halfmove }

createHexchess

Create a blank Hexchess object.

import { createHexchess } from '@bedard/hexchess'

createHexchess() // { board: [ ... ], ep, turn, fullmove, halfmove }

currentMoves

Get current legal moves.

import { currentMoves } from '@bedard/hexchess'

currentMoves(hexchess) // [{ from, to, promotion }, ...]

initHexchess

Create Hexchess object at the initial position.

import { initHexchess } from '@bedard/hexchess'

initHexchess() // { board: [ ... ], ep, turn, fullmove, halfmove }

movesFrom

Get legal moves from a position index.

import { movesFrom } from '@bedard/hexchess'

movesFrom(53) // [{ from, to, promotion }, ...]

parseHexchess

Parse Hexchess object from Forsyth–Edwards Notation.

import { parseHexchess } from '@bedard/hexchess'

parseHexchess('b/qbk/n1b1n/r5r/ppppppppp/11/5P5/4P1P4/3P1B1P3/2P2B2P2/1PRNQBKNRP1 w - 0 1') // { board, turn, ep, halfmove, fullmove }

parseSan

Parse San object from string.

import { parseSan } from '@bedard/hexchess'

parseSan('g4g5') // { from: 53, to: 42, promotion: null }

stringifyHexchess

Convert Hexchess object to string using Forsyth–Edwards Notation.

import { stringifyHexchess } from '@bedard/hexchess'

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

stringifySan

Convert San object to string.

import { stringifySan } from '@bedard/hexchess'

stringifySan({ from: 53, to: 42, promotion: null }) // 'g4g5'

License

MIT

Copyright (c) 2024-present, Scott Bedard