Compare commits
66 Commits
Author | SHA1 | Date | |
---|---|---|---|
e821814cd1 | |||
2899820bcc | |||
6fd9b15261 | |||
97b3eeb984 | |||
982f933698 | |||
d21fcde21c | |||
a47dc9b695 | |||
e8ca1db8d6 | |||
73b2a5b51b | |||
3f3017a389 | |||
f768d16a7e | |||
a658673d2b | |||
222adbb8f0 | |||
c55645ad73 | |||
1b0af3b486 | |||
df20060075 | |||
151a50fba9 | |||
609d5f8c98 | |||
e9c96e83da | |||
76310400e8 | |||
e5bb2d1a5f | |||
4c86d5e815 | |||
b1812f1df6 | |||
9d21141d3c | |||
974af5d19f | |||
df07b4399d | |||
fa59784230 | |||
be34045f92 | |||
4eaed699c0 | |||
310abb0611 | |||
b70ca5302a | |||
83af5f0b97 | |||
72ba6a80ae | |||
1723356f62 | |||
75adb4b1ba | |||
6951b860da | |||
805d9fa95c | |||
4df7cbf01a | |||
00137e022a | |||
570b8df4a7 | |||
9a66a71c38 | |||
41e4119468 | |||
e7fc1b06de | |||
5f79b81ce4 | |||
e5819ee83b | |||
58109c5120 | |||
6478176a7d | |||
ab088da33c | |||
31799cb0ec | |||
96dcbc0a6c | |||
ce06097063 | |||
3fc2aa73ad | |||
de6a03ce08 | |||
dc3031ddc1 | |||
5e4b880aad | |||
540ffa4fb3 | |||
21136a26f0 | |||
fa57dcfc30 | |||
8da349579d | |||
f0467bc516 | |||
a7cf8e0c21 | |||
2128159914 | |||
c564add509 | |||
31b0656332 | |||
24165bb5bb | |||
4c8fdfa3b4 |
@ -1,3 +1,5 @@
|
||||
CXXFLAGS += -O3 -Wall
|
||||
|
||||
# Add .d to Make's recognized suffixes.
|
||||
SUFFIXES += .d
|
||||
|
||||
|
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;
|
||||
}
|
@ -169,8 +169,6 @@ std::string Board::to_fen() const {
|
||||
}
|
||||
|
||||
Board Board::make_move(Move move) const {
|
||||
int8_t dest_piece = this->squares[move.target_square];
|
||||
|
||||
Board ret;
|
||||
std::copy(
|
||||
std::begin(this->squares),
|
||||
@ -184,14 +182,95 @@ Board Board::make_move(Move move) const {
|
||||
ret.squares[move.target_square] = this->squares[move.source_square];
|
||||
|
||||
// -- Handle en passant target being eaten
|
||||
if (move.en_passant)
|
||||
ret.squares[move.target_square - 8] = Piece::None;
|
||||
if (en_passant_target != -1
|
||||
&& (squares[move.source_square] & 0b111) == Piece::Pawn
|
||||
&& squares[move.target_square] == Piece::None)
|
||||
ret.squares[move.target_square + (white_to_play ? -8 : 8)] =
|
||||
Piece::None;
|
||||
|
||||
// -- Handle promotion
|
||||
if (move.promoting_to != Piece::None)
|
||||
ret.squares[move.target_square] = move.promoting_to;
|
||||
|
||||
// -- Set en passant target if need
|
||||
if ((squares[move.source_square] & 0b111) == Piece::Pawn
|
||||
&& std::abs(move.target_square - move.source_square) == 16) {
|
||||
if (white_to_play)
|
||||
ret.en_passant_target = move.target_square - 8;
|
||||
else
|
||||
ret.en_passant_target = move.target_square + 8;
|
||||
} else {
|
||||
ret.en_passant_target = -1;
|
||||
}
|
||||
|
||||
// -- Handle castling (just move the rook over)
|
||||
Coords c = Coords::from_index(move.source_square);
|
||||
if ((squares[move.source_square] & 0b111) == Piece::King) {
|
||||
if (move.target_square - move.source_square == 2) { // king side castle
|
||||
Coords rook_source{7, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{5, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
} else if (move.target_square - move.source_square == -2) { // queen
|
||||
Coords rook_source{0, c.y};
|
||||
int8_t old_rook = ret.squares[rook_source.to_index()];
|
||||
ret.squares[rook_source.to_index()] = Piece::None;
|
||||
Coords rook_dest{3, c.y};
|
||||
ret.squares[rook_dest.to_index()] = old_rook;
|
||||
}
|
||||
}
|
||||
|
||||
// -- Check for castling rights
|
||||
ret.w_castle_rights = w_castle_rights;
|
||||
ret.b_castle_rights = b_castle_rights;
|
||||
bool is_capturing = squares[move.target_square] != Piece::None;
|
||||
if (white_to_play) {
|
||||
if ((squares[move.source_square] & 0b111) == King)
|
||||
ret.w_castle_rights = NeitherSide;
|
||||
|
||||
if ((squares[move.source_square] & 0b111) == Rook) {
|
||||
if (c.x == 0 && (ret.w_castle_rights & QueenSide))
|
||||
ret.w_castle_rights &= ~(QueenSide);
|
||||
if (c.x == 7 && (ret.w_castle_rights & KingSide))
|
||||
ret.w_castle_rights &= ~(KingSide);
|
||||
}
|
||||
|
||||
Coords target = Coords::from_index(move.target_square);
|
||||
if (is_capturing && target.y == 7
|
||||
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||
if (target.x == 0 && (ret.b_castle_rights & QueenSide))
|
||||
ret.b_castle_rights &= ~(QueenSide);
|
||||
if (target.x == 7 && (ret.b_castle_rights & KingSide))
|
||||
ret.b_castle_rights &= ~(KingSide);
|
||||
}
|
||||
} else {
|
||||
if ((squares[move.source_square] & 0b111) == King)
|
||||
ret.b_castle_rights = NeitherSide;
|
||||
|
||||
if ((squares[move.source_square] & 0b111) == Rook) {
|
||||
if (c.x == 0 && (ret.b_castle_rights & QueenSide))
|
||||
ret.b_castle_rights &= ~(QueenSide);
|
||||
if (c.x == 7 && (ret.b_castle_rights & KingSide))
|
||||
ret.b_castle_rights &= ~(KingSide);
|
||||
}
|
||||
|
||||
Coords target = Coords::from_index(move.target_square);
|
||||
if (is_capturing && target.y == 0
|
||||
&& (squares[move.target_square] & 0b111) == Rook) {
|
||||
if (target.x == 0 && (ret.w_castle_rights & QueenSide))
|
||||
ret.w_castle_rights &= ~(QueenSide);
|
||||
if (target.x == 7 && (ret.w_castle_rights & KingSide))
|
||||
ret.w_castle_rights &= ~(KingSide);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int8_t Board::get_king_of(int8_t colour) const {
|
||||
for (int i = 0; i < 64; i++)
|
||||
if (this->squares[i] == (colour | Piece::King))
|
||||
if (squares[i] == (colour | Piece::King))
|
||||
return i;
|
||||
throw std::domain_error(
|
||||
"Apparently there no kings of the such color in this board"
|
||||
@ -208,13 +287,57 @@ std::vector<int8_t> to_target_square(std::vector<Move> moves) {
|
||||
bool Board::is_check_for(int8_t colour) const {
|
||||
int8_t king_idx = this->get_king_of(colour);
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (this->squares[i] == Piece::None)
|
||||
if (this->squares[i] == Piece::None || colour_at(i) == colour)
|
||||
continue;
|
||||
std::vector<Move> moves =
|
||||
legal_moves(this->squares[i], *this, Coords::from_index(i), true);
|
||||
std::vector<int8_t> targets = to_target_square(moves);
|
||||
if (std::find(targets.begin(), targets.end(), i) != targets.end())
|
||||
std::vector<int8_t> targets;
|
||||
if ((squares[i] & 0b00111) == King) {
|
||||
// special case for the king, because it creates infinite recursion
|
||||
// (since he looks if he's walking into a check)
|
||||
Coords king_pos = Coords::from_index(i);
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dy = -1; dy <= 1; dy++) {
|
||||
Coords c{king_pos.x + dx, king_pos.y + dy};
|
||||
if (c.is_within_bounds())
|
||||
targets.push_back(c.to_index());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
std::vector<Move> moves = legal_moves(
|
||||
this->squares[i],
|
||||
*this,
|
||||
Coords::from_index(i),
|
||||
true
|
||||
);
|
||||
targets = to_target_square(moves);
|
||||
}
|
||||
if (std::find(targets.begin(), targets.end(), king_idx)
|
||||
!= targets.end())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Board::no_legal_moves_for(int8_t colour) const {
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if (colour_at(i) == colour) {
|
||||
std::vector<Move> moves =
|
||||
legal_moves(squares[i], *this, Coords::from_index(i));
|
||||
if (moves.size() > 0)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<Move> Board::all_legal_moves() const {
|
||||
std::vector<Move> ret;
|
||||
for (int i = 0; i < 64; i++) {
|
||||
if ((colour_at(i) == White && white_to_play)
|
||||
|| (colour_at(i) == Black && !white_to_play)) {
|
||||
std::vector<Move> moves =
|
||||
legal_moves(squares[i], *this, Coords::from_index(i));
|
||||
ret.insert(ret.end(), moves.begin(), moves.end());
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
@ -8,7 +8,8 @@
|
||||
|
||||
struct Board {
|
||||
private:
|
||||
int8_t get_king_of(int8_t colour) const;
|
||||
int8_t get_king_of(int8_t) const;
|
||||
bool no_legal_moves_for(int8_t) const;
|
||||
|
||||
public:
|
||||
int8_t squares[64] = {Piece::None};
|
||||
@ -25,6 +26,21 @@ struct Board {
|
||||
std::string to_fen() const;
|
||||
bool is_check_for(int8_t) const;
|
||||
|
||||
std::vector<Move> all_legal_moves() const;
|
||||
|
||||
bool is_checkmate_for(int8_t colour) const {
|
||||
return is_check_for(colour) && no_legal_moves_for(colour);
|
||||
}
|
||||
|
||||
bool is_stalemate_for(int8_t colour) const {
|
||||
return !is_check_for(colour) && no_legal_moves_for(colour);
|
||||
}
|
||||
|
||||
bool is_terminal() const {
|
||||
return is_checkmate_for(White) || is_checkmate_for(Black)
|
||||
|| is_stalemate_for(White) || is_stalemate_for(Black);
|
||||
}
|
||||
|
||||
int8_t colour_at(int8_t idx) const {
|
||||
return squares[idx] & 0b11000;
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
@ -18,6 +17,8 @@ struct Coords {
|
||||
}
|
||||
|
||||
static Coords from_index(int idx) {
|
||||
if (idx < 0 || idx > 63)
|
||||
throw std::invalid_argument("The index is outside the board...");
|
||||
return {idx % 8, idx / 8};
|
||||
}
|
||||
|
||||
@ -27,17 +28,17 @@ struct Coords {
|
||||
"An algebraic coordinate should only have two characters"
|
||||
);
|
||||
|
||||
int x = _FILES.find(pos[0]);
|
||||
size_t x = _FILES.find(pos[0]);
|
||||
if (x == std::string::npos)
|
||||
throw std::invalid_argument("The first character of the given "
|
||||
"algebraic coordinate is invalid");
|
||||
|
||||
int y = _RANKS.find(pos[1]);
|
||||
size_t y = _RANKS.find(pos[1]);
|
||||
if (y == std::string::npos)
|
||||
throw std::invalid_argument("The second character of the given "
|
||||
"algebraic coordinate is invalid");
|
||||
|
||||
return Coords{x, y};
|
||||
return Coords{(int) x, (int) y};
|
||||
}
|
||||
|
||||
std::string to_algebraic() const {
|
||||
@ -52,7 +53,7 @@ struct Coords {
|
||||
}
|
||||
|
||||
bool is_within_bounds() const {
|
||||
return this->to_index() < 64;
|
||||
return 0 <= x && x < 8 && 0 <= y && y < 8;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1,11 +1,14 @@
|
||||
#include "board.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);
|
||||
sizeof(b);
|
||||
|
||||
ai::v1_simple ai;
|
||||
|
||||
std::string move = ai.search(pos, 4);
|
||||
std::cout << move << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,15 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include "castle_side.hpp"
|
||||
#include "coords.hpp"
|
||||
#include "pieces/piece.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <sstream>
|
||||
|
||||
struct Move {
|
||||
int8_t source_square;
|
||||
int8_t target_square;
|
||||
|
||||
bool is_capturing = false;
|
||||
CastleSide castle_side = CastleSide::NeitherSide;
|
||||
bool en_passant = false;
|
||||
int8_t promoting_to = 0;
|
||||
int8_t promoting_to = Piece::None;
|
||||
|
||||
std::string to_string() const {
|
||||
std::stringstream ss;
|
||||
ss << Coords::from_index(source_square)
|
||||
<< Coords::from_index(target_square);
|
||||
if (promoting_to != Piece::None) {
|
||||
switch (promoting_to & 0b00111) {
|
||||
case Queen:
|
||||
ss << 'q';
|
||||
break;
|
||||
case Bishop:
|
||||
ss << 'b';
|
||||
break;
|
||||
case Knigt:
|
||||
ss << 'n';
|
||||
break;
|
||||
case Rook:
|
||||
ss << 'r';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ss.str();
|
||||
}
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Move& m) {
|
||||
os << m.to_string();
|
||||
return os;
|
||||
}
|
||||
|
187
cpp/src/perft.cpp
Normal file
187
cpp/src/perft.cpp
Normal file
@ -0,0 +1,187 @@
|
||||
#include "perft.hpp"
|
||||
|
||||
#include "board.hpp"
|
||||
#include "move.hpp"
|
||||
#include "threadpool.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <map>
|
||||
#include <ostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static std::string tick = "\033[92m✔️\033[0m"; // Green Tick;
|
||||
static std::string cross = "\033[91m❌\033[0m"; // Red Cross
|
||||
|
||||
static std::map<std::string, std::map<int, int>> pos2expected{
|
||||
// -- Position 1
|
||||
{
|
||||
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
|
||||
{
|
||||
{1, 20}, // 0
|
||||
{2, 400}, // 1
|
||||
{3, 8902}, // 38
|
||||
{4, 197281}, // 971
|
||||
// {5, 4865609}, // 23032
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 2
|
||||
{
|
||||
"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 2",
|
||||
{
|
||||
{1, 48}, // 0
|
||||
{2, 2039}, // 16
|
||||
{3, 97862}, // 602
|
||||
// {4, 4085603}, // 26612
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 3
|
||||
{
|
||||
"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 3",
|
||||
{
|
||||
{1, 14}, // 0
|
||||
{2, 191}, // 1
|
||||
{3, 2812}, // 11
|
||||
{4, 43238}, // 157
|
||||
{5, 674624}, // 2199
|
||||
// {6, 11030083},
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 4a
|
||||
{
|
||||
"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 4",
|
||||
{
|
||||
{1, 6}, // 0
|
||||
{2, 264}, // 1
|
||||
{3, 9467}, // 69
|
||||
{4, 422333}, // 3085
|
||||
// {5, 15833292}, // 124452
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 4b
|
||||
{
|
||||
"r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5",
|
||||
{
|
||||
{1, 6}, // 0
|
||||
{2, 264}, // 2
|
||||
{3, 9467}, // 104
|
||||
{4, 422333}, // 3742
|
||||
// {5, 15833292}, // 136784
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 5
|
||||
{
|
||||
"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 6",
|
||||
{
|
||||
{1, 44}, // 0
|
||||
{2, 1486}, // 12
|
||||
{3, 62379}, // 357
|
||||
// {4, 2103487}, // 13804
|
||||
// {5, 89941194}, // 1230428
|
||||
},
|
||||
},
|
||||
|
||||
// -- Position 6
|
||||
{
|
||||
"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 "
|
||||
"7",
|
||||
{
|
||||
{1, 46}, // 0
|
||||
{2, 2079}, // 16
|
||||
{3, 89890}, // 602
|
||||
// {4, 3894594}, // 26612
|
||||
// {5, 164075551}, // 1230428
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
static std::stringstream res;
|
||||
|
||||
int move_generation_test(
|
||||
const Board& b, int depth, int max_depth, ThreadPool& pool
|
||||
) {
|
||||
if (depth == max_depth) {
|
||||
res.str("");
|
||||
res.clear();
|
||||
}
|
||||
|
||||
if (b.is_terminal())
|
||||
return 0;
|
||||
if (depth == 0)
|
||||
return 1;
|
||||
|
||||
std::vector<Move> moves = b.all_legal_moves();
|
||||
if (depth == 1)
|
||||
return moves.size();
|
||||
|
||||
int num_pos = 0;
|
||||
|
||||
if (depth == max_depth) {
|
||||
// Parallel execution at the top level
|
||||
std::vector<std::future<int>> futures;
|
||||
for (const Move& move : moves) {
|
||||
Board tmp_board = b.make_move(move);
|
||||
futures.push_back(pool.enqueue(
|
||||
move_generation_test,
|
||||
tmp_board,
|
||||
depth - 1,
|
||||
max_depth,
|
||||
std::ref(pool)
|
||||
));
|
||||
}
|
||||
|
||||
for (auto& future : futures)
|
||||
num_pos += future.get(); // Retrieve the result of each task
|
||||
} else {
|
||||
// Regular sequential execution
|
||||
for (const Move& move : moves) {
|
||||
Board tmp_board = b.make_move(move);
|
||||
int n = move_generation_test(tmp_board, depth - 1, max_depth, pool);
|
||||
if (depth == max_depth)
|
||||
res << move << ": " << n << std::endl;
|
||||
num_pos += n;
|
||||
}
|
||||
}
|
||||
|
||||
return num_pos;
|
||||
}
|
||||
|
||||
void perft() {
|
||||
for (auto& [key, value] : pos2expected)
|
||||
perft(key);
|
||||
}
|
||||
|
||||
void perft(std::string pos) {
|
||||
std::cout << pos << std::endl;
|
||||
std::map<int, int> expected = pos2expected[pos];
|
||||
Board b = Board::setup_fen_position(pos);
|
||||
ThreadPool pool(std::thread::hardware_concurrency());
|
||||
|
||||
for (const auto& [depth, expected_n_moves] : expected) {
|
||||
std::cout << "Depth: " << depth << " " << std::flush;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
int moves = move_generation_test(b, depth, depth, pool);
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
auto elapsed =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
|
||||
.count();
|
||||
|
||||
std::cout << "Results: " << moves << " ";
|
||||
if (moves == expected_n_moves)
|
||||
std::cout << tick << " ";
|
||||
else
|
||||
std::cout << cross << " (expected " << expected_n_moves << ") ";
|
||||
std::cout << "positions Time: " << elapsed << " milliseconds"
|
||||
<< std::endl;
|
||||
if (moves != expected_n_moves) {
|
||||
std::cout << std::endl << res.str();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
6
cpp/src/perft.hpp
Normal file
6
cpp/src/perft.hpp
Normal file
@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
void perft(std::string pos);
|
||||
void perft();
|
@ -46,7 +46,7 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
||||
}
|
||||
|
||||
if (b.is_check_for(b.colour_at(xy)))
|
||||
return ret;
|
||||
return keep_only_blocking(ret, b);
|
||||
|
||||
// -- Castles
|
||||
int8_t castling_rights = b.colour_at(xy) == Colour::White
|
||||
@ -60,7 +60,6 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
Coords{6, xy.y}.to_index(),
|
||||
.castle_side = CastleSide::KingSide
|
||||
});
|
||||
}
|
||||
|
||||
@ -68,7 +67,6 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
Coords{2, xy.y}.to_index(),
|
||||
.castle_side = CastleSide::QueenSide
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -14,7 +14,17 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
int8_t capturable_piece = b.squares[left.to_index()];
|
||||
if (capturable_piece != 0) {
|
||||
if (my_colour != b.colour_at(left))
|
||||
ret.push_back(Move{xy.to_index(), left.to_index()});
|
||||
if ((my_colour == White && left.y == 7)
|
||||
|| (my_colour == Black && left.y == 0))
|
||||
|
||||
for (auto piece : {Rook, Knigt, Bishop, Queen})
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
left.to_index(),
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
ret.push_back(Move{xy.to_index(), left.to_index()});
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,11 +35,29 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
int8_t capturable_piece = b.squares[right.to_index()];
|
||||
if (capturable_piece != 0) {
|
||||
if (my_colour != b.colour_at(right))
|
||||
ret.push_back(Move{xy.to_index(), right.to_index()});
|
||||
if ((my_colour == White && right.y == 7)
|
||||
|| (my_colour == Black && right.y == 0))
|
||||
|
||||
for (auto piece : {Rook, Knigt, Bishop, Queen})
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
right.to_index(),
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
ret.push_back(Move{xy.to_index(), right.to_index()});
|
||||
}
|
||||
}
|
||||
|
||||
// -- Promotion
|
||||
// -- Capture en passant
|
||||
if (b.en_passant_target != -1) {
|
||||
Coords c = Coords::from_index(b.en_passant_target);
|
||||
int dy = my_colour == Colour::White ? 1 : -1;
|
||||
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 1))
|
||||
ret.push_back(Move{xy.to_index(), c.to_index()});
|
||||
}
|
||||
|
||||
// -- Normal move + promotion
|
||||
bool is_on_starting_rank =
|
||||
my_colour == Colour::White ? xy.y == 1 : xy.y == 6;
|
||||
int max_dy = is_on_starting_rank ? 3 : 2;
|
||||
@ -39,12 +67,17 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
|
||||
if (b.squares[new_xy.to_index()] != Piece::None)
|
||||
break;
|
||||
if (new_xy.y == 7 || new_xy.y == 0)
|
||||
for (auto piece : {Queen, Knigt, Bishop, Rook})
|
||||
for (auto piece : {Rook, Knigt, Bishop, Queen})
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
new_xy.to_index(),
|
||||
.promoting_to = piece
|
||||
.promoting_to = (int8_t) (my_colour | piece)
|
||||
});
|
||||
else
|
||||
ret.push_back(Move{
|
||||
xy.to_index(),
|
||||
new_xy.to_index(),
|
||||
});
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -18,14 +18,28 @@ keep_only_blocking(const std::vector<Move> candidates, const Board& board) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::vector<Move> legal_moves(
|
||||
int8_t p, const Board& b, const Coords xy, bool looking_for_check = false
|
||||
) {
|
||||
std::vector<Move>
|
||||
legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) {
|
||||
std::vector<Move> ret;
|
||||
switch (p) {
|
||||
int8_t simple_piece = p & 0b00111;
|
||||
switch (simple_piece) {
|
||||
case Piece::Pawn:
|
||||
ret = pawn_moves(b, xy);
|
||||
break;
|
||||
case Piece::Bishop:
|
||||
ret = bishop_moves(b, xy);
|
||||
break;
|
||||
case Piece::Rook:
|
||||
ret = rook_moves(b, xy);
|
||||
break;
|
||||
case Piece::Knigt:
|
||||
ret = knight_moves(b, xy);
|
||||
break;
|
||||
case Piece::Queen:
|
||||
ret = queen_moves(b, xy);
|
||||
break;
|
||||
case Piece::King:
|
||||
ret = king_moves(b, xy);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -34,38 +48,33 @@ std::vector<Move> legal_moves(
|
||||
if (!looking_for_check)
|
||||
return keep_only_blocking(ret, b);
|
||||
|
||||
return {};
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::optional<Move>
|
||||
move_for_position(const Board& board, const Coords source, const Coords dest) {
|
||||
if (dest.is_within_bounds())
|
||||
if (!dest.is_within_bounds()
|
||||
|| board.colour_at(source) == board.colour_at(dest))
|
||||
return {};
|
||||
|
||||
int8_t piece = board.squares[dest.to_index()];
|
||||
if (piece == Piece::None)
|
||||
return Move{source.to_index(), dest.to_index()};
|
||||
|
||||
int8_t source_colour = board.colour_at(source);
|
||||
int8_t dest_colour = board.colour_at(dest);
|
||||
if (source_colour != dest_colour)
|
||||
return Move{source.to_index(), dest.to_index(), true};
|
||||
return {};
|
||||
return Move{source.to_index(), dest.to_index()};
|
||||
}
|
||||
|
||||
std::vector<Move>
|
||||
look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
|
||||
std::vector<Move> ret;
|
||||
for (int d = 0; d < 8; d++) {
|
||||
for (int d = 1; d < 8; d++) {
|
||||
int dx = mult_dx * d;
|
||||
int dy = mult_dy * d;
|
||||
|
||||
std::optional<Move> move =
|
||||
move_for_position(board, xy, Coords{xy.x + dx, xy.y + dy});
|
||||
Coords target{xy.x + dx, xy.y + dy};
|
||||
std::optional<Move> move = move_for_position(board, xy, target);
|
||||
if (move.has_value()) {
|
||||
ret.push_back(move.value());
|
||||
if (move.value().is_capturing)
|
||||
if (board.squares[target.to_index()] != Piece::None)
|
||||
break;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
|
@ -1,19 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include "../move.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <vector>
|
||||
|
||||
enum Piece : int8_t {
|
||||
None = 0,
|
||||
King = 1,
|
||||
Pawn = 2,
|
||||
Knigt = 3,
|
||||
Bishop = 4,
|
||||
Rook = 5,
|
||||
Queen = 6,
|
||||
Rook = 1,
|
||||
Knigt = 2,
|
||||
Bishop = 3,
|
||||
Queen = 4,
|
||||
King = 5,
|
||||
Pawn = 6,
|
||||
};
|
||||
|
||||
enum Colour : int8_t {
|
||||
@ -21,10 +20,16 @@ enum Colour : int8_t {
|
||||
Black = 16,
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const int8_t& i) {
|
||||
os << std::to_string(i);
|
||||
return os;
|
||||
}
|
||||
|
||||
class Board;
|
||||
struct Coords;
|
||||
struct Move;
|
||||
|
||||
std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool);
|
||||
std::vector<Move> legal_moves(int8_t, const Board&, const Coords, bool = false);
|
||||
std::vector<Move> keep_only_blocking(const std::vector<Move>, const Board&);
|
||||
std::optional<Move> move_for_position(const Board&, const Coords, const Coords);
|
||||
std::vector<Move> look_direction(const Board&, const Coords, int, int);
|
||||
|
73
cpp/src/threadpool.hpp
Normal file
73
cpp/src/threadpool.hpp
Normal file
@ -0,0 +1,73 @@
|
||||
#pragma once
|
||||
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
class ThreadPool {
|
||||
public:
|
||||
ThreadPool(size_t numThreads) {
|
||||
for (size_t i = 0; i < numThreads; ++i) {
|
||||
workers.emplace_back([this] {
|
||||
while (true) {
|
||||
std::function<void()> task;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(queueMutex);
|
||||
condition.wait(lock, [this] {
|
||||
return stop || !tasks.empty();
|
||||
});
|
||||
if (stop && tasks.empty())
|
||||
return;
|
||||
task = std::move(tasks.front());
|
||||
tasks.pop();
|
||||
}
|
||||
task();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
template <class F, class... Args>
|
||||
auto enqueue(F&& f, Args&&... args)
|
||||
-> std::future<typename std::invoke_result<F, Args...>::type> {
|
||||
using return_type = typename std::invoke_result<F, Args...>::type;
|
||||
|
||||
auto task = std::make_shared<std::packaged_task<return_type()>>(
|
||||
std::bind(std::forward<F>(f), std::forward<Args>(args)...)
|
||||
);
|
||||
|
||||
std::future<return_type> res = task->get_future();
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(queueMutex);
|
||||
tasks.emplace([task]() { (*task)(); });
|
||||
}
|
||||
condition.notify_one();
|
||||
return res;
|
||||
}
|
||||
|
||||
void waitAll() {
|
||||
std::unique_lock<std::mutex> lock(queueMutex);
|
||||
condition.wait(lock, [this] { return tasks.empty(); });
|
||||
}
|
||||
|
||||
~ThreadPool() {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(queueMutex);
|
||||
stop = true;
|
||||
}
|
||||
condition.notify_all();
|
||||
for (std::thread& worker : workers)
|
||||
worker.join();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::thread> workers;
|
||||
std::queue<std::function<void()>> tasks;
|
||||
std::mutex queueMutex;
|
||||
std::condition_variable condition;
|
||||
bool stop = false;
|
||||
};
|
Reference in New Issue
Block a user