Skip to content

K0D1Z/cpp-chess

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Console Chess Engine

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.

Author

Badges

C++17 CMake MIT License

Features

  • OOP Architecture: Polymorphic piece structure derived from a base Piece class.
  • 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 tabulate library.
  • Continuous Game Loop: Ability to play multiple matches seamlessly without restarting the executable.

Tech Stack

Language: C++17
Build System: CMake (>= 3.14)
Compiler: GCC / Clang / MSVC
Dependencies: tabulate (Header-only library for terminal formatting, fetched automatically via CMake)

Run Locally

Clone the project repository:

  git clone git@github.com:K0D1Z/cpp-chess.git

Go to the project directory:

  cd cpp-chess

Create a build directory and navigate into it:

  mkdir build
  cd build

Generate build files using CMake (Note: An active internet connection is required during this step to fetch the tabulate dependency:

  cmake ..

Compile the project:

  make

Start the game:

  ./ChessGame

Project Structure

/
├── 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

Architecture / Class Diagram

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
Loading

Documentation

The project uses Doxygen for API documentation.

Generating Documentation

To generate the HTML documentation locally, ensure you have Doxygen installed and run the following command in the root directory:

  doxygen Doxyfile

Viewing the Documentation

Once generated, the documentation is available in HTML format. You can open it using any web browser:

Linux

You can open it directly from the terminal:

xdg-open docs/html/index.html

Tip

If xdg-open is not available, simply navigate to the folder and double-click index.html.

Windows

Use the following command in PowerShell or Command Prompt:

start docs/html/index.html

Tip

Alternatively, locate the docs/html folder in File Explorer and open index.html with your preferred browser ( Chrome, Firefox, Edge).

macOs

open docs/html/index.html

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors