Gaming,Architecture
Extending a C# Chess Engine with King Safety and Material-Based AI
Extended a tutorial-based C# and WPF chess application with independently developed king-safety validation, castling rules, captured-piece tracking, and a computer-controlled opponent.
View GitHub RepositoryThe Problem
The original guided chess implementation established the board, pieces, movement generation, and WPF presentation, but it did not yet demonstrate several of the behaviors needed for a more complete playable game.
Moves needed to be validated against king safety, special movement rules needed careful state-aware handling, and a single-player mode required an opponent capable of choosing only legal moves without freezing the desktop interface.
The project also needed a clear separation between reusable game rules and presentation behavior so that future features could be introduced without concentrating all logic in the WPF window.
TL;DR
- Problem: The guided implementation needed stronger rule enforcement and a playable single-player experience
- Solution: Added board-copy simulation, king-safety filtering, castling validation, captured-piece tracking, and a material-scoring Black AI
- Impact: Produced a clearer demonstration of C# architecture, game-state reasoning, and desktop UI integration
Technical Leadership
- Kept chess rules in the ChessLogic project and presentation concerns in ChessUI rather than embedding engine behavior in event handlers.
- Used copied board states to validate candidate moves without mutating the active game.
- Designed the computer opponent to consume the same legal-move pipeline used by the human-facing game flow.
- Coordinated computer turns asynchronously so the WPF interface remains responsive.
What I Did
The enhancements focus on trustworthy state transitions: generate a candidate move, simulate it, validate king safety, and only then allow it into the playable move set.
The AI follows the same principle. It gathers legal Black moves from the game state, applies each candidate to a board copy, evaluates the resulting material balance, and selects among the highest-scoring options.
- Separated the solution into ChessLogic and ChessUI projects with a project reference from the WPF application to the reusable engine.
- Filtered pseudo-legal piece moves by executing each move on a copied board and rejecting positions that leave the moving player in check.
- Added attacked-square detection with pawn-specific attack handling.
- Implemented king-side and queen-side castling validation, including prior movement, clear-path, current-check, and through-check rules.
- Added captured-piece collections and a WPF display sorted by piece value.
- Created a material-based AI that evaluates every legal Black move and randomizes among equally scored choices.
- Added check and game-over feedback while preventing human input during the computer turn.
Testing and Validation
- Verified legal move generation through live board interaction and destination highlighting.
- Validated that moves exposing the active king are removed from the available move set.
- Exercised castling rules for moved pieces, blocked paths, current check, and attacked transit squares.
- Confirmed that the AI selects from the legal-move collection and executes automatically after a White move.
- Checked captured-piece tracking and redraw behavior after both human and computer captures.
Outcome
- Added a playable human-versus-computer workflow to the desktop application.
- Improved rule accuracy through king-safety and castling validation.
- Made custom contributions distinguishable from the tutorial foundation through repository documentation.
- Established a practical base for checkmate detection, promotion, en passant, deeper search, and automated rule tests.