Magic Chess

An AI-driven chess variant exploring decision systems, game theory, and heuristic search algorithms.

Python Game AI Minimax Alpha-Beta Pruning 2024

The Problem

Chess engines are well-studied, but chess variants with modified rules are underexplored. I wanted to build an AI for a custom chess variant — "Magic Chess" — where certain pieces have special abilities (teleportation, area attacks, temporary invincibility). The challenge: standard chess evaluation functions don't apply, and the branching factor explodes with ability choices.

This project was partly a passion project (I enjoy chess) and partly an exploration of decision systems under complex state spaces — skills directly applicable to reinforcement learning and game theory problems in ML.

The Architecture

graph TB A[Game State] --> B[Move Generator] B --> C{Special Abilities?} C -->|Yes| D[Ability Resolver] C -->|No| E[Standard Moves] D --> F[Minimax with Alpha-Beta] E --> F F --> G[Evaluation Heuristic] G --> H[Best Move] style F fill:#6c5ce7,stroke:#7c6df0,color:#fff style G fill:#00d2ff,stroke:#00b8e6,color:#0a0a0f

Why It's Hard

  • Custom evaluation heuristics. In standard chess, piece values and positional tables are well-known. In Magic Chess, special abilities change piece value dynamically. A knight that can teleport is worth more than a standard knight — but how much more? This required designing novel heuristics.
  • Branching factor explosion. Each piece with an active ability adds a decision layer. The search tree grows combinatorially. Alpha-Beta pruning helps, but move ordering becomes critical — and standard ordering heuristics don't account for ability moves.
  • Iterative deepening with time constraints. The AI needs to make reasonable moves within a time budget. Implementing iterative deepening with the expanded move set required careful profiling.
  • Playtesting-driven tuning. The only way to know if the AI was "fun" was to play against it. I built a simple Pygame UI and iterated on heuristics based on actual gameplay — not just search depth.

Technical Stack

  • Python — core engine: board representation, move generation, search algorithms
  • Minimax with Alpha-Beta pruning — decision engine with configurable search depth
  • Custom evaluation heuristics — material + positional + ability-state scoring
  • Pygame — simple interactive UI for playtesting
  • Move ordering optimizations — ability-move-aware ordering for better pruning efficiency

What I'd Do Differently

  • Explore MCTS (Monte Carlo Tree Search). For games with high branching factors and complex state evaluations, MCTS often outperforms Minimax. AlphaZero-style approaches are particularly interesting for variants.
  • Train evaluation weights with self-play. Instead of hand-tuning heuristics, use self-play + reinforcement learning to discover optimal piece valuations automatically.
  • Add a web-based UI. Pygame is fine for testing, but a web UI (React + WebSocket) would make it shareable and playable by others.
  • Implement difficulty levels. Limiting search depth and adding controlled randomness would make the AI accessible to casual players while keeping it challenging for experienced ones.

Key Takeaways

Building game AI teaches you about search under constraints, heuristic design, and the tradeoff between accuracy and compute — the same problems you face in production ML systems, just in a more playful domain.
← Back to Projects