cleaned up the ais a bit
This commit is contained in:
parent
5f093907f3
commit
32c7832001
@ -2,22 +2,25 @@
|
|||||||
|
|
||||||
#include "../board.hpp"
|
#include "../board.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <atomic>
|
||||||
|
|
||||||
#define DECLARE_AI(x) \
|
|
||||||
struct x : public AI { \
|
|
||||||
std::string search(std::string, int) override; \
|
|
||||||
int minimax(const Board&, int) override; \
|
|
||||||
int eval(const Board&) override; \
|
|
||||||
};
|
|
||||||
|
|
||||||
namespace ai {
|
namespace ai {
|
||||||
struct AI {
|
struct AI {
|
||||||
virtual std::string search(std::string, int) = 0;
|
std::atomic<bool> stop_computation = false;
|
||||||
virtual int minimax(const Board&, int) = 0;
|
virtual Move search(const Board&, bool = false) = 0;
|
||||||
virtual int eval(const Board&) = 0;
|
virtual int eval(const Board&) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
DECLARE_AI(v0_random)
|
struct v0_random : public AI {
|
||||||
DECLARE_AI(v1_simple)
|
Move search(const Board&, bool) override;
|
||||||
|
// int eval(const Board&) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
class v1_simple : public AI {
|
||||||
|
int _search(const Board&, int);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Move search(const Board&, bool) override;
|
||||||
|
int eval(const Board&) override;
|
||||||
|
};
|
||||||
} // namespace ai
|
} // namespace ai
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
#include "ai.hpp"
|
#include "ai.hpp"
|
||||||
|
|
||||||
std::string ai::v1_simple::search(std::string pos, int depth) {
|
Move ai::v0_random::search(const Board& b, bool) {
|
||||||
Board b = Board::setup_fen_position(pos);
|
|
||||||
std::vector<Move> moves = b.all_legal_moves();
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
|
|
||||||
return moves[rand() % moves.size()].to_string();
|
return moves[rand() % moves.size()];
|
||||||
}
|
}
|
||||||
|
@ -7,21 +7,21 @@
|
|||||||
|
|
||||||
static int INFINITY = std::numeric_limits<int>::max();
|
static int INFINITY = std::numeric_limits<int>::max();
|
||||||
|
|
||||||
std::string ai::v1_simple::search(std::string pos, int depth) {
|
Move ai::v1_simple::search(const Board& b, bool am_black) {
|
||||||
Board b = Board::setup_fen_position(pos);
|
|
||||||
|
|
||||||
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();
|
||||||
std::map<std::string, std::future<int>> futures;
|
std::map<Move, std::future<int>> futures;
|
||||||
|
for (int 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({move.to_string(), pool.enqueue([&]() {
|
futures.insert({move, pool.enqueue([&]() {
|
||||||
return minimax(tmp_board, depth - 1);
|
return _search(tmp_board, depth - 1);
|
||||||
})});
|
})});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::string best_move;
|
Move best_move;
|
||||||
int best_eval = -INFINITY;
|
int best_eval = -INFINITY;
|
||||||
for (auto& [move, future] : futures) {
|
for (auto& [move, future] : futures) {
|
||||||
int eval = future.get();
|
int eval = future.get();
|
||||||
@ -34,11 +34,11 @@ std::string ai::v1_simple::search(std::string pos, int depth) {
|
|||||||
return best_move;
|
return best_move;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai::v1_simple::minimax(const Board& b, int depth) {
|
int ai::v1_simple::_search(const Board& b, int depth) {
|
||||||
if (b.is_checkmate_for(b.white_to_play ? White : Black))
|
if (b.is_checkmate_for(b.white_to_play ? White : Black))
|
||||||
return -INFINITY;
|
return -INFINITY;
|
||||||
|
|
||||||
if (depth == 0)
|
if (depth == 0 || stop_computation)
|
||||||
return eval(b);
|
return eval(b);
|
||||||
|
|
||||||
std::vector<Move> moves = b.all_legal_moves();
|
std::vector<Move> moves = b.all_legal_moves();
|
||||||
@ -46,7 +46,7 @@ int ai::v1_simple::minimax(const Board& b, int depth) {
|
|||||||
Move best_move;
|
Move best_move;
|
||||||
for (const Move& move : moves) {
|
for (const Move& move : moves) {
|
||||||
Board tmp_board = b.make_move(move);
|
Board tmp_board = b.make_move(move);
|
||||||
int tmp_eval = -minimax(tmp_board, depth - 1);
|
int tmp_eval = -_search(tmp_board, depth - 1);
|
||||||
best_evaluation = std::max(best_evaluation, tmp_eval);
|
best_evaluation = std::max(best_evaluation, tmp_eval);
|
||||||
}
|
}
|
||||||
return best_evaluation;
|
return best_evaluation;
|
||||||
@ -62,7 +62,7 @@ int count_material(const Board& b, int8_t colour) {
|
|||||||
int ret = 0;
|
int ret = 0;
|
||||||
for (int i = 0; i < 64; i++) {
|
for (int i = 0; i < 64; i++) {
|
||||||
if (b.colour_at(i) == colour)
|
if (b.colour_at(i) == colour)
|
||||||
switch (b.squares[i] & 0b111) {
|
switch (b.piece_at(i)) {
|
||||||
case Piece::Pawn:
|
case Piece::Pawn:
|
||||||
ret += PawnValue;
|
ret += PawnValue;
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user