Project Overview
This is a fully-featured chess game built with C++ and SFML (Simple and Fast Multimedia Library). The project implements complete chess rules, a graphical user interface, save/load functionality, and game state management.
♟️ Complete Chess Rules
All piece movements, check, checkmate, and stalemate detection
🎨 Graphical Interface
Beautiful SFML-based board with custom piece sprites
💾 Save/Load System
Persistent game states and high score tracking
⚙️ Settings Management
Customizable sound and theme preferences
File Structure
| File | Purpose | Lines of Code |
|---|---|---|
| main.cpp | Main game loop and event handling | ~180 |
| board.cpp/h | Board state and initialization | ~40 |
| moves.cpp/h | Move validation for all pieces | ~150 |
| checkmate.cpp/h | Check, checkmate, stalemate logic | ~250 |
| render.cpp/h | Graphics rendering and textures | ~120 |
| menu.cpp/h | Main menu and pause menu UI | ~280 |
| ending.cpp/h | End game screen and results | ~100 |
| save.cpp/h | Game state serialization | ~50 |
| settings.cpp/h | User preferences management | ~30 |
| highscores.cpp/h | Win tracking and statistics | ~40 |
System Architecture
Program Flow
Data Flow Architecture
// Global Board State (board.cpp)
char board[8][8];
// Chess board representation
char turn = 'w'; // Current player ('w' or 'b')
int whiteWins = 0; // White victories
int blackWins = 0; // Black victories
// Main Game Loop (main.cpp)
while (window.isOpen() && inGame) {
// 1. Event Processing
if (mouse click) {
windowToBoard(mousePos); // Convert to board
coords
isMoveValid(from, to); // Validate move
isKingInCheck(side); // Check legality
isCheckmate(opponent); // Win condition
}
// 2. Rendering
drawBoardAndPieces(window);
drawHighlight(selectedSquare);
}
Module Dependencies
Dependencies: All modules (orchestrator)
Role: Central game loop, event handling, state transitions
Dependencies: None (core data)
Role: Maintains board state and turn tracking
Dependencies: board.cpp
Role: Validates piece-specific move rules
Dependencies: board.cpp, moves.cpp
Role: Advanced game state detection (check, checkmate, stalemate)
Module Documentation
Purpose: Manages the chess board state and global game variables
Sets up the initial chess position with all pieces in their starting squares
Debug function to print the board state to console
Global Variables:
Purpose: Validates all chess piece movements according to chess rules
Master function that validates any move based on piece type and game state
Validates pawn moves: forward 1/2 squares, diagonal captures
Validates rook moves: straight lines (horizontal/vertical)
Validates bishop moves: diagonal lines
Validates knight moves: L-shape (2+1 squares)
Validates queen moves: combination of rook + bishop
Validates king moves: one square in any direction
Checks if there are no pieces blocking the path between two squares
Purpose: Implements check, checkmate, and stalemate detection
Determines if a square is under attack by a given color
Locates the king position for a given side
Checks if the specified side's king is currently in check
Determines if a player has any legal moves available (critical for checkmate/stalemate)
Returns true if the side is in checkmate (in check + no legal moves)
Returns true if the side is in stalemate (not in check + no legal moves)
Gets all legal king moves for a side (used for UI hints)
Attack Detection Algorithm:
Purpose: Handles all SFML rendering including board, pieces, and highlights
Loads all piece sprites and board texture from assets folder
Renders the chess board and all pieces in their current positions
Draws a semi-transparent yellow highlight on the selected square
Converts mouse pixel coordinates to board row/column indices
Piece Texture Mapping:
Purpose: Manages game state serialization to files
Saves current board state, turn, and scores to file
Loads a previously saved game state from file
Save File Format:
Game Logic Deep Dive
Move Validation System
The game uses a multi-layered validation system to ensure all moves are legal:
Layer 1: Basic Validation
Layer 2: Piece-Specific Rules
Example: Pawn Movement
Layer 3: Check Validation
After a move is made, the game ensures the king is not in check:
Checkmate Detection Algorithm
Board Representation
The board uses a character-based 2D array where:
- Uppercase letters = White pieces (P, R, N, B, Q, K)
- Lowercase letters = Black pieces (p, r, n, b, q, k)
- Space character = Empty square
- Row 0 = Black's back rank (top of screen)
- Row 7 = White's back rank (bottom of screen)
User Interface System
Menu System
Features:
- Start New Game
- Load Saved Game
- Settings Configuration
- High Scores Display
- Exit Game
Accessed during gameplay by pressing P key
- Resume - Return to game
- Save Game - Save current state
- Exit to Main Menu - Abandon current game
Game Result Types:
Rendering Pipeline
Mouse-to-Board Coordinate Conversion
Keyboard Controls
| Key | Action | Context |
|---|---|---|
| ESC | Exit to main menu | During game or settings |
| P | Pause menu | During game |
| S | Quick save | During game |
| Mouse Click | Select piece / Make move | During game |
| Enter / Space | Play again | End screen |
Build & Compilation Guide
⚠️ Prerequisites
- SFML 2.6.2 - Graphics library
- MinGW/G++ - C++ compiler (Windows)
- GCC - C++ compiler (Linux/Mac)
- Assets folder - Chess piece images and board texture
Windows Compilation
Linux Compilation
Required Directory Structure
chess_game/
│
├── main.cpp
├── board.cpp / board.h
├── moves.cpp / moves.h
├── checkmate.cpp / checkmate.h
├── render.cpp / render.h
├── menu.cpp / menu.h
├── ending.cpp / ending.h
├── save.cpp / save.h
├── highscores.cpp / highscores.h
├── settings.cpp / settings.h
│
├── assets/
│ ├── board.png
│ └── pieces/
│ ├── white_pawn.png
│ ├── white_rook.png
│ ├── white_knight.png
│ ├── white_bishop.png
│ ├── white_queen.png
│ ├── white_king.png
│ ├── black_pawn.png
│ ├── black_rook.png
│ ├── black_knight.png
│ ├── black_bishop.png
│ ├── black_queen.png
│ └── black_king.png
│
├── savegame.txt (auto-generated)
├── highscores.dat (auto-generated)
└── settings.cfg (auto-generated)
Common Compilation Issues
| Error | Solution |
|---|---|
| Cannot find SFML headers | Verify -I flag points to SFML include directory |
| Undefined reference to SFML functions | Check -L flag and ensure all -lsfml-* libraries are linked |
| Failed to load font | Ensure Arial font exists at C:/Windows/Fonts/arial.ttf (Windows) |
| Failed to load textures | Create assets/pieces/ folder with all PNG files |
| Window doesn't open | SFML DLLs must be in same directory as executable (Windows) |
Runtime Files Generated
savegame.txt
Stores the current game state including:
- 8x8 board configuration (8 lines)
- Current turn (w or b)
- White wins count
- Black wins count
highscores.dat
Simple text file with two integers:
settings.cfg
User preferences:
Performance Notes
- Game runs at 60 FPS (capped via
window.setFramerateLimit(60)) - Checkmate detection uses brute-force search (acceptable for chess)
- Texture loading happens once at startup
- No dynamic memory allocation during gameplay