Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
joakim committed Oct 25, 2022
1 parent d1dcfb4 commit 289126d
Showing 1 changed file with 7 additions and 35 deletions.
42 changes: 7 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Fischer960

A Fischer Random Chess / Chess960 library for JS based on algorithms. No large lookup tables needed.
A Fischer Random Chess / Chess960 JavaScript library based on algorithms.

```js
let sp = fischer.random()
Expand All @@ -11,13 +11,12 @@ sp.arrangement // -> ['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']

## Install

With Yarn: `yarn add fischer960`
With npm: `npm install fischer960`
npm: `npm i fischer960`
yarn: `yarn add fischer960`
pnpm: `npm add fischer960`

## Use

fischer960 is available as CJS for Node, ESM for bundlers and UMD for legacy environments. A bundler (Webpack/Rollup/etc) is recommended for use in browsers.

One can import only the functions to be used:

```js
Expand All @@ -37,24 +36,21 @@ A few things to be aware of:
- `random()` and `decode()` return the arrangement as an array (to convert the array to a string, see `toString()`)
- All `arrangement` arguments can take either an array (`['B', 'B', 'Q', 'N', 'N', 'R', 'K', 'R']`) or a string (`'BBQNNRKR'`)


### Main functions

#### `random(strong)`
#### `random()`

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

If the optional `strong` argument is set to `true`, it will use a cryptographically strong pseudo-random number generator that is slower, but with higher entropy. Defaults to `false`.

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

#### `randomID(strong)`
#### `randomID()`

Picks a random starting position's ID.

If the optional `strong` argument is set to `true`, it will use a cryptographically strong pseudo-random number generator that is slower, but with higher entropy. Defaults to `false`.

```js
randomID() // -> eg. 518
```
Expand Down Expand Up @@ -153,27 +149,3 @@ Note: `960` is not a valid ID, as this library uses zero-based IDs.
isValidID(0) // -> true
isValidID(960) // -> false
```

## Benchmark

Performance doesn't matter if you only need one starting position at a time, but it doesn't hurt to be fast 🚀

Run `yarn benchmark` or `npm run benchmark` to compare the three ways of returning a random starting position. Here's my results:

```
random() x 3,360,058 ops/sec ±0.51% (95 runs sampled)
random(true) x 421,398 ops/sec ±0.44% (94 runs sampled)
generate() x 217,324 ops/sec ±0.51% (89 runs sampled)
```

- `random()` is by far the fastest of these
- `random(true)` has better entropy, at the cost of being ~8 times slower
- `generate()` (now deprecated) was ~15 times slower than `random()`

If, for some reason, performance is essential, using a lookup table will be ~5 times faster than `random()`:

```
lookup() x 18,632,990 ops/sec ±0.41% (93 runs sampled)
```

However, that comes at the cost of ~14 KB extra file size. If that's OK, see `src/lookup.js`.

0 comments on commit 289126d

Please sign in to comment.