Skip to content

Commit cadfe4f

Browse files
committed
documentation update
1 parent aae63f0 commit cadfe4f

2 files changed

Lines changed: 190 additions & 100 deletions

File tree

README.md

Lines changed: 85 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,115 @@
1-
# ♟️ Chessboard Engine
1+
# Chess Engine
22

3-
A chess engine written in **C**, using **bitboards** and tested with **Criterion**.
4-
It can load FEN positions, generate all legal moves, and run **perft tests** to validate move generation.
5-
6-
---
7-
8-
## 📂 Project Structure
9-
10-
├── assets/ # Resources (images, ...)
11-
├── lib/
12-
│ └── chessboard/ # Engine headers
13-
├── src/
14-
│ └── chessboard/ # Engine source code
15-
├── tests/ # Unit tests (Criterion)
16-
├── Makefile # Build system
17-
└── README.md # This file
18-
19-
---
20-
21-
## ⚡ Installation & Build
22-
23-
### Requirements
24-
- **gcc** or **clang**
25-
- **make**
26-
- **Criterion** for tests
27-
```sh
28-
sudo apt-get install libcriterion-dev
29-
```
30-
31-
32-
### Build with GUI
33-
```sh
34-
make ui
35-
```
36-
37-
38-
### Run the engine
39-
```sh
40-
make run
41-
```
42-
43-
### Run tests
44-
```sh
45-
make test
46-
```
3+
A high-performance chess engine written in C, featuring an advanced move generation system, graphical interface, and bot API for automated gameplay.
474

485
## Features
496

50-
- Load positions via FEN ✅
51-
52-
- Generate all legal moves ✅
53-
54-
- Play a game through a GUI ✅
55-
56-
- Validate move generation using perft tests ✅
57-
58-
- Position evaluation (WIP)
59-
60-
- Search engine (minimax / alpha-beta, WIP)
61-
62-
### 🧪 CI/CD
63-
64-
This project is automatically tested with GitHub Actions.
65-
Unit tests are executed on every push and pull request.
66-
67-
Move generation is validated using perft tests.
68-
👉 [Perft results reference](https://www.chessprogramming.org/Perft_Results)
69-
70-
## 🚀 Optimizations
71-
72-
To improve performance and speed up perft tests, several optimizations have been implemented:
73-
74-
- Bitboards: 64-bit masks to represent the board, enabling fast operations with bitwise logic.
7+
### 🚀 High-Performance Move Generation
8+
- **Bitboard representation** for efficient board state management
9+
- **Magic bitboards** for O(1) sliding piece move generation
10+
- **Multi-threaded perft testing** achieving up to 80 million moves per second
11+
- Compact move structures optimized for cache efficiency
12+
13+
### 🎮 Graphical Interface
14+
- Interactive chess board with visual piece movement
15+
- Human vs Bot gameplay
16+
- Bot vs Bot gameplay
17+
18+
### 🤖 Bot API
19+
- bot communication
20+
- Support for bot vs bot matches
21+
- Standardized move format and game state representation
22+
- Easy integration for custom chess engines
23+
24+
### 🧠 Chess Engine
25+
- Legal move generation with full rule support
26+
- Evaluation function for position assessment
27+
- Search algorithms for move selection
28+
- Support for standard chess notation
29+
30+
## Getting Started
31+
32+
### Prerequisites
33+
- GCC or compatible C compiler
34+
- Make build system
35+
- POSIX threads support (for multi-threading)
36+
- SDL2 for ui
37+
38+
### Building the Project
39+
40+
```bash
41+
# Clone the repository
42+
git clone [https://github.com/TomBaran501/chessboardEngine.git]
43+
cd [chessboardEngine]
44+
45+
# Build and run the project
46+
make run
47+
```
7548

76-
- Precomputed tables: cached attacks for "normal" pieces (knight, pawn, king) and magic bitboards for sliding pieces (rook, bishop, queen).
49+
## Documentation
7750

78-
- Multi-threaded perft: parallelized tree exploration to take advantage of multi-core CPUs.
51+
Comprehensive documentation is available in the `documentation/` folder:
7952

80-
- Reduced dynamic allocations: use of preallocated arrays (Move move_list[MAX_MOVES]) to avoid unnecessary memory overhead.
53+
- **[Move Generation](documentations/legal_move_generation.md)** - Technical details on the legal move generation system, optimization techniques, and performance benchmarks
54+
- **[Bot API](documentations/bot_api.md)** - API specification for developing and connecting chess bots
55+
- **[Main Commands](documentations/commands_main.md)** - Command-line interface reference and usage guide
8156

82-
### ⚡ Performance:
57+
## Usage
8358

84-
Single-thread speed: 8–10 million moves/s
59+
### Playing Against the Bot
8560

86-
Multi-thread speed: 40–90 million moves/s
87-
(⚠️ Multi-threading is less efficient when the root position has fewer legal moves)
61+
Launch the graphical interface and play against the built-in chess engine. Use the documented commands to control the game flow.
8862

89-
## Future Work
63+
### Running Bot vs Bot Matches
9064

91-
Planned improvements and features to make the engine stronger and more complete:
65+
Start the API server and connect your bots to compete against each other. See the [Bot API documentation](documentation/bot_api.md) for integration details.
9266

93-
#### Search improvements
67+
### Performance Testing
9468

95-
- Implement iterative deepening
69+
Run perft tests to verify move generation correctness and measure performance:
9670

97-
- Add alpha-beta pruning
71+
```bash
72+
./chess
73+
go perft [depth]
74+
```
9875

99-
- Introduce move ordering (killer moves, history heuristic)
76+
## Project Structure
10077

101-
#### Position evaluation
78+
```
79+
.
80+
├── src/ # Source code files
81+
├── lib/ # Header files
82+
├── documentation/ # Project documentation
83+
├── tests/ # Test suites
84+
├── bots/ # Test suites
85+
├── Makefile # Build configuration
86+
└── README.md # This file
10287
103-
- Material and piece-square tables
10488
105-
- King safety and pawn structure evaluation
89+
## Performance Benchmarks
10690
107-
- Mobility and control of the center
91+
- **Move generation**: Up to 80 million moves/second (perft)
92+
- **Evaluation speed**: Between 5 millions and 40 millions positions evaluated per second
10893
109-
#### Transposition tables
94+
## Technical Highlights
11095
111-
- Store already-evaluated positions using Zobrist hashing
96+
- Efficient bitboard-based board representation
97+
- Magic bitboard move generation for sliding pieces
98+
- Multi-threaded perft calculations
99+
- Cache-optimized data structures
100+
- Minimal memory footprint per move
112101
113-
- Reduce redundant calculations in search
114102
115-
#### UCI Protocol support
103+
## License
116104
117-
- Allow communication with GUIs such as Arena, Cute Chess, or lichess-bot
105+
MIT License
118106
107+
Copyright (c) 2025 Baran Tom
119108
120-
---
109+
## Acknowledgments
121110
122-
## 📸 Screenshots
111+
- Magic bitboard implementation inspired by [Chess Programming Wiki](https://www.chessprogramming.org/)
112+
- Perft positions from standard test suites
123113
124-
### 🧵 Multi-threaded Perft Test
125-
Exemple d’un perft test lancé en multi-threading :
126-
![Perft Test](assets/screenshots/result_perft_test_multi_threading.png)
127114
128-
### 🎨 Graphical User Interface
129-
Interface graphique permettant de jouer une partie :
130-
![Chessboard UI](assets/screenshots/ihm_chess_engine.png)
115+
**Note**: This is an educational/hobby project demonstrating advanced techniques in chess programming and game engine development.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Legal Move Generation
2+
3+
## Overview
4+
5+
This chess engine implements a highly optimized legal move generation system, achieving performance of up to **80 million moves per second** during perft testing. The implementation leverages modern optimization techniques including bitboards, magic bitboards, efficient data structures, and multi-threading.
6+
7+
## Core Techniques
8+
9+
### Bitboards
10+
11+
The engine uses a bitboard representation where each piece type and color is represented as a 64-bit integer, with each bit corresponding to a square on the board. This approach offers several advantages:
12+
13+
- **Parallel operations**: Multiple squares can be evaluated simultaneously using bitwise operations
14+
- **Memory efficiency**: The entire board state fits in CPU cache
15+
- **Fast move generation**: Bitwise operations (AND, OR, XOR, shifts) are extremely fast at the hardware level
16+
17+
### Magic Bitboards
18+
19+
Magic bitboards are used for sliding piece move generation (bishops, rooks, queens). This technique uses pre-computed lookup tables with magic numbers to instantly retrieve possible moves:
20+
21+
1. **Occupancy masking**: Relevant occupied squares are identified
22+
2. **Magic multiplication**: A magic number transforms the occupancy into a unique hash
23+
3. **Table lookup**: The hash indexes into a pre-computed move table
24+
25+
This eliminates the need for iterative ray-casting and provides O(1) move generation for sliding pieces.
26+
27+
### Compact Move Structures
28+
29+
Moves are represented using minimal memory to maximize cache efficiency:
30+
31+
- Compact bit-packed structures store source square, destination square, move type, and flags
32+
- Smaller move structures mean more moves fit in cache, reducing memory latency
33+
- Move lists can be processed more efficiently due to better spatial locality
34+
35+
### Multi-threading
36+
37+
The move generation system leverages parallel processing for performance-critical operations:
38+
39+
- Perft calculations are parallelized across multiple threads
40+
- Work is distributed efficiently to maximize CPU utilization
41+
- Thread-safe data structures ensure correctness in concurrent scenarios
42+
43+
## Performance Testing with Perft
44+
45+
### What is Perft?
46+
47+
Perft (performance test) is a standard debugging and benchmarking function in chess programming. It counts all leaf nodes at a given depth in the game tree, serving as both a correctness test and a performance benchmark.
48+
49+
### Testing Methodology
50+
51+
The engine's move generation was validated and benchmarked using perft tests:
52+
53+
1. **Correctness verification**: Results were compared against known perft values from standard positions
54+
2. **Performance measurement**: Nodes per second (NPS) was measured across various depths
55+
3. **Regression testing**: Perft suites ensure modifications don't introduce bugs
56+
57+
### Performance Results
58+
59+
The implementation achieves **80 million moves per second** during perft testing, demonstrating:
60+
61+
- Efficient move generation algorithms
62+
- Effective use of CPU cache through compact data structures
63+
- Successful parallelization across multiple cores
64+
- Minimal branching and optimal instruction pipelining
65+
66+
### Standard Test Positions
67+
68+
Common perft positions used for validation include:
69+
70+
- **Starting position**: `rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1`
71+
- **Kiwipete**: A complex middlegame position testing en passant, castling, and promotions
72+
- **Position 3-6**: Additional positions from the perft suite covering edge cases
73+
74+
👉 [Perft results reference](https://www.chessprogramming.org/Perft_Results)
75+
76+
## Implementation Highlights
77+
78+
### Move Generation Pipeline
79+
80+
1. **Bitboard initialization**: Board state is maintained as bitboards
81+
2. **Attack generation**: Magic bitboards generate sliding piece attacks
82+
3. **Legal move filtering**: Moves are validated against check and pin constraints
83+
4. **Move encoding**: Legal moves are packed into compact structures
84+
5. **Move ordering**: Moves are sorted for optimal alpha-beta pruning (when used in search)
85+
86+
### Optimization Techniques Applied
87+
88+
- **Pre-computed lookup tables**: Attack patterns, magic numbers, and ray tables
89+
- **Bit manipulation intrinsics**: Use of hardware instructions (POPCNT, TZCNT, etc.)
90+
- **Branch prediction optimization**: Code structured to minimize mispredictions
91+
- **Memory alignment**: Data structures aligned for optimal CPU access
92+
- **Copy-make approach**: Efficient board state copying for recursive search
93+
94+
## Future Improvements
95+
96+
Potential areas for further optimization:
97+
98+
- SIMD instructions for parallel bitboard operations
99+
- GPU acceleration for bulk perft calculations
100+
- Advanced prefetching strategies
101+
- Further tuning of magic bitboard tables
102+
103+
## Conclusion
104+
105+
The combination of bitboards, magic bitboards, compact data structures, and multi-threading results in a move generation system that is both fast and correct. The 80 million NPS achieved during perft testing demonstrates that the implementation is competitive with modern chess engines and provides a solid foundation for the complete chess engine.

0 commit comments

Comments
 (0)