This project is a C++ implementation of a chess game with both graphical and console-based interfaces. The game supports human players and AI opponents of varying difficulty levels. It includes functionalities such as move validation, special moves (like castling and pawn promotion), and game state checking (check, checkmate, stalemate).
- Graphical Display: Utilizes SFML to render the chessboard and pieces.
- Console Display: Outputs the chessboard state and status messages to the console.
- Human Player Support: Allows two human players to play against each other.
- AI Player Support: Includes multiple levels of AI difficulty:
- Level 1: Random legal moves.
- Level 2: Prefers capturing moves and checks.
- Level 3: Prefers avoiding capture, capturing moves, and checks.
- Level 4: Uses a weighted random selection based on move scores for increased difficulty and randomness.
- Special Moves: Supports castling and pawn promotion.
- Game State Checking: Identifies check, checkmate, and stalemate conditions.
Enter command: game human human
New game started between Human (White) and Human (Black).
8 ♜ ♞ ♝ ♛ ♚ ♝ ♞ ♜
7 ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
6 . . . . . . . .
5 . . . . . . . .
4 . . . . . . . .
3 . . . . . . . .
2 ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
1 ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
a b c d e f g h
- C++ Compiler: Ensure you have a C++14 compatible compiler installed.
- Make: Used for building the project.
- SFML Development Libraries: Required for graphical display.
-
Homebrew: Install Homebrew if you haven't already.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" -
SFML: Install SFML to get the necessary libraries.
brew install sfml@2
-
Make: Install Make.
xcode-select --install
-
Clone the Repository:
git clone https://github.com/matteo-psnt/chess.git cd chess -
Build the Project:
make
After building the project, run the executable:
./chess- inc/: Contains header files for various classes.
- src/: Contains implementation files for various classes.
- build/: Directory where the object files are generated.
- main.cc: Entry point for the application.
- game.h / game.cc: Manages the game flow and state.
- board.h / board.cc: Represents the chessboard and handles piece placement and movement.
- pieces.h / pieces.cc: Defines the different types of chess pieces and their movement logic.
- player.h / player.cc: Abstract class for players and derived classes for human and AI players.
- gamemanager.h / gamemanager.cc: Manages game initialization, player setup, and command processing.
- display.h / display.cc: Handles graphical display using SFML.
- position.h: Defines the Position struct used to represent coordinates on the board.
- playeraction.h: Defines the playerAction struct used to represent and score potential moves.
The game accepts the following commands:
- game [whitePlayerType] [blackPlayerType]: Starts a new game with specified player types (human or computer[1-4]).
- setup: Enters setup mode to manually set up the board.
- move [start_position] [end_position] [optional_promotion]: Moves a piece from the start position to the end position. For pawn promotions, specify the promotion piece (e.g., move e7 e8 Q to promote to a queen).
- print: Displays the current state of the board in the console.
-
Start a New Game with Two Human Players:
game human human
-
Start a New Game with a Human Player vs. Level 2 AI:
game human computer[2]
-
Enter Setup Mode:
setup
-
Move a Piece:
move a2 a4
-
Pawn Promotion:
move a7 a8 Q
Default promotion is to queen if no piece type is specified.
