Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Fischer Random / Chess960 library for JavaScript

License

NotificationsYou must be signed in to change notification settings

joakim/fischer960

Repository files navigation

A Fischer Random Chess / Chess960 JavaScript library based on algorithms.

letsp=fischer.random()sp.id// -> 518sp.arrangement// -> ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']

Install

npm:npm i fischer960
yarn:yarn add fischer960
pnpm:pnpm add fischer960

There are no dependencies.

Use

One can import only the functions to be used:

import{random,toString,toUnicode}from'fischer960'

But importing the whole library will let you writefischer.random() 😎

import*asfischerfrom'fischer960'letsp=fischer.random()

A few things to be aware of:

  • IDs are zero-indexed (0-959, the standard starting position being 518)
  • random() anddecode() return the arrangement as an array (to convert the array to a string, seetoString())
  • Allarrangement arguments can take either an array (['B', 'B', 'Q', 'N', 'N', 'R', 'K', 'R']) or a string ('BBQNNRKR')

Main functions

random()

Generates a random starting position, returning its ID and arrangement of pieces.

random()// -> eg. { id: 518, arrangement: ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'] }

randomID()

Picks a random starting position's ID.

randomID()// -> eg. 518

decode(id)

Given an ID, returns the starting position's arrangement of pieces, orfalse if the ID is invalid.

decode(518)// -> eg. ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']

encode(arrangement)

Given an arrangement of pieces, returns the starting position's ID, or-1 if the arrangement is invalid.

encode('RKRNNQBB')// -> 959

Helper functions

A set of helper functions for manipulating arrangements are also provided.

Except fortoString() andtoArray(), these functions return the same type as was provided (if you pass a string you get a string, if you pass an array you get an array). Except fortoLowerCase() andtoUpperCase(), these also return the same case as was provided.

toString(arrangement)

Converts an arrangement of pieces fromArray toString.

toString(['B','B','Q','N','N','R','K','R'])// -> 'BBQNNRKR'

toArray(arrangement)

Converts an arrangement of pieces fromString toArray.

toArray('BBQNNRKR')// -> ['B', 'B', 'Q', 'N', 'N', 'R', 'K', 'R']

toLowerCase(arrangement)

Converts an arrangement of pieces to lowercase notation.

toLowerCase('BBQNNRKR')// -> 'bbqnnrkr'

toUpperCase(arrangement)

Converts an arrangement of pieces to uppercase notation.

toUpperCase('bbqnnrkr')// -> 'BBQNNRKR'

toMirror(arrangement)

Mirrors an arrangement of pieces (returns its “twin”).

toMirror('BBQNNRKR')// -> 'RKRNNQBB'

toUnicode(arrangement, color)

Converts an arrangement of pieces to Unicode symbols.

Thecolor argument is optional and defaults to white (falsy = white, truthy = black).

Note: There's no way to convert back to letters from Unicode symbols.

toUnicode('BBQNNRKR')// -> '♗♗♕♘♘♖♔♖'toUnicode('BBQNNRKR',true)// -> '♝♝♛♞♞♜♚♜'

isValidArrangement(arrangement)

Validates a starting position's arrangement of pieces.

isValidArrangement('BBQNNRKR')// -> trueisValidArrangement('KQRBRBNN')// -> false (not a valid starting position)

isValidID(id)

Validates a starting position's ID.

Note:960 is not a valid ID, as this library uses zero-based IDs.

isValidID(0)// -> trueisValidID(960)// -> false

[8]ページ先頭

©2009-2025 Movatter.jp