An open API service indexing awesome lists of open source software.

https://github.com/sepehrbayat/simple-chess-opensource

A complete open-source chess game with AI opponent, move evaluation, and beautiful UI. Built with Python and Pygame.
https://github.com/sepehrbayat/simple-chess-opensource

ai alpha-beta-pruning artificial-intelligence board-game chess chess-engine educational game-development game-engine minimax open-source pygame pygame-tutorial python python-game

Last synced: 7 days ago
JSON representation

A complete open-source chess game with AI opponent, move evaluation, and beautiful UI. Built with Python and Pygame.

Awesome Lists containing this project

README

          

# Simple Chess - Open Source

[![Python](https://img.shields.io/badge/Python-3.7+-blue.svg)](https://www.python.org/)
[![Pygame](https://img.shields.io/badge/Pygame-2.5+-green.svg)](https://www.pygame.org/)
[![License](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

**Author:** Sepehr Bayat

A complete, open-source chess game built with Python and Pygame. Features move quality evaluation, AI opponent with minimax algorithm, and a clean, modular architecture. Perfect for learning chess programming, AI algorithms, and game development.

## ๐ŸŽฎ Features

- **Complete Chess Rules**: All standard chess rules including special moves (castling, en passant, pawn promotion)
- **Move Quality Scoring**: Each move is evaluated and assigned a score from 0-100 based on material balance, position quality, and move impact
- **Visual Feedback**: Highlighted valid moves and selected pieces
- **Game Status**: Real-time display of check, checkmate, and stalemate conditions
- **AI Opponent**: Play against computer with minimax algorithm and alpha-beta pruning
- **Multiple Game Modes**:
- User vs User (two players)
- User vs AI (play as white or black)
- AI vs AI (watch computer play)
- **High-Quality Piece Graphics**: Beautifully drawn chess pieces with shadows and highlights
- **Custom Piece Images**: Support for custom piece images (falls back to drawn pieces if not available)
- **Clean Architecture**: Object-oriented design with separate classes for pieces, board, game logic, AI, and evaluation

## ๐Ÿ“‹ Requirements

- Python 3.7 or higher
- Pygame 2.5.0 or higher
- PyInstaller 6.0.0 or higher (optional, for building executable)

## ๐Ÿš€ Installation

### Quick Setup (Windows)

1. **Clone the repository**
```bash
git clone https://github.com/sepehrbayat/simple-chess-opensource.git
cd simple-chess-opensource
```

Or download the ZIP file from the repository page and extract it.

2. **Run the setup script:**
- **PowerShell**: `.\setup.ps1`
- **Command Prompt**: `setup.bat`

This will:
- Check for Python installation
- Create a virtual environment
- Install all dependencies

### Manual Setup

1. **Install Python 3.7+** from [python.org](https://www.python.org/downloads/)
- Make sure to check "Add Python to PATH" during installation

2. **Create a virtual environment:**
```bash
python -m venv venv
```

3. **Activate the virtual environment:**
- **Windows (PowerShell)**: `.\venv\Scripts\Activate.ps1`
- **Windows (CMD)**: `venv\Scripts\activate.bat`
- **Linux/Mac**: `source venv/bin/activate`

4. **Install dependencies:**
```bash
pip install -r requirements.txt
```

## ๐ŸŽฏ Running the Game

### Run from Source

**Using the run script:**
- **PowerShell**: `.\run.ps1`
- **Command Prompt**: `run.bat`

**Or manually:**
```bash
python main.py
```

### Build Executable (Windows)

To create a standalone `.exe` file using PyInstaller:

```bash
pyinstaller --onefile --windowed --name ChessMVP main.py
```

The executable will be created in the `dist` folder.

**With icon:**
```bash
pyinstaller --onefile --windowed --icon=icon.ico --name ChessMVP main.py
```

## ๐ŸŽฒ How to Play

1. **Select Game Mode**: When the game starts, choose your preferred game mode:
- **User vs User**: Play against another person
- **User vs AI (White)**: You play as white, computer plays as black
- **User vs AI (Black)**: You play as black, computer plays as white
- **AI vs AI**: Watch two computer players compete

2. **Select a Piece**: Click on one of your pieces (white moves first)

3. **Make a Move**: Click on a highlighted square to move the selected piece

4. **Move Score**: After each move, the quality score (0-100) is displayed in the side panel

5. **Game Status**: The panel shows the current turn, game status (check/checkmate/stalemate), and move scores

6. **Quit**: Press ESC to exit the game

## ๐Ÿ“Š Move Scoring System

The move evaluation system assigns scores based on:

- **Material Balance** (0-90 points): Value of captured pieces
- **Position Quality** (0-20 points): Center control and piece activity
- **Move Impact** (0-15 points): Checks, threats, and strategic value

**Score Interpretation:**
- **81-100**: Excellent move (major capture, check, strong threat)
- **61-80**: Good move (gains material, improves position)
- **31-60**: Neutral move
- **0-30**: Poor move (loses material, weakens position)

## ๐Ÿค– AI Engine

The AI uses a minimax algorithm with alpha-beta pruning to find the best moves. The default search depth is 3, which provides a good balance between skill and speed.

## ๐Ÿ“ Project Structure

```
simple-chess-opensource/
โ”œโ”€โ”€ chess/
โ”‚ โ”œโ”€โ”€ __init__.py
โ”‚ โ”œโ”€โ”€ game.py # Game class (main loop, rendering, menu)
โ”‚ โ”œโ”€โ”€ board.py # Board class (grid, state management)
โ”‚ โ”œโ”€โ”€ pieces.py # Piece base class + 6 subclasses
โ”‚ โ”œโ”€โ”€ evaluator.py # Evaluator class (heuristic scoring)
โ”‚ โ”œโ”€โ”€ ai.py # AI engine (minimax algorithm)
โ”‚ โ”œโ”€โ”€ menu.py # Game mode selection menu
โ”‚ โ”œโ”€โ”€ piece_images.py # Piece image loader and renderer
โ”‚ โ””โ”€โ”€ constants.py # Colors, dimensions, Unicode symbols
โ”œโ”€โ”€ assets/
โ”‚ โ””โ”€โ”€ README.md # Instructions for adding custom piece images
โ”œโ”€โ”€ main.py # Entry point
โ”œโ”€โ”€ requirements.txt # Python dependencies
โ”œโ”€โ”€ setup.ps1 # PowerShell setup script
โ”œโ”€โ”€ setup.bat # Batch setup script
โ”œโ”€โ”€ run.ps1 # PowerShell run script
โ”œโ”€โ”€ run.bat # Batch run script
โ”œโ”€โ”€ LICENSE # MIT License
โ””โ”€โ”€ README.md # This file
```

## ๐Ÿ—๏ธ Architecture

The project uses a modular OOP architecture:

- **Piece Hierarchy**: Base `Piece` class with 6 subclasses (Pawn, Rook, Knight, Bishop, Queen, King)
- **Board**: Manages game state, move validation, and check/checkmate detection
- **Evaluator**: Calculates move quality scores using heuristic evaluation
- **AI Engine**: Implements minimax algorithm with alpha-beta pruning for computer player
- **Game**: Handles Pygame main loop, event handling, UI rendering, and game mode management
- **Menu**: Provides game mode selection interface
- **Piece Images**: Loads custom images or generates high-quality drawn pieces

## ๐ŸŽจ Custom Piece Images

You can add your own chess piece images to the `assets/` folder. See `assets/README.md` for details.

Required files:
- `wp.png`, `wn.png`, `wb.png`, `wr.png`, `wq.png`, `wk.png` (white pieces)
- `bp.png`, `bn.png`, `bb.png`, `br.png`, `bq.png`, `bk.png` (black pieces)

If images are not provided, the game will use beautifully drawn pieces with shadows and highlights.

## ๐Ÿงช Testing

Run the test suite:
```bash
python test_chess.py
```

## ๐Ÿ“ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿ‘ค Author

**Sepehr Bayat**

- **Email**: sepehrbayat5@gmail.com
- **Telegram**: [@bayatsepehr](https://t.me/bayatsepehr)
- **LinkedIn**: [linkedin.com/in/sepehrbayat](https://linkedin.com/in/sepehrbayat)
- **GitHub**: [@sepehrbayat](https://github.com/sepehrbayat)

## ๐Ÿ“ฆ Version

Current version: **1.0.0**

## ๐ŸŒŸ Star History

If you find this project useful, please consider giving it a โญ on GitHub!

## ๐Ÿค Contributing

Contributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of conduct and the process for submitting pull requests.

## ๐Ÿ“ Changelog

See [CHANGELOG.md](CHANGELOG.md) for a list of changes and version history.

## ๐Ÿ™ Acknowledgments

- Pygame community for the excellent game development library
- Chess.com for inspiration and chess rules reference
- Open source community for continuous support and feedback

## ๐Ÿ“ž Contact & Links

**Sepehr Bayat**

- **Email**: sepehrbayat5@gmail.com
- **Telegram**: [@bayatsepehr](https://t.me/bayatsepehr)
- **LinkedIn**: [linkedin.com/in/sepehrbayat](https://linkedin.com/in/sepehrbayat)
- **GitHub**: [@sepehrbayat](https://github.com/sepehrbayat)

**Project Links:**
- **Repository**: [simple-chess-opensource](https://github.com/sepehrbayat/simple-chess-opensource)
- **Issues**: [Report a Bug](https://github.com/sepehrbayat/simple-chess-opensource/issues)
- **Discussions**: [Join the Discussion](https://github.com/sepehrbayat/simple-chess-opensource/discussions)

---

**Made with โค๏ธ and Python** | **Enjoy playing Simple Chess!** ๐ŸŽฎโ™Ÿ๏ธ