A comprehensive command-line chess engine implemented in C++. The game supports two players and includes full chess rules with advanced features and complete FEN (Forsyth-Edwards Notation) support.
- Complete Chess Rules: Full implementation including castling (kingside and queenside), en passant captures, and pawn promotion
- FEN Support: Parse positions from FEN notation and generate FEN strings of current board state
- Move Validation: Comprehensive validation ensuring moves are legal (including check prevention)
- Check/Checkmate/Stalemate Detection: Accurate detection of check, checkmate, and stalemate conditions
- Turn-based Gameplay: Full game loop with turn management
- Colored Console Output: Visual highlighting of selected pieces and possible moves (Windows-specific)
- Game State Tracking: Half-move clock and full-move number preservation
- C++ compiler (e.g., g++, clang++)
- Standard Template Library (STL)
- Windows OS (uses Windows.h for console coloring)
-
Compile the source files:
g++ -o ChessGame main.cpp ChessBoard.cpp
-
Run the executable:
./ChessGame
- The game starts with the initial chess position (standard FEN:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1). - Players take turns to move pieces (White moves first).
- To make a move, enter the algebraic notation of the piece's current square (e.g., "e2" for the pawn in front of the white king). Note: Use lowercase letters for files (a-h) and digits for ranks (1-8).
- The game will display the board with:
- Selected piece highlighted in green
- Possible destination squares highlighted in blue
- Enter the destination square (e.g., "e4") to move the piece.
- Alternatively, enter the square of another one of your pieces to change your selection.
- If a pawn reaches the opposite end of the board, you will be prompted to choose a promotion piece (queen, rook, bishop, or knight). Enter "q", "r", "b", or "n" (case-insensitive).
- The game will update the board and display the new position along with the move that was made.
- If a player is in checkmate, the game ends and the other player wins.
- Type "-1" at any prompt to quit the game.
The game currently starts with the standard chess position. To modify the starting position, edit the fen string in main.cpp:
string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
ChessBoard board(fen);- Contains the main function and game loop.
- Handles user input and output.
- Manages the game state and turn-based gameplay.
- Initializes the board with a FEN string.
- Implements the
ChessBoardclass, which represents the chess board and pieces. - Provides methods for:
- FEN parsing and generation (
getPositionsFromFen(),getFen()) - Piece movement (individual movement rules for each piece type)
- Move validation (including check prevention via simulation)
- Special moves (castling, en passant, pawn promotion)
- Check/checkmate/stalemate detection (
isInCheck(),isCheckmate(),isStalemate()) - Board display (colored console output)
- FEN parsing and generation (
- Header file declaring the
ChessBoardclass and its methods. - Defines piece constants (
W_PAWN,B_KING, etc.) and theMovetuple type. - Declares the public interface for the chess board functionality.
The engine validates moves by:
- Generating pseudo-legal moves for the selected piece
- Simulating each move on a copied board state
- Checking if the king would be in check after the move
- Only keeping moves that result in a safe king position
The FEN parser correctly interprets:
- Piece placement
- Active color
- Castling rights (both kingside and queenside for both colors)
- En passant target square (with proper context validation)
- Half-move clock (for 50-move rule)
- Full-move number
The isInCheck() function checks for attacks from:
- Pawns (diagonal captures)
- Knights (L-shaped moves)
- Bishops/Queens (diagonals)
- Rooks/Queens (orthogonals)
- Kings (adjacent squares)
<iostream>: For input/output operations.<string>: For string manipulation.<vector>: For dynamic arrays.<tuple>: For returning multiple values from functions.<algorithm>: For sorting and searching algorithms.<set>: For storing unique elements.<stdexcept>: For exception handling.<sstream>: For string stream operations.<random>: For generating random numbers.- Windows.h: For console text coloring (Windows-specific).
<cmath>: For floor function in pawn movement and square name generation.
This project is licensed under the MIT License - see the LICENSE file for details.

