# keshavraj7/chess-game architecture

> Architecture diagram of the GitHub repository keshavraj7/chess-game, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/keshavraj7/chess-game
- Repository: https://github.com/keshavraj7/chess-game

## Overview

1. **Overall shape**
   This is a single-process, desktop chess application in C++ with SFML handling the window, input, and drawing. The code is organized around a clear split between:
   - chess domain logic,
   - game/session orchestration,
   - rendering/UI,
   - AI move selection.

2. **Entry point and runtime loop**
   `main.cpp` is the application bootstrap. It likely creates the top-level `Game` object and hands off control to the main loop. The runtime is event-driven: SFML provides input events and window lifecycle, while the game loop repeatedly processes input, updates game state, and renders the board.

3. **Game/session coordinator**
   `Game.cpp/.h` appears to be the central controller. It likely owns the current mode, turn state, selected piece, game-over status, and the transition between human turns and AI turns. This is the layer that connects user actions, rule enforcement, and rendering.

4. **Board and rules engine**
   `Board.cpp/.h` is the core rules subsystem. It likely stores the board state, piece placement, turn information, move history, and special-state flags such as castling rights or en passant eligibility. It is also the natural boundary for legality checks, check/checkmate/stalemate detection, and applying/undoing moves.

5. **Piece hierarchy**
   `Piece.h` defines the abstraction for all pieces, and `Pawn.cpp`, `Knight.cpp`, `Bishop.cpp`, `Rook.cpp`, `Queen.cpp`, and `King.cpp` implement piece-specific movement rules. This is the main polymorphic domain model: the board asks a piece for legal moves, and each subclass contributes its own movement and special-case behavior.

6. **Move representation**
   `Move.h` is the shared data contract for actions in the system. It likely carries source/destination squares and any extra metadata needed for chess rules such as captures, promotions, castling, or en passant. This object is probably used by both the rules engine and the AI search tree.

7. **AI subsystem**
   `AI.cpp/.h` encapsulates the computer opponent. The README confirms a minimax search with alpha-beta pruning, so this module is likely responsible for:
   - generating candidate moves from board state,
   - evaluating positions,
   - choosing the best move for the computer side.
   It depends on the board/rules layer but should remain independent from rendering and input.

8. **Rendering and user interface**
   `Renderer.cpp/.h` is the presentation layer. It likely loads SFML textures from `assets/`, draws the board and pieces, highlights legal moves and last move, and renders end-state overlays like checkmate or promotion selection. This layer should consume game state but not implement chess rules itself.

9. **Data flow between subsystems**
   The main flow is:
   user input from SFML → `Game` interprets selection/action → `Board` validates and applies move → `Game` updates turn/game-state flags → `Renderer` redraws the result.
   In human-vs-computer mode, the flow branches after a human turn: `Game` asks `AI` for a move, `AI` queries board state and returns a move, then `Board` applies it.

10. **Special chess mechanics**
   The repository explicitly supports castling, en passant, promotion, check, checkmate, and stalemate. Architecturally, these are not separate subsystems; they are cross-cutting rule behaviors implemented primarily in `Board` and the piece classes, with `Game` coordinating prompts or state transitions when promotion or game-over occurs.

11. **Game modes and state boundaries**
   The application supports both human-vs-human and human-vs-computer play. The important boundary here is turn ownership: `Game` decides whether the active side is controlled by a local player or by `AI`, while `Board` remains the authority for whether a move is legal regardless of who proposes it.

12. **Assets and external dependency**
   The `assets/` directory is a required runtime dependency containing piece images and other visual resources. SFML is the only major external framework, and it materially defines the UI architecture: window management, event handling, texture loading, and drawing are all delegated to it.

13. **Why this architecture is maintainable**
   The design follows a classic object-oriented chess split: `Piece` subclasses encode movement behavior, `Board` encodes game rules and state, `Game` coordinates sessions, `AI` reasons over moves, and `Renderer` handles presentation. That separation makes it straightforward to diagram the system as four major layers: input/control, rules/state, AI, and rendering.

Main entry points: [`Piece.h`](https://github.com/keshavraj7/chess-game/blob/main/Piece.h) (Piece), [`Game.cpp`](https://github.com/keshavraj7/chess-game/blob/main/Game.cpp) (Game), [`Board.cpp`](https://github.com/keshavraj7/chess-game/blob/main/Board.cpp) (Board), [`AI.cpp`](https://github.com/keshavraj7/chess-game/blob/main/AI.cpp) (AI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_runtime["Runtime"]
  node_main(("main<br/>entrypoint<br/>[main.cpp]"))
  node_game["Game<br/>session controller<br/>[Game.cpp]"]
end

subgraph group_grp_rules["Rules"]
  node_board[("Board<br/>rules engine<br/>[Board.cpp]")]
  node_move["Move<br/>[Move.h]"]
  node_piece["Piece<br/>base model<br/>[Piece.h]"]
  node_pawn["Pawn<br/>piece rule<br/>[Pawn.cpp]"]
  node_knight["Knight<br/>piece rule<br/>[Knight.cpp]"]
  node_bishop["Bishop<br/>piece rule<br/>[Bishop.cpp]"]
  node_rook["Rook<br/>piece rule<br/>[Rook.cpp]"]
  node_queen["Queen<br/>piece rule<br/>[Queen.cpp]"]
  node_king["King<br/>piece rule<br/>[King.cpp]"]
end

subgraph group_grp_ai["AI"]
  node_ai["AI<br/>minimax search<br/>[AI.cpp]"]
end

subgraph group_grp_ui["UI"]
  node_renderer["Renderer<br/>presentation<br/>[Renderer.cpp]"]
  node_sfml{{"SFML<br/>framework"}}
  node_assets["Assets<br/>resource set"]
end

node_main -->|"starts"| node_game
node_game -->|"manages"| node_board
node_game -->|"draws via"| node_renderer
node_game -->|"requests move"| node_ai
node_game -->|"uses"| node_move
node_board -->|"owns pieces"| node_piece
node_board -->|"applies"| node_move
node_piece -->|"specializes"| node_pawn
node_piece -->|"specializes"| node_knight
node_piece -->|"specializes"| node_bishop
node_piece -->|"specializes"| node_rook
node_piece -->|"specializes"| node_queen
node_piece -->|"specializes"| node_king
node_ai -->|"analyzes"| node_board
node_ai -->|"returns"| node_move
node_renderer -->|"depends on"| node_sfml
node_renderer -->|"loads"| node_assets
node_game -->|"receives input"| node_sfml

click node_main "https://github.com/keshavraj7/chess-game/blob/main/main.cpp"
click node_game "https://github.com/keshavraj7/chess-game/blob/main/Game.cpp"
click node_board "https://github.com/keshavraj7/chess-game/blob/main/Board.cpp"
click node_move "https://github.com/keshavraj7/chess-game/blob/main/Move.h"
click node_piece "https://github.com/keshavraj7/chess-game/blob/main/Piece.h"
click node_pawn "https://github.com/keshavraj7/chess-game/blob/main/Pawn.cpp"
click node_knight "https://github.com/keshavraj7/chess-game/blob/main/Knight.cpp"
click node_bishop "https://github.com/keshavraj7/chess-game/blob/main/Bishop.cpp"
click node_rook "https://github.com/keshavraj7/chess-game/blob/main/Rook.cpp"
click node_queen "https://github.com/keshavraj7/chess-game/blob/main/Queen.cpp"
click node_king "https://github.com/keshavraj7/chess-game/blob/main/King.cpp"
click node_ai "https://github.com/keshavraj7/chess-game/blob/main/AI.cpp"
click node_renderer "https://github.com/keshavraj7/chess-game/blob/main/Renderer.cpp"
click node_assets "https://github.com/keshavraj7/chess-game/tree/main/assets"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_game toneBlue
class node_board,node_move,node_piece,node_pawn,node_knight,node_bishop,node_rook,node_queen,node_king toneAmber
class node_ai toneMint
class node_renderer,node_sfml,node_assets toneRose
```

## Components

### Runtime

Application entry and main loop

- [main](https://github.com/keshavraj7/chess-game/blob/main/main.cpp): entrypoint · Bootstraps the app and starts the game loop · `main.cpp`
- [Game](https://github.com/keshavraj7/chess-game/blob/main/Game.cpp): session controller · Coordinates turns, modes, selection, and flow between systems · `Game.cpp`

### Rules

Board state, move model, and piece logic

- [Board](https://github.com/keshavraj7/chess-game/blob/main/Board.cpp): rules engine · Owns board state, legality, and special chess mechanics · `Board.cpp`
- [Move](https://github.com/keshavraj7/chess-game/blob/main/Move.h): move model · Shared representation for chess actions and metadata · `Move.h`
- [Piece](https://github.com/keshavraj7/chess-game/blob/main/Piece.h): base model · Abstract piece behavior and movement contract · `Piece.h`
- [Pawn](https://github.com/keshavraj7/chess-game/blob/main/Pawn.cpp): piece rule · Pawn movement and special handling · `Pawn.cpp`
- [Knight](https://github.com/keshavraj7/chess-game/blob/main/Knight.cpp): piece rule · Knight movement rules · `Knight.cpp`
- [Bishop](https://github.com/keshavraj7/chess-game/blob/main/Bishop.cpp): piece rule · Bishop movement rules · `Bishop.cpp`
- [Rook](https://github.com/keshavraj7/chess-game/blob/main/Rook.cpp): piece rule · Rook movement rules · `Rook.cpp`
- [Queen](https://github.com/keshavraj7/chess-game/blob/main/Queen.cpp): piece rule · Queen movement rules · `Queen.cpp`
- [King](https://github.com/keshavraj7/chess-game/blob/main/King.cpp): piece rule · King movement and castling behavior · `King.cpp`

### AI

Computer move selection

- [AI](https://github.com/keshavraj7/chess-game/blob/main/AI.cpp): minimax search · Chooses computer moves using board evaluation and search · `AI.cpp`

### UI

Rendering and assets

- [Renderer](https://github.com/keshavraj7/chess-game/blob/main/Renderer.cpp): presentation · Draws board, pieces, highlights, and overlays via SFML · `Renderer.cpp`
- **SFML**: framework · Windowing, input, textures, and drawing backend
- [Assets](https://github.com/keshavraj7/chess-game/tree/main/assets): resource set · Piece images and visual runtime resources · `assets`

## Connections

- main → Game: starts · Application bootstrap hands control to the game controller
- Game → Board: manages · Game coordinates board state and applies moves
- Game → Renderer: draws via · Game triggers presentation of current state
- Game → AI: requests move · Game asks the computer opponent for a move when needed
- Game → Move: uses · Game passes move actions between input, rules, and AI
- Board → Piece: owns pieces · Board stores and queries the piece hierarchy
- Board → Move: applies · Moves are validated and executed against board state
- Piece → Pawn: specializes · Pawn implements the base piece contract
- Piece → Knight: specializes · Knight implements the base piece contract
- Piece → Bishop: specializes · Bishop implements the base piece contract
- Piece → Rook: specializes · Rook implements the base piece contract
- Piece → Queen: specializes · Queen implements the base piece contract
- Piece → King: specializes · King implements the base piece contract
- AI → Board: analyzes · AI reads board state for search and evaluation
- AI → Move: returns · AI outputs a chosen move
- Renderer → SFML: depends on · Renderer uses SFML for windowing and drawing
- Renderer → Assets: loads · Renderer consumes visual resources from the assets directory
- Game → SFML: receives input · Game consumes SFML events and window state
