@ayshrj/checkers.js
Version:
A TypeScript-based headless Checker game engine for simulating game logic, AI moves, and game state management.
136 lines (132 loc) • 4.18 kB
text/typescript
import { EventEmitter } from 'events';
/**
* Player colors in Checkers
*/
type Color = "red" | "black";
/**
* Type of checker piece
*/
type PieceType = "man" | "king";
/**
* Represents a game piece
*/
interface Piece {
color: Color;
type: PieceType;
}
/**
* Board position (row, column)
*/
interface Position {
row: number;
col: number;
}
/**
* Complete move definition including captures
*/
interface CheckersMove {
from: Position;
path: Position[];
captures: Position[];
}
/**
* Possible game states
*/
type GameState = "redTurn" | "blackTurn" | "gameOver";
/**
* Complete game state snapshot
*/
interface CheckersGameState {
board: (Piece | null)[][];
turn: Color;
allowedMoves: CheckersMove[];
gameState: GameState;
boardStatus: string;
isGameOver: boolean;
}
/**
* Main Checkers game class with event emitting
*/
declare class Checkers extends EventEmitter {
private _board;
private _turn;
constructor();
/**
* Reset game to initial state
*/
reset(): void;
/**
* Returns the piece at the given position (or null).
*/
getPiece(pos: Position): Piece | null;
/**
* Get current game state
*/
getCurrentState(): CheckersGameState;
/**
* Execute a move if valid
*/
move(move: CheckersMove): boolean;
/**
* Generate best move using minimax algorithm
*/
bestMove(depth: number): CheckersMove | null;
private _emitStateChange;
private _promoteIfNeeded;
private validateMove;
/**
* Generate all allowed moves for the player whose turn is `turn` on the given board.
* If any capturing moves are available, they are mandatory.
*/
static getAllowedMovesForBoard(board: (Piece | null)[][], turn: Color): CheckersMove[];
/**
* Recursively generate all capturing (jump) moves for a piece starting from `pos` on the given board.
*/
static getCapturesForPiece(board: (Piece | null)[][], pos: Position, piece: Piece, currentMove?: CheckersMove): CheckersMove[];
/**
* Generate simple (non-capturing) moves for a piece at position `pos` on the given board.
*/
static getSimpleMovesForPiece(board: (Piece | null)[][], pos: Position, piece: Piece): CheckersMove[];
/**
* Deep-clone a board (8x8 array).
*/
static cloneBoard(board: (Piece | null)[][]): (Piece | null)[][];
/**
* Returns the allowed movement directions for a piece.
* For a man: red moves upward (row decreases) and black moves downward (row increases).
* Kings move in all four diagonal directions.
*/
static getDirections(piece: Piece): number[][];
/**
* Returns a new board state after applying the given move.
* This function does not modify the original board.
*/
static updateBoard(board: (Piece | null)[][], move: CheckersMove): (Piece | null)[][];
/**
* Evaluate the board state from the perspective of the given player.
* Positive values favor `perspective`, negative values favor the opponent.
*/
static evaluateBoard(board: (Piece | null)[][], perspective: Color): number;
/**
* Helper: Shuffle an array (Fisher–Yates shuffle).
*/
static shuffle<T>(array: T[]): T[];
/**
* Minimax algorithm (without alpha–beta pruning) to search for the best move.
* @param board The board state to evaluate.
* @param depth How many plies to search.
* @param isMax Whether we are maximizing or minimizing.
* @param turn Which color is to move on this board.
* @param perspective The original player for whom we are evaluating.
* @returns An object with a score and the best move found.
*/
static minimax(board: (Piece | null)[][], depth: number, isMax: boolean, turn: Color, perspective: Color): {
score: number;
move: CheckersMove | null;
};
/**
* Helper: Compare two moves for equality.
*/
static compareMoves(m1: CheckersMove, m2: CheckersMove): boolean;
}
export { Checkers, type CheckersGameState, type CheckersMove, type Color, type GameState, type Piece, type PieceType, type Position };