How ovladisluvv/haskell-chess-2026 works
Purpose and Runtime
A desktop, local-hotseat chess game with optional built-in AI. It runs as a Haskell executable using Gloss for 2D/OpenGL graphics and loads bitmap piece sprites at runtime from assets/pieces/. The build/package definition is haskell-chess.cabal and package.yaml; Stack is the expected development runtime.
Application Entry and Game Loop
app/Main.hs is the executable entry point and delegates application startup to src/Lib.hs. The library establishes the Gloss-driven application loop: render the current game/UI state, accept input events, and transition state over time. This is an in-process desktop application; there is no server, persistence layer, or network service.
Core Domain State
src/Types.hs defines the shared chess and UI model: board/piece representation, colors, moves, game status, history, and likely menu/promotion/selection state. It is the contract between rules, AI, rendering, and event handling. src/Board.hs owns initial-position construction, board updates, coordinate conversion, and board-oriented utilities.
Move Generation and Rules Boundary
src/Moves.hs generates candidate movements by piece type. src/Rules.hs is the authoritative legality and game-resolution layer: it filters candidates against king safety and applies chess-specific state transitions, including castling, en passant, promotion, check/checkmate, stalemate, repetition, fifty-move, and insufficient-material outcomes. UI and bot decisions should cross this boundary through legal moves rather than directly mutating board state.
Bot Decision Stage
src/Bot.hs consumes legal game positions and selects a move for computer-controlled turns. Its architecture is minimax search with alpha-beta pruning at depth 4, using material and piece-square-table evaluation, including endgame-aware king evaluation. Bot execution is coordinated by the main state/event flow, rather than a separate process or external AI service.
Input, UI State, and Rendering
src/Events.hs translates Gloss mouse and keyboard events into state transitions: menu selection, board-square selection, move attempts, promotion choices, reset/menu navigation, and undo behavior. It invokes the rules and bot stages as appropriate. src/Render.hs converts current state into Gloss pictures: chessboard, sprites, legal-move/check highlights, captured pieces, history, controls, menus, promotion overlay, and terminal-game displays. Sprite resources are assets/pieces/wk.bmp, assets/pieces/bk.bmp, and peer piece files.
Primary Control Flow
Startup initializes resources and menu/game state (app/Main.hs → src/Lib.hs). Menu events choose PvP or bot mode; gameplay events select a source and destination, request legal validation/application from src/Rules.hs, then update history/status. In bot mode, a completed human turn triggers src/Bot.hs, whose selected legal move returns through the same rules/state-update path. Every resulting state is rendered by src/Render.hs; undo restores prior state and may account for an AI response.
Delivery
Release automation is defined in .github/workflows/release.yml. Released binaries must be distributed with or retain access to the runtime bitmap assets under assets/pieces/.
Components 13
Desktop runtime
Local Gloss executable and resource loading.
- Executable entry
app/Main.hs - Gloss game loop · Owns rendering, event dispatch, and time-driven state updates.
src/Lib.hs - Input transitions · Maps mouse and keyboard input to menu and gameplay actions.
src/Events.hs - Game renderer
src/Render.hs - Piece sprites · Bitmap sprites loaded for board pieces.
Chess engine
Shared state, move generation, rules, and AI.
- Game and UI state · Board, moves, status, history, and UI state contract.
src/Types.hs - Board operations · Initial setup, board updates, and coordinates.
src/Board.hs - Candidate moves · Generates piece-type movement candidates.
src/Moves.hs - Legal rules and outcomes · Authoritative legal move application and game resolution.
src/Rules.hs - Chess bot · Depth-4 alpha-beta search with positional evaluation.
src/Bot.hs
Release delivery
Build metadata and release packaging.
- Cabal package
haskell-chess.cabal - Hpack package spec
package.yaml - Release workflow · Publishes binaries with access to runtime sprite assets.
.github/workflows/release.yml
Connections 19
- Executable entry → Gloss game loop: starts
- Gloss game loop → Input transitions: dispatches input
- Gloss game loop → Game renderer: renders state
- Game renderer → Piece sprites: loads
- Gloss game loop → Game and UI state: owns current state
- Input transitions → Legal rules and outcomes: requests legal moves
- Input transitions → Chess bot: triggers AI turn
- Input transitions → Game and UI state: updates UI state
- Legal rules and outcomes → Candidate moves: filters candidates
- Legal rules and outcomes → Board operations: applies board updates
- Legal rules and outcomes → Game and UI state: updates status and history
- Candidate moves → Game and UI state: reads position
- Board operations → Game and UI state: constructs and transforms
- Chess bot → Legal rules and outcomes: uses legal moves
- Chess bot → Game and UI state: evaluates positions
- Game renderer → Game and UI state: visualizes
- Release workflow → Cabal package: builds package
- Cabal package → Hpack package spec: package metadata
- Release workflow → Piece sprites: ships with binaries