Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ef25455ac0 | ||
|
4aa3d4ba33 | ||
|
cb1a1274a0 | ||
|
1ff9b33bda | ||
|
f7733c1317 | ||
|
0aa8b7a16f | ||
|
ebf5934909 | ||
|
ecba8e3c6e | ||
|
1834b3b8ce | ||
|
ec4cc85ca3 | ||
|
97f9def306 |
4
Makefile
4
Makefile
@ -28,8 +28,8 @@ obj/%.o:
|
|||||||
@mkdir -p $(dir $@)
|
@mkdir -p $(dir $@)
|
||||||
$(CXX) $(CXXFLAGS) -o $@ -c $<
|
$(CXX) $(CXXFLAGS) -o $@ -c $<
|
||||||
|
|
||||||
main: $(OBJFILES)
|
stickfosh: $(OBJFILES)
|
||||||
$(CXX) $(CXXFLAGS) $(OBJFILES) $(LOADLIBES) $(LDLIBS) -o main -lsfml-graphics -lsfml-window -lsfml-system
|
$(CXX) $(CXXFLAGS) $(OBJFILES) $(LOADLIBES) $(LDLIBS) -o stickfosh -lsfml-graphics -lsfml-window -lsfml-system
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf obj/* $(DEPFILES) test_bin/
|
rm -rf obj/* $(DEPFILES) test_bin/
|
||||||
|
85
README.md
85
README.md
@ -1,3 +1,86 @@
|
|||||||
# Stickfosh
|
# Stickfosh
|
||||||
|
|
||||||
> Stockfish, but worse :)
|
> [Stockfish](https://stockfishchess.org), 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 (that shows nothing, but is useful for debugging the AIs) 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 stickfosh
|
||||||
|
```
|
||||||
|
1. Run the program:
|
||||||
|
```sh
|
||||||
|
./stickfosh
|
||||||
|
```
|
||||||
|
|
||||||
|
## Running the Application
|
||||||
|
|
||||||
|
Stickfosh provides multiple execution modes, selectable via command-line arguments:
|
||||||
|
|
||||||
|
| Mode | Description | Example Command |
|
||||||
|
|------|------------|----------------|
|
||||||
|
| **Human vs AI** | Play against the AI | `./stickfosh --mode human_vs_ai` |
|
||||||
|
| **AI vs AI** | Watch two AI versions compete | `./stickfosh --mode ai_vs_ai --ai1 v3_AB_ordering --ai2 v6_iterative_deepening` |
|
||||||
|
| **Human vs Human** | Manually input moves for both sides | `./stickfosh --mode human_vs_human` |
|
||||||
|
| **Perft Testing** | Performance test move generation | `./stickfosh --mode perft` |
|
||||||
|
| **Custom FEN** | Start from a custom position | `./stickfosh --mode ai_vs_ai --fen "rnbqkb1r/pppppppp/8/8/8/8/PPPPPPPP/RNBQKB1R w KQkq - 0 1"` |
|
||||||
|
|
||||||
|
## Video Demo
|
||||||
|
|
||||||
|
<!-- [](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.
|
||||||
|
BIN
res/stickfosh.gif
Normal file
BIN
res/stickfosh.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.8 MiB |
85
src/main.cpp
85
src/main.cpp
@ -9,36 +9,73 @@
|
|||||||
#include "view/view.hpp"
|
#include "view/view.hpp"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void print_usage() {
|
||||||
|
std::cout << "Usage: chess_ai [OPTIONS]\n";
|
||||||
|
std::cout << "Options:\n";
|
||||||
|
std::cout
|
||||||
|
<< " --mode <human_vs_ai|ai_vs_ai|human_vs_human|perft> Choose the "
|
||||||
|
"game mode.\n";
|
||||||
|
std::cout << " --ai1 <version> Choose the first AI version (for ai_vs_ai "
|
||||||
|
"mode).\n";
|
||||||
|
std::cout << " --ai2 <version> Choose the second AI version (for "
|
||||||
|
"ai_vs_ai mode).\n";
|
||||||
|
std::cout << " --fen <FEN_STRING> Set a custom FEN position.\n";
|
||||||
|
std::cout << " --help Show this help message.\n";
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
// std::string pos =
|
std::string mode = "human_vs_ai";
|
||||||
// "r2qkb1r/2p1pppp/p1n1b3/1p6/B2P4/2P1P3/P4PPP/R1BQK1NR w KQkq - 0 9 ";
|
std::string ai1_version = "v0_random";
|
||||||
std::string pos = "3r4/3r4/3k4/8/3K4/8/8/8 w - - 0 1";
|
std::string ai2_version = "v6_iterative_deepening";
|
||||||
|
std::string fen =
|
||||||
|
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||||
|
|
||||||
// pos for ai timing<
|
for (int i = 1; i < argc; ++i) {
|
||||||
// std::string pos =
|
std::string arg = argv[i];
|
||||||
// "r3k2r/p1ppqpb1/Bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPB1PPP/R3K2R b KQkq - 0
|
if (arg == "--mode" && i + 1 < argc) {
|
||||||
// 3";
|
mode = argv[++i];
|
||||||
|
} else if (arg == "--ai1" && i + 1 < argc) {
|
||||||
Board b = Board::setup_fen_position(pos);
|
ai1_version = argv[++i];
|
||||||
|
} else if (arg == "--ai2" && i + 1 < argc) {
|
||||||
ai::v0_random p1(true, std::chrono::milliseconds(1000));
|
ai2_version = argv[++i];
|
||||||
// ai::v1_pure_minimax p2(false, std::chrono::milliseconds(20000));
|
} else if (arg == "--fen" && i + 1 < argc) {
|
||||||
// ai::v2_alpha_beta p2(false, std::chrono::milliseconds(20000));
|
fen = argv[++i];
|
||||||
// ai::v3_AB_ordering p2(false, std::chrono::milliseconds(20000));
|
} else if (arg == "--help") {
|
||||||
// ai::v4_search_captures p2(false, std::chrono::milliseconds(20000));
|
print_usage();
|
||||||
// ai::v5_better_endgame p2(false, std::chrono::milliseconds(20000));
|
return 0;
|
||||||
ai::v6_iterative_deepening p2(false, std::chrono::milliseconds(2000));
|
} else {
|
||||||
|
std::cerr << "Unknown option: " << arg << "\n";
|
||||||
|
print_usage();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Board board = Board::setup_fen_position(fen);
|
||||||
GUI gui;
|
GUI gui;
|
||||||
// NoOpView gui;
|
Controller* controller = nullptr;
|
||||||
// AIvsAIController manual(b, gui, p1, p2);
|
|
||||||
HumanVsAIController manual(b, gui, p2);
|
|
||||||
|
|
||||||
Controller& controller = manual;
|
if (mode == "human_vs_ai") {
|
||||||
|
ai::v6_iterative_deepening ai(false, std::chrono::milliseconds(2000));
|
||||||
|
controller = new HumanVsAIController(board, gui, ai);
|
||||||
|
} else if (mode == "ai_vs_ai") {
|
||||||
|
ai::v0_random p1(true, std::chrono::milliseconds(1000));
|
||||||
|
ai::v6_iterative_deepening p2(false, std::chrono::milliseconds(2000));
|
||||||
|
controller = new AIvsAIController(board, gui, p1, p2);
|
||||||
|
} else if (mode == "human_vs_human") {
|
||||||
|
controller = new ManualController(board, gui);
|
||||||
|
} else if (mode == "perft") {
|
||||||
|
perft();
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
std::cerr << "Invalid mode selected!\n";
|
||||||
|
print_usage();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
controller.start();
|
controller->start();
|
||||||
|
delete controller;
|
||||||
// perft();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,10 @@
|
|||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
static long int position_counter = 0;
|
||||||
|
|
||||||
Move ai::AI::search(const Board& b) {
|
Move ai::AI::search(const Board& b) {
|
||||||
|
position_counter = 0;
|
||||||
Move result;
|
Move result;
|
||||||
|
|
||||||
std::condition_variable cv;
|
std::condition_variable cv;
|
||||||
@ -47,6 +50,13 @@ Move ai::AI::search(const Board& b) {
|
|||||||
// Ensure timer thread is also stopped
|
// Ensure timer thread is also stopped
|
||||||
timer_thread.join();
|
timer_thread.join();
|
||||||
|
|
||||||
std::cout << "Took " << elapsed << " ms" << std::endl;
|
std::cout << "Took " << elapsed << " ms, " << "Looked at "
|
||||||
|
<< position_counter << " positions" << std::endl;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int ai::AI::eval(const Board& b) {
|
||||||
|
int ret = _eval(b);
|
||||||
|
position_counter++;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
@ -20,8 +20,9 @@ namespace ai {
|
|||||||
std::atomic<bool> stop_computation = false;
|
std::atomic<bool> stop_computation = false;
|
||||||
|
|
||||||
Move search(const Board& b);
|
Move search(const Board& b);
|
||||||
|
int eval(const Board&);
|
||||||
|
|
||||||
virtual int eval(const Board&) = 0;
|
virtual int _eval(const Board&) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct v0_random : public AI {
|
struct v0_random : public AI {
|
||||||
@ -29,7 +30,7 @@ namespace ai {
|
|||||||
|
|
||||||
Move _search(const Board&) override;
|
Move _search(const Board&) override;
|
||||||
|
|
||||||
int eval(const Board&) override {
|
int _eval(const Board&) override {
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -41,7 +42,7 @@ namespace ai {
|
|||||||
v1_pure_minimax(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
|
v1_pure_minimax(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
|
||||||
|
|
||||||
Move _search(const Board&) override;
|
Move _search(const Board&) override;
|
||||||
int eval(const Board&) override;
|
int _eval(const Board&) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class v2_alpha_beta : public AI {
|
class v2_alpha_beta : public AI {
|
||||||
@ -52,7 +53,7 @@ namespace ai {
|
|||||||
v2_alpha_beta(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
|
v2_alpha_beta(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
|
||||||
|
|
||||||
virtual Move _search(const Board&) override;
|
virtual Move _search(const Board&) override;
|
||||||
virtual int eval(const Board&) override;
|
virtual int _eval(const Board&) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class v3_AB_ordering : public AI {
|
class v3_AB_ordering : public AI {
|
||||||
@ -63,7 +64,7 @@ namespace ai {
|
|||||||
v3_AB_ordering(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
|
v3_AB_ordering(bool w, std::chrono::milliseconds tt): AI(w, tt) {}
|
||||||
|
|
||||||
virtual Move _search(const Board&) override;
|
virtual Move _search(const Board&) override;
|
||||||
virtual int eval(const Board&) override;
|
virtual int _eval(const Board&) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class v4_search_captures : public v3_AB_ordering {
|
class v4_search_captures : public v3_AB_ordering {
|
||||||
@ -86,7 +87,7 @@ namespace ai {
|
|||||||
v5_better_endgame(bool w, std::chrono::milliseconds tt)
|
v5_better_endgame(bool w, std::chrono::milliseconds tt)
|
||||||
: v4_search_captures(w, tt) {}
|
: v4_search_captures(w, tt) {}
|
||||||
|
|
||||||
virtual int eval(const Board&) override;
|
virtual int _eval(const Board&) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class v6_iterative_deepening : public v5_better_endgame {
|
class v6_iterative_deepening : public v5_better_endgame {
|
||||||
|
@ -7,10 +7,7 @@
|
|||||||
|
|
||||||
#define MULTITHREADED 1
|
#define MULTITHREADED 1
|
||||||
|
|
||||||
static int position_counter = 0;
|
|
||||||
|
|
||||||
Move ai::v1_pure_minimax::_search(const Board& b) {
|
Move ai::v1_pure_minimax::_search(const Board& b) {
|
||||||
position_counter = 0;
|
|
||||||
std::vector<Move> moves = b.all_legal_moves();
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
|
|
||||||
Move best_move;
|
Move best_move;
|
||||||
@ -52,7 +49,6 @@ Move ai::v1_pure_minimax::_search(const Board& b) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
std::cout << "Looked at " << position_counter << " positions" << std::endl;
|
|
||||||
return best_move;
|
return best_move;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,8 +74,7 @@ int ai::v1_pure_minimax::_search(const Board& b, int depth) {
|
|||||||
return best_evaluation;
|
return best_evaluation;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai::v1_pure_minimax::eval(const Board& b) {
|
int ai::v1_pure_minimax::_eval(const Board& b) {
|
||||||
position_counter++;
|
|
||||||
int white_eval = count_material(b, Colour::White);
|
int white_eval = count_material(b, Colour::White);
|
||||||
int black_eval = count_material(b, Colour::Black);
|
int black_eval = count_material(b, Colour::Black);
|
||||||
|
|
||||||
|
@ -7,11 +7,7 @@
|
|||||||
|
|
||||||
#define MULTITHREADED 1
|
#define MULTITHREADED 1
|
||||||
|
|
||||||
|
|
||||||
static int position_counter = 0;
|
|
||||||
|
|
||||||
Move ai::v2_alpha_beta::_search(const Board& b) {
|
Move ai::v2_alpha_beta::_search(const Board& b) {
|
||||||
position_counter = 0;
|
|
||||||
std::vector<Move> moves = b.all_legal_moves();
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
|
|
||||||
Move best_move;
|
Move best_move;
|
||||||
@ -53,7 +49,6 @@ Move ai::v2_alpha_beta::_search(const Board& b) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
std::cout << "Looked at " << position_counter << " positions" << std::endl;
|
|
||||||
return best_move;
|
return best_move;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,8 +74,7 @@ int ai::v2_alpha_beta::_search(const Board& b, int depth, int alpha, int beta) {
|
|||||||
return alpha;
|
return alpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai::v2_alpha_beta::eval(const Board& b) {
|
int ai::v2_alpha_beta::_eval(const Board& b) {
|
||||||
position_counter++;
|
|
||||||
int white_eval = count_material(b, Colour::White);
|
int white_eval = count_material(b, Colour::White);
|
||||||
int black_eval = count_material(b, Colour::Black);
|
int black_eval = count_material(b, Colour::Black);
|
||||||
|
|
||||||
|
@ -8,12 +8,14 @@
|
|||||||
|
|
||||||
#define MULTITHREADED 1
|
#define MULTITHREADED 1
|
||||||
|
|
||||||
|
|
||||||
static int position_counter;
|
|
||||||
|
|
||||||
Move ai::v3_AB_ordering::_search(const Board& b) {
|
Move ai::v3_AB_ordering::_search(const Board& b) {
|
||||||
position_counter = 0;
|
|
||||||
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;
|
||||||
@ -56,7 +58,6 @@ Move ai::v3_AB_ordering::_search(const Board& b) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
std::cout << "Looked at " << position_counter << " positions" << std::endl;
|
|
||||||
return best_move;
|
return best_move;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +75,10 @@ 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) {
|
||||||
return m1.score_guess(b) > m2.score_guess(b);
|
int score = m1.score_guess(b) - m2.score_guess(b);
|
||||||
|
if (!am_white)
|
||||||
|
score *= -1;
|
||||||
|
return score < 0;
|
||||||
});
|
});
|
||||||
|
|
||||||
Move best_move;
|
Move best_move;
|
||||||
@ -88,8 +92,7 @@ int ai::v3_AB_ordering::_ab_search(
|
|||||||
return alpha;
|
return alpha;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai::v3_AB_ordering::eval(const Board& b) {
|
int ai::v3_AB_ordering::_eval(const Board& b) {
|
||||||
position_counter++;
|
|
||||||
int white_eval = count_material(b, Colour::White);
|
int white_eval = count_material(b, Colour::White);
|
||||||
int black_eval = count_material(b, Colour::Black);
|
int black_eval = count_material(b, Colour::Black);
|
||||||
|
|
||||||
|
@ -6,9 +6,6 @@
|
|||||||
|
|
||||||
#define MULTITHREADED 1
|
#define MULTITHREADED 1
|
||||||
|
|
||||||
|
|
||||||
static int position_counter;
|
|
||||||
|
|
||||||
int ai::v4_search_captures::_ab_search(
|
int ai::v4_search_captures::_ab_search(
|
||||||
const Board& b, int depth, int alpha, int beta
|
const Board& b, int depth, int alpha, int beta
|
||||||
) {
|
) {
|
||||||
|
@ -36,8 +36,8 @@ static float endgame_phase_weight(int material_count_no_pawns) {
|
|||||||
return 1.f - std::min(1.f, material_count_no_pawns * multiplier);
|
return 1.f - std::min(1.f, material_count_no_pawns * multiplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai::v5_better_endgame::eval(const Board& b) {
|
int ai::v5_better_endgame::_eval(const Board& b) {
|
||||||
int old_eval = v4_search_captures::eval(b);
|
int old_eval = v4_search_captures::_eval(b);
|
||||||
Colour attacking_colour = b.white_to_play ? White : Black;
|
Colour attacking_colour = b.white_to_play ? White : Black;
|
||||||
Colour defending_colour = b.white_to_play ? Black : White;
|
Colour defending_colour = b.white_to_play ? Black : White;
|
||||||
return old_eval
|
return old_eval
|
||||||
|
@ -13,7 +13,8 @@ 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;
|
||||||
for (int depth = 1; !stop_computation; depth++) {
|
int 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(
|
||||||
@ -36,5 +37,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -24,3 +24,31 @@ 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;
|
||||||
|
}
|
||||||
|
@ -15,6 +15,8 @@ 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)
|
||||||
|
13
tests/move.cpp
Normal file
13
tests/move.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#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());
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user