Advanced Chess Kit
Documentation
Full-featured chess engine for Unity: supports multiple difficulty levels, game saving, move hints, and ready-made UI. Includes its own minimax search, opening book, and ability to save/load positions from FEN.
IChessGame
Interface for a single game. Manages board state, move generation, opponent reply computation, and hints.
string BoardFen { get; }Returns the position in FEN notation (piece placement only, e.g. "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR").
ChessGameHeader Header { get; }Returns metadata about the current position: number of plies, whose turn, difficulty, whether in check.
ChessOutcome Outcome { get; }Returns the result: InProgress, PlayerWon, PlayerLost, or Draw.
bool CanUndo { get; }Returns false when the game is finished (undo is impossible in terminal state).
IReadOnlyList<string> LegalMovesFrom(string square)Returns list of legal moves from the given square (e.g. "e2") in UCI format (e.g. "e2e4").
string LastMove { get; }Returns the most recently played move in UCI format, or null if no moves have been played.
string ExpectedPlayerReply { get; }Returns the expected player move in UCI format from the opponent's last move analysis, or null if unavailable.
bool IsPromotion(string move)Checks whether the move requires a piece choice for pawn promotion (4-character move without promotion piece).
bool TryPlayMove(string move)Applies the player's move; returns false if the move is illegal or opponent reply is already computing.
bool IsComputingReply { get; }Returns true while the opponent's reply is being computed.
void AdvanceReplyComputation(double sliceSeconds)Advances opponent reply computation by the specified time (seconds); called from game loop to distribute load.
void BeginHint(int count)Starts search for best moves; result will be in HintMoves when search completes.
bool IsComputingHint { get; }Returns true while computing hint moves.
void AdvanceHintComputation(double sliceSeconds)Advances hint search by the specified time (seconds).
IReadOnlyList<string> HintMoves { get; }Returns sorted list of recommended moves, empty until search completes.
bool TryUndo(int plies)Undoes the specified number of plies; returns false if game is finished or computation is in progress.
void Resign()Ends the game as a player loss (cancels any pending computation).
string Save()Returns serialized game state with full history; safe to call at any time.
IChessGameFactory
Creates new games and restores saved ones.
IChessGame NewGame(ChessDifficulty difficulty, bool playerPlaysWhite = true)Creates a new game; if player is Black, opponent starts computing the opening move.
IChessGame NewGameFromFen(ChessDifficulty difficulty, bool playerPlaysWhite, string fen)Creates a game from an arbitrary position via FEN (for debug/test levels).
bool TryLoad(string state, out IChessGame game)Restores a game from a saved string; returns false for corrupted data.
ChessDifficulty
Difficulty level: Easy, Normal, or Hard — determines time budget for opponent search.
string ToKey(this ChessDifficulty difficulty)Returns string key ("easy", "normal", "hard") for saving to config files.
bool TryParse(string key, out ChessDifficulty difficulty)Parses string back to enum; returns false if key is unrecognized.
ChessOutcome
Game result from the player's perspective: InProgress, PlayerWon, PlayerLost, Draw.
ChessGameHeader
Lightweight position info (without loading the full game).
int PliesPlayed { get; }Number of plies played so far.
bool IsPlayerToMove { get; }Returns true if it's the player's turn, false if opponent's.
ChessDifficulty Difficulty { get; }Current game difficulty level.
bool PlayerPlaysWhite { get; }Returns true if player is White, false if Black.
bool IsCheck { get; }Returns true if the side to move is in check.
ChessBoardHost
Unity component managing IChessGame and syncing it with UI and saves.
IChessGame Game { get; }Current game, or null if no game is active.
bool HasGame { get; }Returns true if Game != null.
IChessGameFactory Factory { get; set; }Can be overridden with a custom engine; defaults to MinimalChessGameFactory.
IChessGameStorage Storage { get; set; }Where to save/load games; defaults to PlayerPrefsChessGameStorage.
bool CanPlayerAct { get; }Returns true if player can make a move, undo, or resign (false during computation and animation).
void NewGame(ChessDifficulty difficulty, bool playerPlaysWhite = true)Creates a new game via the current Factory.
void NewGameFromFen(ChessDifficulty difficulty, bool playerPlaysWhite, string fen)Creates a game from an arbitrary position; fires GameEnded immediately if position is already terminal.
bool TryLoad(string state)Loads a game from a string; returns false for corrupted data.
string Save()Returns the serialized state of the current game.
void SaveToStorage()Writes Save() through Storage (defaults to PlayerPrefs).
bool TryLoadFromStorage()Reads from Storage and loads via TryLoad(); returns false if nothing was ever saved.
void FlushStorage()Forces Storage buffer to sync to disk (called on pause and quit).
bool IsPromotion(string move)Checks if move requires piece selection; called before opening promotion picker.
bool TryPlayMove(string move)Applies the player's move; returns false if move is illegal.
void BeginHint(int count)Starts search for top-N moves to suggest.
void AdvanceHintComputation(double sliceSeconds)Advances hint search; called from Update().
bool TryUndo(int plies)Undoes plies; returns false if computation is in progress.
void Resign()Resigns the game.
event Action<ChessGameHeader> HeaderChangedFires after every applied ply (player move or opponent reply).
event Action<ChessGameHeader> PliesUndoneFires when plies are undone (before HeaderChanged).
event Action<ChessOutcome> GameEndedFires once when the game reaches a terminal outcome.
event Action<bool> CanPlayerActChangedFires when CanPlayerAct changes.
void SetRevealInProgress(bool inProgress)Keeps CanPlayerAct false while move animation is playing (UI calls for sync).
ChessBoardView
Unity component representing the board in world space: piece rendering, click handling, move animation.
void ApplySkin(ChessBoardSkin skin)Applies a new skin (board, pieces, label colors); can be called to switch appearance.
void RedrawPieces()Redraws piece positions on the board (used after animation completes).
void SetRevealInProgress(bool inProgress)Communicates animation state to ChessBoardHost (CanPlayerAct stays false until complete).
IChessGameStorage
Interface for persistent game save storage.
void Write(string state)Writes game state to storage.
string Read()Reads the last saved state, or null if nothing has been saved.
void Flush()Syncs buffer to disk (may be no-op for unbuffered storage).
Typical Usage Loop
- Create ChessBoardHost on a GameObject in the Gameplay scene.
- Call
host.NewGame(difficulty, playerPlaysWhite)to start. - In Update() call
host.AdvanceReplyComputation(Time.deltaTime)to compute replies. - ChessBoardView reads board.Game and redraws pieces; on click sends move via
host.TryPlayMove(). - Listen to
host.HeaderChangedto update UI;host.GameEndedfor result screen. - Call
host.SaveToStorage()on pause/quit for auto-save.