GeorgesCoding / Chess

Chess game with an AI engine developed using the PyGame module
MIT License
4 stars 0 forks source link
chess-game minimax-alpha-beta-pruning pygame python two-player

Overview

https://github.com/GeorgesCoding/Chess/assets/118407807/88b3ea94-de94-4dbb-b189-552dcc79c38a

Notes:

#

Dependencies

Tested on Python 3.12.

Requires PyGame module installation: pip install pygame

#

Running the Program

Below are the steps for downloading and running the program locally or using the release file. The release file can run the game without using an IDE or the need to download Python and the PyGame module.

A. Downloading the release file

  1. Go to the repository's release page: click me!
  2. On the latest release (the topmost of the page), click on Assets.
  3. Click on the file called Chess.exe. This will download the program's file.
  4. Run this file. Windows Defender may abort the program as it's a random .exe file downloaded.
  5. To run, click "More Info", then "Run anyway".

    B. Running locally through an IDE with Python installed

  6. Clone repository: git clone https://github.com/GeorgesCoding/Chess.git
  7. Install PyGame: pip install pygame
  8. In the IDE terminal, run the file with the command: py Main.py

Notes

#

Files/Modules

For a more in-depth explanation of the game's functionality, see Game Structure.

  1. Main.py: Game initialization and event handler

    • Running this file will initialize the game window to the starting state, an empty board waiting for a button press.
    • Is responsible for dictating game behaviour by examining the event (mouse/keyboard clicks) detected by PyGame.
    • Uses functions from other modules to determine how to draw the state of the board from these events.
    • Three main buttons are restart, two-player mode and computer with the pawn promotion buttons underneath.
  2. GUI.py: Drawing and displaying components

    • Draws the board, pieces, buttons, and side window and updates dialogue text throughout the game.
    • The dialogue window is altered through the recursive function addText.
    • Also includes the dragging animation when moving pieces with functions that help locate and identify the piece.
    • Images for the pieces and buttons are stored in the Assets folder.
  3. Controller.py: Game controller and move computations

    • Responsible for controlling the game logistics and button/keyboard presses.
    • Contains functions for move computations, move validity and helper functions for computations and checks.
    • Includes en passant capture, castling, check, checkmate, stalemate and pawn promotion.
  4. Engine.py: Chess Engine

    • Uses a minimax algorithm with alpha-beta pruning for the computer-generated moves.
    • Utilizes piece-square tables for middle and end-game scenarios for stronger move computations.
    • Currently reaches a max depth of 3 in the search tree within an appropriate response time.
  5. Package.py: Executable packaging script

    • First, the script copies all necessary modules and files into a temporary folder.
    • Then edits the main files, Main.Py, GUI.py and Controller.py to include the _resourcepath function and replace the old image paths.
    • This function allows the picture binary data to be read through the compiled Python code.
    • After will create a version info resource file to be read by PyInstaller to include file information.
    • Will then sign the file using Microsoft's SignTool.
    • Automatically deletes any unnecessary files leaving only the executable in the current working directory.

#

Game Structure

image

#

Features: Buttons and Dialogue Window

In this program, there are three main buttons:

  1. Restart

    • Restarts the game to the beginning state, waiting for the user to select a game mode.

      image-removebg-preview

  2. Two Player Mode

    • Two-player mode initializes the game to white to move, rotating the board after every move.

      image-removebg-preview (1)

  3. Computer Mode

    • Computer moves will prompt the user to press the key on their keyboard to determine their colour:
    • B for black, W for white and R for random.
    • The game will be initialized accordingly.

      image-removebg-preview (2)

\ Underneath these buttons are the pawn promotion buttons:

image-removebg-preview (12)

image-removebg-preview (11)

During checkmate, all piece movements and buttons are disabled with only the restart button enabled.

There is a black box on the bottom right-hand side of the screen underneath the pawn promotion buttons. Here, commentary will be displayed according to the moves of the players. It will also display if the move is invalid if the king is in check, castling, checkmate and more. The most recent comment is at the bottom.

Chess - frame at 0m59s