ChessEngine

Chess Engine Explanation

Overview

This file documents the internal working of Chess/ChessEngine.py.

Important Definition: In the context of this project, “Engine” refers to a Game State Manager and Rules Validator. I call it an engine only because it sounds cool. It handles the logic of chess rules (valid moves, checkmate, castling) but does not currently contain an Artificial Intelligence (AI) to play against. It is effectively a “Rules Engine” designed to facilitate a 2-player game (PvP).

Logic & Architecture

1. Board Representation

The board is represented as a 8x8 2D List (Array of Arrays) in Python.

2. Move Generation Strategy

The engine uses a “Pseudo-Legal” to “Legal” move generation pipeline:

  1. Generate All Possible Moves: Iterate through every square on the board. If the piece belongs to the active player, generate all physically possible moves for that piece (ignoring checks).
    • Sliding Pieces (Rook, Bishop, Queen): Iteratively check along directions until blocked.
    • Stepping Pieces (Knight, King): Check fixed offsets.
    • Pawns: Complex logic including single/double steps, diagonal captures, and En Passant.
  2. Filter for Legality: For every generated “possible” move:
    • Simulate: Make the move on the board.
    • Verify: Check if the current player’s King is under attack (inCheck()).
    • Undo: Revert the move.
    • If the King remained safe, the move is added to the Valid Moves list.

3. Special Rules Implementation

Performance Analysis

Computational Complexity

Feature This Engine Stockfish / Modern Engines
Logic 2D List, Naive Loop Bitboards (64-bit integers), CPU instructions
Language Python (Interpreted) C++ / Assembly (Compiled, optimized)
Move Gen ~100s-1000s positions/sec >200,000,000 positions/sec
AI / Search None (cannot play) Alpha-Beta Pruning, Negamax, Quiescence
Evaluation None Neural Nets (NNUE), Hand-tuned Heuristics
Strength 0 ELO (Rule Enforcer) 3500+ ELO (Superhuman)

4. User Interface (Retro Tactile Python)

The primary interface is built using PyGame in ChessMain.py, featuring a custom design system:

Conclusion

This engine is a foundational framework for a Chess UI. It correctly enforces the rules of Chess, allowing two humans to play in a premium-feeling environment. Future work will focus on the Artificial Intelligence layers (Minimax/Alpha-Beta) to transform it from a rule enforcer into a tactical opponent.


Appendix: Status & Error Messages (CRT Dialog)

The following transient and persistent messages are displayed in the Pygame CRT Status Dialog based on game state events:

Turn States (Persistent)

Game End States (Persistent)

In-Game Events (Transient)

Error Messages (Transient)

Displayed when the user attempts an invalid action (text turns terra/red):