- Notifications
You must be signed in to change notification settings - Fork0
Fischer Random / Chess960 library for JavaScript
License
joakim/fischer960
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
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']
npm:npm i fischer960
yarn:yarn add fischer960
pnpm:pnpm add fischer960
There are no dependencies.
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()
)- All
arrangement
arguments can take either an array (['B', 'B', 'Q', 'N', 'N', 'R', 'K', 'R']
) or a string ('BBQNNRKR'
)
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'] }
Picks a random starting position's ID.
randomID()// -> eg. 518
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']
Given an arrangement of pieces, returns the starting position's ID, or-1
if the arrangement is invalid.
encode('RKRNNQBB')// -> 959
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.
Converts an arrangement of pieces fromArray
toString
.
toString(['B','B','Q','N','N','R','K','R'])// -> 'BBQNNRKR'
Converts an arrangement of pieces fromString
toArray
.
toArray('BBQNNRKR')// -> ['B', 'B', 'Q', 'N', 'N', 'R', 'K', 'R']
Converts an arrangement of pieces to lowercase notation.
toLowerCase('BBQNNRKR')// -> 'bbqnnrkr'
Converts an arrangement of pieces to uppercase notation.
toUpperCase('bbqnnrkr')// -> 'BBQNNRKR'
Mirrors an arrangement of pieces (returns its “twin”).
toMirror('BBQNNRKR')// -> 'RKRNNQBB'
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)// -> '♝♝♛♞♞♜♚♜'
Validates a starting position's arrangement of pieces.
isValidArrangement('BBQNNRKR')// -> trueisValidArrangement('KQRBRBNN')// -> false (not a valid starting position)
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
About
Fischer Random / Chess960 library for JavaScript