Kokopu is a JavaScript/TypeScript library for chess applications. It implements the chess game rules, and provides tools to read/write the standard chess file formats (PGN, FEN, UCI, etc.).
https://www.npmjs.com/package/kokopu
kokopu.js
or file kokopu.min.js
in your HTML page.npm install kokopu
Version 3.0.0 introduces some breaking changes with regard to the previous versions. Please look at the migration guide to determine whether your codebase needs to be adapted or not when upgrading Kokopu from 1.x or 2.x to 3.0.0 (or any subsequent version).
const { Position } = require('kokopu');
// Create a new position, play some moves...
const position = new Position();
position.play('e4');
position.play('e5');
position.play('Nf3');
// Display an ASCII-art representation of the position.
console.log(position.ascii());
// +---+---+---+---+---+---+---+---+
// | r | n | b | q | k | b | n | r |
// +---+---+---+---+---+---+---+---+
// | p | p | p | p | | p | p | p |
// +---+---+---+---+---+---+---+---+
// | | | | | | | | |
// +---+---+---+---+---+---+---+---+
// | | | | | p | | | |
// +---+---+---+---+---+---+---+---+
// | | | | | P | | | |
// +---+---+---+---+---+---+---+---+
// | | | | | | N | | |
// +---+---+---+---+---+---+---+---+
// | P | P | P | P | | P | P | P |
// +---+---+---+---+---+---+---+---+
// | R | N | B | Q | K | B | | R |
// +---+---+---+---+---+---+---+---+
// b KQkq -
// List the available moves.
const moves = position.moves();
console.log(moves.map(move => position.notation(move)));
// [ 'a6', 'a5', 'b6', 'b5', 'c6', 'c5', 'd6','d5', 'f6', 'f5', 'g6',
// 'g5', 'h6', 'h5', 'Na6', 'Nc6', 'Qe7', 'Qf6', 'Qg5', 'Qh4', 'Ke7',
// 'Be7', 'Bd6', 'Bc5', 'Bb4', 'Ba3', 'Nf6', 'Nh6', 'Ne7' ]
Or within a browser:
<script src="kokopu.js"></script>
<script>
const position = new kokopu.Position();
position.play('e4');
position.play('e5');
// etc...
</script>
Generated using TypeDoc