Compare commits

..

No commits in common. "0aa8b7a16f2a91b91317119c60d78b2acbd8f552" and "97f9def306c5e5650e95468640bd410317381fe2" have entirely different histories.

6 changed files with 5 additions and 137 deletions

View File

@ -1,82 +1,3 @@
# Stickfosh # Stickfosh
> [Stockfish](https://stockfishchess.org), but worse :) > Stockfish, but worse :)
## Overview
This project is a **Chess AI** built using the **Model-View-Controller (MVC)
pattern**. It provides a modular framework for playing chess, allowing both
**human vs AI** and **AI vs AI** matches. The AI has undergone several
iterations, improving its decision-making capabilities through enhancements like
**alpha-beta pruning**, **move ordering**, **iterative deepening**, and
**transposition tables**.
## Features
- **MVC Architecture**: The project follows the MVC pattern for clean separation of concerns:
- **Model**: Handles chess rules, board state, and AI logic.
- **View**: GUI and NoOp (console-based) rendering options.
- **Controller**: Manages interactions between players and the game.
- **Multiple AI Versions**: Several AI versions with increasing complexity have been implemented.
- **AI vs AI Matches**: A dedicated mode to watch different AI versions compete.
- **Human vs AI Mode**: Play against the AI using a graphical interface.
- **FEN Support**: Load chess positions using FEN notation.
- **Performance Testing (Perft)**: Built-in performance testing for move generation.
## AI Iterations
This project has undergone multiple AI improvements, including:
1. **v0 Random AI**: Selects moves randomly.
1. **v1 Pure Minimax**: Implements basic minimax search.
1. **v2 Alpha-Beta Pruning**: Optimizes minimax with pruning.
1. **v3 Move Ordering**: Prioritizes moves to improve search efficiency.
1. **v4 Search Captures**: Enhances move ordering by focusing on captures.
1. **v5 Better Endgame**: Introduces heuristics for endgame play.
1. **v6 Iterative Deepening**: Dynamically adjusts search depth for better performance.
1. **v7 Transposition Tables**: Caches board states to reduce redundant computations.
## Installation & Usage
### Prerequisites
- C++ Compiler (C++17 or later)
- `make`
- SFML (for GUI rendering)
### Build Instructions
1. Clone the repository:
```sh
git clone https://github.com/karma-riuk/stickfosh.git
cd stickfosh
```
1. Create a build directory and compile:
```sh
make main
```
1. Run the program:
```sh
./main
```
## Running AI vs AI Matches
To watch two AI versions play against each other, modify `main.cpp` to instantiate the desired AI versions and run:
```sh
./main
```
## Video Demo
<!-- [![AI vs AI Chess Match](https://img.youtube.com/vi/XXXXXXXXXX/0.jpg)](https://www.youtube.com/watch?v=XXXXXXXXXX) -->
<!-- *Click the image above to watch a video of two AI versions competing!* -->
## Future Improvements
- Implement **opening book** for better early-game decisions.
- Enhance **evaluation function** with more advanced heuristics.
- Introduce **neural network-based AI** for machine-learning-driven play.
- Introduce **Monte-Carlo Tree Search** for stochastic driven play.

View File

@ -10,12 +10,6 @@
Move ai::v3_AB_ordering::_search(const Board& b) { Move ai::v3_AB_ordering::_search(const Board& b) {
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();
std::sort(moves.begin(), moves.end(), [&](Move& m1, Move& m2) {
int score = m1.score_guess(b) - m2.score_guess(b);
if (!am_white)
score *= -1;
return score < 0;
});
Move best_move; Move best_move;
int best_eval = -INFINITY; int best_eval = -INFINITY;
@ -75,10 +69,7 @@ int ai::v3_AB_ordering::_ab_search(
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();
std::sort(moves.begin(), moves.end(), [&](Move& m1, Move& m2) { std::sort(moves.begin(), moves.end(), [&](Move& m1, Move& m2) {
int score = m1.score_guess(b) - m2.score_guess(b); return m1.score_guess(b) > m2.score_guess(b);
if (!am_white)
score *= -1;
return score < 0;
}); });
Move best_move; Move best_move;

View File

@ -5,6 +5,8 @@
#include <map> #include <map>
static int position_counter = 0;
Move ai::v6_iterative_deepening::_search(const Board& b) { Move ai::v6_iterative_deepening::_search(const Board& b) {
ThreadPool pool(std::thread::hardware_concurrency()); ThreadPool pool(std::thread::hardware_concurrency());
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();
@ -13,8 +15,7 @@ Move ai::v6_iterative_deepening::_search(const Board& b) {
int best_eval = -INFINITY; int best_eval = -INFINITY;
std::map<Move, std::future<int>> futures; std::map<Move, std::future<int>> futures;
int depth; for (int depth = 1; !stop_computation; depth++) {
for (depth = 1; !stop_computation; depth++) {
for (const Move& move : moves) { for (const Move& move : moves) {
Board tmp_board = b.make_move(move); Board tmp_board = b.make_move(move);
futures.insert( futures.insert(
@ -37,7 +38,5 @@ Move ai::v6_iterative_deepening::_search(const Board& b) {
} }
futures.clear(); futures.clear();
} }
std::cout << "Went up until depth: " << depth << std::endl;
return best_move; return best_move;
} }

View File

@ -24,31 +24,3 @@ int Move::score_guess(const Board& b) const {
return ret; return ret;
} }
Move Move::from_string(std::string move) {
if (!(4 <= move.size() && move.size() <= 5))
throw std::invalid_argument("Move must be 4 or 5 characters long");
Move ret;
ret.source_square = Coords::from_algebraic(move.substr(0, 2)).to_index();
ret.target_square = Coords::from_algebraic(move.substr(2, 2)).to_index();
if (move.size() == 5)
switch (move[4]) {
case 'n':
ret.promoting_to = Knigt;
break;
case 'b':
ret.promoting_to = Bishop;
break;
case 'r':
ret.promoting_to = Rook;
break;
case 'q':
ret.promoting_to = Queen;
break;
default:
throw std::invalid_argument("Promotion piece must be one of 'nbrq'"
);
}
ret.target_square = Coords::from_algebraic(move.substr(2, 2)).to_index();
return ret;
}

View File

@ -15,8 +15,6 @@ struct Move {
int score_guess(const Board&) const; int score_guess(const Board&) const;
static Move from_string(std::string);
std::string to_string() const { std::string to_string() const {
std::stringstream ss; std::stringstream ss;
ss << Coords::from_index(source_square) ss << Coords::from_index(source_square)

View File

@ -1,13 +0,0 @@
#include "../src/model/board/board.hpp"
#include "lib.hpp"
int main() {
std::string str_move = "a2a3";
ASSERT_EQUALS(str_move, Move::from_string(str_move).to_string());
str_move = "b2f4";
ASSERT_EQUALS(str_move, Move::from_string(str_move).to_string());
str_move = "a2a1r";
ASSERT_EQUALS(str_move, Move::from_string(str_move).to_string());
}