Compare commits
6 Commits
a47dc9b695
...
e821814cd1
Author | SHA1 | Date | |
---|---|---|---|
|
e821814cd1 | ||
|
2899820bcc | ||
|
6fd9b15261 | ||
|
97b3eeb984 | ||
|
982f933698 | ||
|
d21fcde21c |
23
cpp/src/ais/ai.hpp
Normal file
23
cpp/src/ais/ai.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "../board.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
#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 {
|
||||
struct AI {
|
||||
virtual std::string search(std::string, int) = 0;
|
||||
virtual int minimax(const Board&, int) = 0;
|
||||
virtual int eval(const Board&) = 0;
|
||||
};
|
||||
|
||||
DECLARE_AI(v0_random)
|
||||
DECLARE_AI(v1_simple)
|
||||
} // namespace ai
|
8
cpp/src/ais/v0_random.cpp
Normal file
8
cpp/src/ais/v0_random.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include "ai.hpp"
|
||||
|
||||
std::string ai::v1_simple::search(std::string pos, int depth) {
|
||||
Board b = Board::setup_fen_position(pos);
|
||||
std::vector<Move> moves = b.all_legal_moves();
|
||||
|
||||
return moves[rand() % moves.size()].to_string();
|
||||
}
|
97
cpp/src/ais/v1_simple.cpp
Normal file
97
cpp/src/ais/v1_simple.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
#include "../pieces/piece.hpp"
|
||||
#include "../threadpool.hpp"
|
||||
#include "ai.hpp"
|
||||
|
||||
#include <future>
|
||||
#include <map>
|
||||
|
||||
static int INFINITY = std::numeric_limits<int>::max();
|
||||
|
||||
std::string ai::v1_simple::search(std::string pos, int depth) {
|
||||
Board b = Board::setup_fen_position(pos);
|
||||
|
||||
ThreadPool pool(std::thread::hardware_concurrency());
|
||||
|
||||
std::vector<Move> moves = b.all_legal_moves();
|
||||
std::map<std::string, std::future<int>> futures;
|
||||
for (const Move& move : moves) {
|
||||
Board tmp_board = b.make_move(move);
|
||||
futures.insert(
|
||||
{move.to_string(), pool.enqueue([this, tmp_board, depth]() {
|
||||
return minimax(tmp_board, depth - 1);
|
||||
})}
|
||||
);
|
||||
}
|
||||
|
||||
std::string best_move;
|
||||
int best_eval = -INFINITY;
|
||||
for (auto& [move, future] : futures) {
|
||||
int eval = future.get();
|
||||
if (eval > best_eval) {
|
||||
best_eval = eval;
|
||||
best_move = move;
|
||||
}
|
||||
}
|
||||
|
||||
return best_move;
|
||||
}
|
||||
|
||||
int ai::v1_simple::minimax(const Board& b, int depth) {
|
||||
if (b.is_checkmate_for(b.white_to_play ? White : Black))
|
||||
return -INFINITY;
|
||||
|
||||
if (depth == 0)
|
||||
return eval(b);
|
||||
|
||||
std::vector<Move> moves = b.all_legal_moves();
|
||||
int best_evaluation = 0;
|
||||
Move best_move;
|
||||
for (const Move& move : moves) {
|
||||
Board tmp_board = b.make_move(move);
|
||||
int tmp_eval = -minimax(tmp_board, depth - 1);
|
||||
best_evaluation = std::max(best_evaluation, tmp_eval);
|
||||
}
|
||||
return best_evaluation;
|
||||
}
|
||||
|
||||
static int PawnValue = 100;
|
||||
static int KnightValue = 300;
|
||||
static int BishopValue = 320;
|
||||
static int RookValue = 500;
|
||||
static int QueenValue = 900;
|
||||
|
||||
int count_material(const Board& b, int8_t colour) {
|
||||
int ret = 0;
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (b.colour_at(i) == colour)
|
||||
switch (b.squares[i] & 0b111) {
|
||||
case Piece::Pawn:
|
||||
ret += PawnValue;
|
||||
break;
|
||||
case Piece::Knigt:
|
||||
ret += KnightValue;
|
||||
break;
|
||||
case Piece::Bishop:
|
||||
ret += BishopValue;
|
||||
break;
|
||||
case Piece::Rook:
|
||||
ret += RookValue;
|
||||
break;
|
||||
case Piece::Queen:
|
||||
ret += QueenValue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ai::v1_simple::eval(const Board& b) {
|
||||
int white_eval = count_material(b, Colour::White);
|
||||
int black_eval = count_material(b, Colour::Black);
|
||||
|
||||
int evaluation = white_eval - black_eval;
|
||||
|
||||
int perspective = b.white_to_play ? 1 : -1;
|
||||
|
||||
return perspective * evaluation;
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
@ -1,12 +1,14 @@
|
||||
#include "board.hpp"
|
||||
#include "stickfosh.hpp"
|
||||
#include "ais/ai.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::string pos =
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
// Board b = Board::setup_fen_position(pos);
|
||||
perft();
|
||||
|
||||
ai::v1_simple ai;
|
||||
|
||||
std::string move = ai.search(pos, 4);
|
||||
std::cout << move << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
#include "stickfosh.hpp"
|
||||
#include "perft.hpp"
|
||||
|
||||
#include "board.hpp"
|
||||
#include "move.hpp"
|
Loading…
x
Reference in New Issue
Block a user