This project implements a text-mode chess game in C++ using object-oriented programming principles. The game allows users to play chess by entering moves in algebraic notation, with support for all standard chess rules and special moves.
- Board Display: Text-based representation of the chessboard with Unicode chess symbols.
- Move Input: Accepts moves in algebraic notation (e.g.,
e2e4) via standard input. - Turn Management: Alternates between white and black players.
- Basic Piece Movements: Supports legal moves for all pieces — pawn, rook, knight, bishop, queen, and king — with full validation.
- Captures: Handles piece captures including pawn diagonal captures.
- Pawn Rules: Implements pawn forward movement (1 or 2 squares on first move), diagonal captures, and promotion to queen, rook, bishop, or knight.
- Check Detection: Identifies when a king is in check and prevents moves that would leave the king in check.
- Game End Conditions: Detects checkmate and stalemate, ending the game appropriately.
- Special Moves:
- Castling: Kingside (
O-O) and queenside (O-O-O) with full validation (unmoved pieces, clear path, no check through traversed squares). - En Passant: Pawn capture en passant under correct timing conditions.
- Castling: Kingside (
- Commands: Supports
/quit,/resign, and/drawto end the game at any point. - Output: Prints the final board position in canonical form and the game result (
1-0,0-1,1/2-1/2,?-?).
- OOP Design: Uses inheritance with an abstract
Piecebase class and concrete subclasses for each piece type. - Modular Structure: Code is organized into separate files for maintainability:
| File | Responsibility |
|---|---|
square.h / square.cpp |
Square coordinates and algebraic notation conversion |
piece.h / piece.cpp |
Piece classes and movement logic for all 6 types |
echiquier.h / echiquier.cpp |
Board state, piece placement, move validation, game logic |
jeu.h / jeu.cpp |
User interface, turn management, game flow and result |
chess.cpp |
Main program entry point |
Makefile |
Build system (C++17, separate compilation) |
- Memory Management: Proper dynamic allocation for pieces with cleanup in destructors.
- Error Handling: Validates moves and provides meaningful error messages for illegal attempts.
- Move Validation: Correctly implementing all chess rules, especially complex cases like castling (king must not pass through check) and en passant (one-move timing window).
- Check Detection: Simulating board states to verify whether a move would leave or expose the king to check.
- Pawn Promotion: Handling user input for promotion choice while maintaining game flow, including a fallback to queen in non-interactive (piped) mode.
- Canonical Position Output: Generating the exact serialized string format required for testing, with correct comma separation and color prefixes.
- Unicode Display: Ensuring chess symbols render correctly across different terminal environments.
- Language & Standard: C++17 — used for modern features such as
std::optionalto track the en passant target square. - Inheritance Hierarchy: Polymorphism for pieces allows uniform handling while preserving piece-specific behavior (notably, pawn attack vs. pawn movement differ).
- Board Representation: A 2D array of
Piece*pointers combined with per-color piece lists for flexible and efficient iteration. - Input Handling:
cin-based input withisattydetection — prompts are suppressed when stdin is piped, enabling scriptable use. - Build System:
Makefilewith separate compilation for clean builds and proper dependency tracking. - Code Style: Meaningful variable names, comments on complex logic, and clear separation of concerns across modules.
Compile:
makeRun interactively:
./chessRun with a move file:
cat moves.txt | ./chessRun with comment filtering:
grep -v "^#" moves.txt | ./chess a b c d e f g h
8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
Move (eg. a1a8) ? e2e4
Move (eg. a1a8) ? e7e5
Move (eg. a1a8) ? O-O
Move (eg. a1a8) ? /quit
wR,wN,wB,wQ,,wB,,wR,... ?-?
Project/
├── chess.cpp # Entry point
├── jeu.h / jeu.cpp # Game controller
├── echiquier.h / echiquier.cpp # Board and move logic
├── piece.h / piece.cpp # Abstract Piece + 6 concrete types
├── square.h / square.cpp # Square / coordinate utilities
├── Makefile # Build system
└── test/
├── test-level.sh # Test runner
└── data/ # Move sequence test cases
├── 1-leg-*.txt # Legal piece moves
├── 1-ill-*.txt # Illegal move rejection
├── 3-leg-*.txt # Special moves (castling, en passant, promotion)
└── 4-leg-*.txt # Endgame scenarios (checkmate, stalemate)