rapid-draughts - GitHub
rapid-draughts is a super speedy, blazing fast, rocket-powered TypeScript draughts/checkers engine with move validation, AI and game history.
Table of Contents
1. Introduction
One of the key advantages is its use of bitboards, a board representation that holds the draughts board in three 32 or 64-bit unsigned integers. One for the light pieces, dark pieces, and the king pieces. Bitboards enable fast move generation and have minimal memory usage, contributing to the library’s exceptional performance.
In the following sections, we will explore its usage in detail, covering installation, usage, AI strategies, game history, and more.
2. Core
2.1. Setting up the engine
First, install the library using npm:
npm install rapid-draughts
Next we can import the EnglishDraughts
factory object into your project:
import {
EnglishDraughts as Draughts
} from 'rapid-draughts/english';
With this, we call the setup()
function to create a new draughts game.
const draughts = Draughts.setup();
In english draughts, the dark pieces move first. You can change this by passing in the player
property.
import {
DraughtsPlayer
} from 'rapid-draughts'
import {
EnglishDraughts as Draughts
} from 'rapid-draughts/english';
const draughts = Draughts.setup({ player: DraughtsPlayer.LIGHT });
2.2 Using the draughts engine
2.2.1. The board
By default, the draughts board is set up with the standard starting position. You can get the current board state by accessing the board property. The board is returned as a 1D array with the following type:
type Board = {
position?: number
dark: boolean
piece?: {
king: boolean
player: DraughtsPlayer
}
}[]
Only valid, dark squares will have a position
property, and only squares with a pieces will have a piece
property.
For convenience you can output an ASCII representation of the board with the asciiBoard
method:
console.log(draughts.asciiBoard());
2.2.2. Current player
To get the current player, access the player
property of the draughts
object. The player will be either DraughtsPlayer.LIGHT
or DraughtsPlayer.DARK
.
const { player } = draughts;
console.log(`Current player: ${player}`);
2.2.3. Current status
To get the current game status, access the status
property of the draughts
object. The status will be one of the following values: DraughtsStatus.PLAYING
, DraughtsStatus.DRAW
, DraughtsStatus.LIGHT_WINS
, or DraughtsStatus.DARK_WINS
.
const { status } = draughts;
console.log(`Current status: ${status}`);
2.2.4. Game history
The draughts
object maintains a history of moves played during the game, as well as all the resulting board positions. To access the game history, use the history
property.
const { moves, boards } = draughts.history;
2.2.3. Making a move
To make a move, first, get the list of legal moves available for the current position. You can access this list using the moves
property of the draughts
object.
const { moves } = draughts;
Each move is an object with the following type:
type EnglishDraughtsMove = {
origin: number
destination: number
captures: number[]
}
origin
refers to the square the piece is moving from, destination
refers to the square the piece is moving to, and captures
refers to the positions of the pieces that are being captured. To make a move, call the move()
method of the draughts
object, passing in one of the legal moves.
const selectedMove = draughts.moves[0];
draughts.move(selectedMove);
After making a move, the draughts object will update the board, the current player, and the game status. It will also add the move to the game history. Keep in mind that the move()
method does not validate if the move is legal. An error will be thrown if an illegal move is passed in.
3. AI
The library provides a set of AI strategies that can be used to play against or to simulate games. In this section, we’ll cover two strategies: Random and AlphaBeta.
3.1. Random Strategy
The random strategy is a simple AI that chooses a random legal move. To create a random AI player, use the ComputerFactory.random()
method.
import {
EnglishDraughtsComputerFactory as ComputerFactory
} from 'rapid-draughts/english';
const randomComputer = ComputerFactory.random();
To get a random move, pass the draughts
object to the randomComputer
function. Make the move as usual with the draughts.move()
function.
const randomMove = await randomComputer(draughts);
draughts.move(randomMove);
Note that the computer player functions are asynchronous, so you’ll need to use the await
keyword.
3.2. AlphaBeta Strategy
The AlphaBeta strategy is a more advanced AI that uses the alpha-beta pruning algorithm to search for the best move. You can configure the search depth by passing an object with the maxDepth
property to the ComputerFactory.alphaBeta()
method.
import {
EnglishDraughtsComputerFactory as ComputerFactory
} from 'rapid-draughts/english';
const alphaBetaComputer = ComputerFactory.alphaBeta({ maxDepth: 7 });
To get a move from the alpha-beta computer player, pass the draughts object as an argument. Make the move as usual with the draughts.move()
function.
const alphaBetaMove = await alphaBetaComputer(draughts);
draughts.move(alphaBetaMove);
Note that the computer player functions are asynchronous, so you’ll need to use the await
keyword.