A fully functional, terminal-based chess engine built in modern C++17. This project implements FIDE rules, relying on a strong Object-Oriented Programming (OOP) architecture, polymorphism, and safe memory management using smart pointers.
- OOP Architecture: Polymorphic piece structure derived from a base
Piececlass. - Memory Safety: Zero memory leaks achieved through the exclusive use of
std::unique_ptr. - Advanced Move Validation: Strict adherence to FIDE rules, including blocking moves that expose the King to a check.
- Special Mechanics: Fully implemented castling, pawn promotion, and en passant validation.
- Game State Detection: Automatic detection of Check, Checkmate, and Stalemate using a custom rollback buffer mechanism.
- CLI Interface: Clean terminal-based chessboard rendering using the
tabulatelibrary. - Continuous Game Loop: Ability to play multiple matches seamlessly without restarting the executable.
Language: C++17
Build System: CMake (>= 3.14)
Compiler: GCC / Clang / MSVC
Dependencies: tabulate (Header-only library for terminal formatting, fetched
automatically via CMake)
Clone the project repository:
git clone git@github.com:K0D1Z/cpp-chess.gitGo to the project directory:
cd cpp-chessCreate a build directory and navigate into it:
mkdir build
cd buildGenerate build files using CMake (Note: An active internet connection is required during this step to fetch the tabulate dependency:
cmake ..Compile the project:
makeStart the game:
./ChessGame/
├── CMakeLists.txt
├── README.md
├── LICENSE
└── src/
├── main.cpp
├── core/
│ ├── Game.h
│ ├── Game.cpp
│ ├── Board.h
│ ├── Board.cpp
│ ├── Position.h
│ ├── Position.cpp
│ └── ChessTypes.h
└── pieces/
├── Piece.h
├── Piece.cpp
├── Pawn.h
├── Pawn.cpp
├── Rook.h
├── Rook.cpp
├── Knight.h
├── Knight.cpp
├── Bishop.h
├── Bishop.cpp
├── Queen.h
├── Queen.cpp
├── King.h
└── King.cpp
The core of the engine is built on an Object-Oriented design, utilizing polymorphism to handle specific piece movements and a simulated rollback buffer for move validation.
Tip
Click 'Expand' button to see Mermaid.js based project structure
classDiagram
class Board {
-unique_ptr~Piece~ board_[8][8]
-Col_T enPassantCol_
-Row_T enPassantRow_
+print() void
+isEmpty() bool
+hasEnemy() bool
+isSquareUnderAttack() bool
+isInCheck(kingColor) bool
+hasAnyLegalMove(playerColor) bool
+hasSufficientMaterial() bool
+isCheckmate(playerColor) bool
+isStalemate(playerColor) bool
+getPiece(col, row) Piece*
+setPiece(col, row, piece) void
+movePiece(fromCol, fromRow, toCol, toRow, optionalPromotionPiece) bool
+getEnPassantCol() Col_T
+getEnPassantRow() Row_T
+setupInitialPositions() void
+reset() void
}
class Piece {
<<abstract>>
#Position position_
#PieceType type_
#Color color_
#bool hasMoved_
+virtual ~Piece()
+virtual isLegalMove(targetCol, targetRow, board) bool
+virtual getSymbol() char
+isValid(col, row) bool$
+hasMoved() bool
+getPosition() Position
+getColor() Color
+getType() PieceType
+print() void
+setPos(position) void
+moveTo(col, row) void
}
class King {
+isLegalMove(targetCol, targetRow, board) bool
+getSymbol() char
}
class Pawn {
+isLegalMove(targetCol, targetRow, board) bool
+getSymbol() char
}
class Rook {
+isLegalMove(targetCol, targetRow, board) bool
+getSymbol() char
}
class Queen {
+isLegalMove(targetCol, targetRow, board) bool
+getSymbol() char
}
class Bishop {
+isLegalMove(targetCol, targetRow, board) bool
+getSymbol() char
}
class Knight {
+isLegalMove(targetCol, targetRow, board) bool
+getSymbol() char
}
class Game {
-Board board_
-int drawCounter_
-Color currentTurn_
-parseMove() bool$
+run() void
}
class Position {
-Col_T col_
-Row_T row_
+getCol() Col_T
+getRow() Row_T
+setCol() void
+setRow() void
+print() void
}
Piece <|-- King : Inheritance
Piece <|-- Pawn : Inheritance
Piece <|-- Rook : Inheritance
Piece <|-- Queen : Inheritance
Piece <|-- Bishop : Inheritance
Piece <|-- Knight : Inheritance
Board *-- Piece : Composition (unique_ptr)
Game *-- Board : Composition
Piece *-- Position : Composition
The project uses Doxygen for API documentation.
To generate the HTML documentation locally, ensure you have Doxygen installed and run the following command in the root directory:
doxygen DoxyfileOnce generated, the documentation is available in HTML format. You can open it using any web browser:
You can open it directly from the terminal:
xdg-open docs/html/index.htmlTip
If xdg-open is not available, simply navigate to the folder and double-click index.html.
Use the following command in PowerShell or Command Prompt:
start docs/html/index.htmlTip
Alternatively, locate the docs/html folder in File Explorer and open index.html with your preferred browser (
Chrome, Firefox, Edge).
open docs/html/index.html