Skip to content

ValentinJSchmidt/Chess

Repository files navigation

# Chess AI

In our midterm course project for the Symbolic AI Course we created an GOFAI system using negamax-search, pruning and iterative deepening and various heuristic. The system is specified to play a smaller variant of chess against other agents. It plays better then an inexperienced chess player.

## Requirements

- Python >= 3.7
- pygame
- tkinter

Install dependencies:

```bash
pip install pygame
pip install tk
```

## Starting the GUI

Run `ChessMain.py` with the following arguments:

```bash
python3 ChessMain.py --agent1 [AGENT] --agent2 [AGENT] --verbose --time_control [SECONDS] --use_gui
```

**Agent options:**

| Value       | Description                                              |
|-------------|----------------------------------------------------------|
| `Human`     | Play yourself via the GUI                                |
| `MrRandom`  | Plays completely random valid moves                      |
| `MrNovice`  | Plays using a basic minimax algorithm                    |
| `Agent1`    | Uses the `Agent` class from `student_agents/template.py` |
| `Agent2`    | Uses the `Agent` class from `student_agents/template2.py`|
| `path/to/agent.py` | Path to a custom agent file (not supported on Windows) |

**Example — Human vs. your agent:**

```bash
python3 ChessMain.py --agent1 Human --agent2 Agent1 --time_control 20 --use_gui
```

**Example — Agent vs. Agent (no GUI):**

```bash
python3 ChessMain.py --agent1 Agent1 --agent2 MrRandom --time_control 20
```

## Board Selection

Edit `Settings.json` and set the board value to `1` or `2` to choose between available board layouts.

## Agent Implementation

Your agent lives in `student_agents/template.py`. The class must be named `Agent` and follow the interface in that file. Use `update_move` to register preliminary moves so your agent does not forfeit on timeout.

Your agent must be **single-threaded**. Multi-threaded agents are disqualified.



## Algorithm

Our agent (`student_agents/template.py`) uses **Negamax with Alpha-Beta pruning** and **iterative deepening**:

- **Negamax**: A symmetric formulation of minimax — each node maximizes the negated score of its children, so white and black are handled with the same code.
- **Alpha-Beta pruning**: Cuts off branches that cannot influence the final decision, dramatically reducing the number of nodes evaluated.
- **Iterative deepening**: The search starts at depth 1 and increments indefinitely. After each completed depth, the best move found so far is registered via `update_move`, so the agent always has a valid move ready when time runs out.

## Evaluation Function

The heuristic scores the board by summing material value and positional bonuses for each piece:

**Material values:** K=0, Q=9, R=5, B=3, N=3, p=1

**Piece-square tables:** Each piece type has a 6×6 positional score table that rewards good squares (e.g. knights in the center, pawns advanced toward promotion). Pawns have separate tables for white and black to incentivise advancement.

**Pawn structure / endgame:** The white and black pawn tables assign very high scores (9–10) to pawns near the promotion rank, encouraging the agent to push pawns in the endgame.

**Zobrist hashing + transposition table:** Board positions are hashed using a Zobrist hash (random 64-bit integers per piece/square combination, XOR'd together). Evaluated positions are cached in a lookup table so repeated positions are not re-evaluated.

---

About

Our repository for the chess competition. The framework for this is not mine, it was provided by the lecture. We ended up being on the eleventh place, ranking in the top third.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages