Skip to content

omarabdiwali/chess-cpp

Repository files navigation

ChessGame

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.

Position moves

Features

  • 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

Previous move

Getting Started

Prerequisites

  • C++ compiler (e.g., g++, clang++)
  • Standard Template Library (STL)
  • Windows OS (uses Windows.h for console coloring)

Building and Running

  1. Compile the source files:

    g++ -o ChessGame main.cpp ChessBoard.cpp
  2. Run the executable:

    ./ChessGame

Usage

  1. The game starts with the initial chess position (standard FEN: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1).
  2. Players take turns to move pieces (White moves first).
  3. 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).
  4. The game will display the board with:
    • Selected piece highlighted in green
    • Possible destination squares highlighted in blue
  5. 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.
  6. 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).
  7. The game will update the board and display the new position along with the move that was made.
  8. If a player is in checkmate, the game ends and the other player wins.
  9. Type "-1" at any prompt to quit the game.

FEN Position Loading

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);

Code Structure

main.cpp

  • 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.

ChessBoard.cpp

  • Implements the ChessBoard class, 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)

ChessBoard.h

  • Header file declaring the ChessBoard class and its methods.
  • Defines piece constants (W_PAWN, B_KING, etc.) and the Move tuple type.
  • Declares the public interface for the chess board functionality.

Implementation Details

Move Validation Algorithm

The engine validates moves by:

  1. Generating pseudo-legal moves for the selected piece
  2. Simulating each move on a copied board state
  3. Checking if the king would be in check after the move
  4. Only keeping moves that result in a safe king position

FEN Parsing

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

Check Detection

The isInCheck() function checks for attacks from:

  • Pawns (diagonal captures)
  • Knights (L-shaped moves)
  • Bishops/Queens (diagonals)
  • Rooks/Queens (orthogonals)
  • Kings (adjacent squares)

Dependencies

  • <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.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A simple command-line chess game implemented in C++.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages