Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Saphereye authored Oct 9, 2024
1 parent 72103d8 commit 199f8b4
Showing 1 changed file with 59 additions and 8 deletions.
67 changes: 59 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
# Shard Chess Engine
<img src="https://github.com/Saphereye/shard/blob/main/assets/demo.png" width="500" />

*Shard Vs Shard (Shard Wins)*
<img src="https://github.com/Saphereye/shard/blob/main/assets/demo.png" width="300" />

## Introduction
Shard is a chess engine written in Rust. It is supports most UCI commands.
The engine utilizes the Monte Carlo Tree Search algorithm.
*Shard vs Shard (Shard Wins)*

The tree looks like this:
## Overview

**Shard** is a chess engine built in Rust that supports most UCI (Universal Chess Interface) commands. The engine employs the **Monte Carlo Tree Search (MCTS)** algorithm to evaluate and make decisions. The goal of Shard is to explore the possibilities of integrating MCTS into chess, giving it a unique style of move generation and search depth.

## Key Features
- Written entirely in **Rust**.
- Supports **UCI** commands for easy integration with GUIs and other chess systems.
- Uses **Monte Carlo Tree Search (MCTS)** to evaluate moves based on exploration and exploitation trade-offs.

## How It Works

At the core of Shard's engine is a tree-like structure representing different game states (positions on the chessboard). Each "Node" corresponds to a board position, with additional details tracked for evaluation:

- **t:** The total score accumulated by visiting this position.
- **n:** The number of visits to this position (how many times the engine has evaluated this position in simulations).

After performing the simulations, to pick the _best move_, the engine picks the move with the most number of visits. The rationale is that the move shows the most promise.

### Example Tree Structure:
```
Node t:16396, n:36 (rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1) (None)
Node t:11048, n:8 (rnbqkbnr/pppppppp/8/8/6P1/8/PPPPPP1P/RNBQKBNR b KQkq - 0 1) (g2g4)
Expand All @@ -21,5 +36,41 @@ Node t:16396, n:36 (rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1) (N
...
...
```
Each 'Node' represents a position in the game. The 't' value is the total score of the position, and the 'n' value is the number of times the position has been visited.
When a move is made, the engine will set the current position to the position of the move, and the tree will be updated accordingly from there.

In this example, the top node represents the starting position of a chess game. Each subsequent node represents a move, with the total score (`t`) and visit count (`n`) helping the engine evaluate which positions are worth further exploration. The engine continuously updates this tree as moves are played, focusing more on moves that yield promising results based on prior simulations.

To choose the next node to explore, the evaluation is performed using the $\text{UCB1}$ score, which in this case is calculated as follows:

$$
\text{UCB1} = \frac{c_t}{c_n} + \sqrt{2} \times \sqrt{\frac{\log p_n}{c_n}}
$$

Here, $c_t$ and $c_n$ represent the total score and the number of visits to the current node. Similarly, $p_n$ represents the number of visits to the current node's parent. The constant $\sqrt{2}$ balances the _exploitation vs. the exploration_ term, corresponding to the left and right of the addition term on the RHS.

Here, $c_t$ and $c_n$ represent the total score and the number of visits to the current node, respectively, while $p_n$ represents the number of visits to the current node's parent. The constant $\sqrt{2}$ balances the exploitation (left term) and exploration (right term) components.

In traditional Monte Carlo Tree Search, the **rollout** phase consists of a simulation of the game that assigns a score of `+1/-1` for a win or loss. However, as the game reaches the endgame in chess, it becomes increasingly difficult for random moves to reach a conclusion. Therefore, I use a handcrafted board evaluation function to assign scores instead during rollouts.

## Getting Started

1. **Clone the repository:**
```bash
git clone https://github.com/Saphereye/shard.git
cd shard
```

2. **Run the engine:**
```bash
chmod u+x run.sh
./run.sh
```

The `build.sh` script does the additional task of copying the executable to the lichess-bot project.

## License

This project is licensed under the GNU GPLv3 License. See the [LICENSE](LICENSE) file for details.

---

Feel free to contribute or open an issue if you have any feedback or suggestions!

0 comments on commit 199f8b4

Please sign in to comment.