Veggerby.Boards.Chess 0.1.0-prerelease0212

This is a prerelease version of Veggerby.Boards.Chess.
dotnet add package Veggerby.Boards.Chess --version 0.1.0-prerelease0212
                    
NuGet\Install-Package Veggerby.Boards.Chess -Version 0.1.0-prerelease0212
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Veggerby.Boards.Chess" Version="0.1.0-prerelease0212" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Veggerby.Boards.Chess" Version="0.1.0-prerelease0212" />
                    
Directory.Packages.props
<PackageReference Include="Veggerby.Boards.Chess" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Veggerby.Boards.Chess --version 0.1.0-prerelease0212
                    
#r "nuget: Veggerby.Boards.Chess, 0.1.0-prerelease0212"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Veggerby.Boards.Chess@0.1.0-prerelease0212
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Veggerby.Boards.Chess&version=0.1.0-prerelease0212&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Veggerby.Boards.Chess&version=0.1.0-prerelease0212&prerelease
                    
Install as a Cake Tool

Veggerby.Boards.Chess

Chess module for Veggerby.Boards providing complete move legality, special moves (castling, en passant, promotion), checkmate/stalemate detection, and SAN notation built atop the immutable deterministic core engine.

Depends on Veggerby.Boards. Use when you want a ready chess ruleset or a foundation for chess variants (house rules, timing layers, analysis tooling).

Install

dotnet add package Veggerby.Boards.Chess

Overview

This module provides a complete, deterministic implementation of chess with:

  • Standard 8×8 board with all piece types
  • Full move legality (pseudo-legal generation + king safety filtering)
  • Special moves: castling (kingside/queenside), en passant, pawn promotion
  • Check, checkmate, and stalemate detection
  • Standard Algebraic Notation (SAN) with all symbols (#, +, =Q, O-O, x, e.p.)
  • Immutable state history enabling analysis and replay

Quick Start

using Veggerby.Boards.Chess;
using Veggerby.Boards.Chess.MoveGeneration;

// Create a standard chess game
var builder = new ChessGameBuilder();
var progress = builder.Compile();

// Execute moves using SAN notation (simplest approach)
progress = progress.MoveSan("e4");   // White pawn to e4
progress = progress.MoveSan("e5");   // Black pawn to e5
progress = progress.MoveSan("Nf3");  // White knight to f3

// Or use low-level event handling
var piece = progress.Game.GetPiece("white-pawn-5");  // e-file pawn
var from = progress.Game.GetTile("e2");
var to = progress.Game.GetTile("e4");
var pathVisitor = new ResolveTilePathPatternVisitor(progress.Game.Board, from, to);
progress = progress.HandleEvent(new MovePieceGameEvent(piece, pathVisitor.ResultPath));

// Check game status
if (progress.IsGameOver())
{
    var outcome = progress.GetOutcome() as ChessOutcomeState;
    Console.WriteLine($"Game ended: {outcome.Result}");
}

// Check for check/checkmate using detector
var detector = new ChessEndgameDetector(progress.Game);
var status = detector.GetEndgameStatus(progress.State);
if (status == EndgameStatus.Checkmate)
{
    Console.WriteLine("Checkmate!");
}

Key Concepts

ChessStateExtras

The module uses ChessStateExtras to track:

  • CastlingRights: Which castling moves are still available (kingside/queenside for each player)
  • EnPassantTargetTileId: The target square for en passant capture (if available this turn)

These extras are immutably updated after each move based on piece movements and captures.

Events & Mutators

Event Mutator Description
MovePieceGameEvent ChessMovePieceStateMutator Moves a piece, updates extras, rotates players
CastlingGameEvent CastlingStateMutator Executes castling (moves both king and rook)
PromotionGameEvent PromotionStateMutator Promotes a pawn to queen/rook/bishop/knight
EnPassantCaptureGameEvent EnPassantCapturePieceStateMutator Captures pawn en passant

Movement Patterns

Chess pieces use these pattern types:

  • Directional Sliding: Rooks, bishops, queens (repeating in a direction until blocked)
  • Fixed Step: Kings, knights (fixed offset jumps)
  • Conditional: Pawns (forward move, diagonal capture, two-square initial, en passant)

Patterns are resolved to TilePath instances representing concrete move sequences.

Move Legality

Move validation follows a two-stage process:

  1. Pseudo-legal Generation: ChessPseudoLegalMoveGenerator produces all moves that follow piece movement patterns
  2. King Safety Filter: ChessLegalityFilter rejects moves that leave the king in check

This ensures only legal moves are allowed while maintaining performance.

SAN Notation

The ChessNomenclature class provides Standard Algebraic Notation:

  • Piece moves: Nf3, Qd4
  • Captures: Nxf3, Qxd4
  • Pawn moves: e4, exd5 (with file disambiguation on capture)
  • Castling: O-O (kingside), O-O-O (queenside)
  • Promotion: e8=Q
  • En passant: exd6 e.p.
  • Check: Nf3+
  • Checkmate: Qh5#
  • Disambiguation: Nbd2, R1a3 (when multiple pieces can move to same square)

Checkmate & Stalemate Detection

ChessEndgameDetector detects:

  • Check: King is under attack
  • Checkmate: King is in check with no legal moves
  • Stalemate: No legal moves but king is not in check

Phase-based endgame detection is also available via .WithEndGameDetection() (see Phase-Based Design docs).

Phases

The Chess module uses phase-based endgame detection by default:

AddGamePhase("play")
    .WithEndGameDetection()  // Automatically checks for checkmate/stalemate
    .Then()
        .All()
        .ForEvent<ChessMoveEvent>()
            .If<...>()  // Move validation
            .Then()
                .Do<ChessMovePieceStateMutator>()
                .Do<NextPlayerStateMutator>();

Legacy ChessEndgameDetector is retained for backward compatibility.

Testing

Run the module tests:

cd test/Veggerby.Boards.Tests
dotnet test --filter "FullyQualifiedName~Chess"

Test Coverage (16+ unit tests, 4 integration tests):

  • Pseudo-legal move generation (all piece types)
  • King safety filtering (check detection, pinned pieces)
  • Castling validation (safety checks, piece movement)
  • En passant capture mechanics
  • Pawn promotion variants
  • SAN notation generation and parsing
  • Checkmate and stalemate scenarios
  • Full game playability (Scholar's Mate, Immortal Game)

Known Limitations

  • Draw Rules: 50-move rule and threefold repetition are not enforced
  • Promotion Choice: API supports only default queen promotion; UI-driven selection requires extension
  • PGN Support: Import/export not implemented
  • Time Controls: No built-in time tracking

Extending This Module

Common extension scenarios:

Adding Chess960 (Fischer Random)

public class Chess960GameBuilder : ChessGameBuilder
{
    protected override void Build()
    {
        // Custom back-rank setup logic
        // ...
    }
}

Implementing Draw Detection

public sealed class FiftyMoveRuleCondition : IGameStateCondition
{
    public ConditionResponse Evaluate(Game game, GameState state)
    {
        // Track half-moves since last pawn move or capture
        // ...
    }
}

Adding Time Controls

public sealed record ChessTimeControlExtras(
    TimeSpan WhiteTime,
    TimeSpan BlackTime,
    TimeSpan Increment
);

// Add time tracking mutators

References

Versioning

Semantic versioning aligned with repository releases. Breaking movement / rule API changes bump MAJOR.

Contributing

Open issues & PRs at https://github.com/veggerby/Veggerby.Boards. Follow contributor guidelines.

License

MIT License. See root LICENSE.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0-prerelease0212 82 4/13/2026
0.1.0-prerelease0211 78 2/8/2026
0.1.0-prerelease0210 78 1/26/2026
0.1.0-prerelease0209 85 1/12/2026
0.1.0-prerelease0208 74 1/11/2026
0.1.0-prerelease0207 86 1/11/2026
0.1.0-prerelease0206 85 1/11/2026
0.1.0-prerelease0205 78 1/10/2026
0.1.0-prerelease0204 84 1/9/2026
0.1.0-prerelease0203 84 1/9/2026
0.1.0-prerelease0202 86 1/9/2026
0.1.0-prerelease0201 82 1/8/2026
0.1.0-prerelease0200 78 1/8/2026
0.1.0-prerelease0199 80 1/7/2026
0.1.0-prerelease0198 77 1/6/2026
0.1.0-prerelease0197 78 1/6/2026
0.1.0-prerelease0196 81 1/6/2026
0.1.0-prerelease0195 87 1/5/2026
0.1.0-prerelease0194 79 1/4/2026
0.1.0-prerelease0193 251 12/17/2025
Loading failed