Compare commits

..

No commits in common. "a47dc9b695bf59014d1af71b129370e67137c86f" and "c55645ad7307473daa7c75d274d0b52cab95f7e7" have entirely different histories.

8 changed files with 54 additions and 135 deletions

View File

@ -182,9 +182,7 @@ Board Board::make_move(Move move) const {
ret.squares[move.target_square] = this->squares[move.source_square]; ret.squares[move.target_square] = this->squares[move.source_square];
// -- Handle en passant target being eaten // -- Handle en passant target being eaten
if (en_passant_target != -1 if (move.en_passant)
&& (squares[move.source_square] & 0b111) == Piece::Pawn
&& squares[move.target_square] == Piece::None)
ret.squares[move.target_square + (white_to_play ? -8 : 8)] = ret.squares[move.target_square + (white_to_play ? -8 : 8)] =
Piece::None; Piece::None;
@ -205,26 +203,23 @@ Board Board::make_move(Move move) const {
// -- Handle castling (just move the rook over) // -- Handle castling (just move the rook over)
Coords c = Coords::from_index(move.source_square); Coords c = Coords::from_index(move.source_square);
if ((squares[move.source_square] & 0b111) == Piece::King) { if (move.castle_side & KingSide) {
if (move.target_square - move.source_square == 2) { // king side castle
Coords rook_source{7, c.y}; Coords rook_source{7, c.y};
int8_t old_rook = ret.squares[rook_source.to_index()]; int8_t old_rook = ret.squares[rook_source.to_index()];
ret.squares[rook_source.to_index()] = Piece::None; ret.squares[rook_source.to_index()] = Piece::None;
Coords rook_dest{5, c.y}; Coords rook_dest{5, c.y};
ret.squares[rook_dest.to_index()] = old_rook; ret.squares[rook_dest.to_index()] = old_rook;
} else if (move.target_square - move.source_square == -2) { // queen } else if (move.castle_side & QueenSide) {
Coords rook_source{0, c.y}; Coords rook_source{0, c.y};
int8_t old_rook = ret.squares[rook_source.to_index()]; int8_t old_rook = ret.squares[rook_source.to_index()];
ret.squares[rook_source.to_index()] = Piece::None; ret.squares[rook_source.to_index()] = Piece::None;
Coords rook_dest{3, c.y}; Coords rook_dest{3, c.y};
ret.squares[rook_dest.to_index()] = old_rook; ret.squares[rook_dest.to_index()] = old_rook;
} }
}
// -- Check for castling rights // -- Check for castling rights
ret.w_castle_rights = w_castle_rights; ret.w_castle_rights = w_castle_rights;
ret.b_castle_rights = b_castle_rights; ret.b_castle_rights = b_castle_rights;
bool is_capturing = squares[move.target_square] != Piece::None;
if (white_to_play) { if (white_to_play) {
if ((squares[move.source_square] & 0b111) == King) if ((squares[move.source_square] & 0b111) == King)
ret.w_castle_rights = NeitherSide; ret.w_castle_rights = NeitherSide;
@ -237,7 +232,7 @@ Board Board::make_move(Move move) const {
} }
Coords target = Coords::from_index(move.target_square); Coords target = Coords::from_index(move.target_square);
if (is_capturing && target.y == 7 if (move.is_capturing && target.y == 7
&& (squares[move.target_square] & 0b111) == Rook) { && (squares[move.target_square] & 0b111) == Rook) {
if (target.x == 0 && (ret.b_castle_rights & QueenSide)) if (target.x == 0 && (ret.b_castle_rights & QueenSide))
ret.b_castle_rights &= ~(QueenSide); ret.b_castle_rights &= ~(QueenSide);
@ -256,7 +251,7 @@ Board Board::make_move(Move move) const {
} }
Coords target = Coords::from_index(move.target_square); Coords target = Coords::from_index(move.target_square);
if (is_capturing && target.y == 0 if (move.is_capturing && target.y == 0
&& (squares[move.target_square] & 0b111) == Rook) { && (squares[move.target_square] & 0b111) == Rook) {
if (target.x == 0 && (ret.w_castle_rights & QueenSide)) if (target.x == 0 && (ret.w_castle_rights & QueenSide))
ret.w_castle_rights &= ~(QueenSide); ret.w_castle_rights &= ~(QueenSide);

View File

@ -5,7 +5,7 @@
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
std::string pos = std::string pos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; "r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 5";
// Board b = Board::setup_fen_position(pos); // Board b = Board::setup_fen_position(pos);
perft(); perft();
return 0; return 0;

View File

@ -11,6 +11,9 @@ struct Move {
int8_t source_square; int8_t source_square;
int8_t target_square; int8_t target_square;
bool is_capturing = false;
CastleSide castle_side = CastleSide::NeitherSide;
bool en_passant = false;
int8_t promoting_to = Piece::None; int8_t promoting_to = Piece::None;
std::string to_string() const { std::string to_string() const {

View File

@ -60,6 +60,7 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
ret.push_back(Move{ ret.push_back(Move{
xy.to_index(), xy.to_index(),
Coords{6, xy.y}.to_index(), Coords{6, xy.y}.to_index(),
.castle_side = CastleSide::KingSide
}); });
} }
@ -67,6 +68,7 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
ret.push_back(Move{ ret.push_back(Move{
xy.to_index(), xy.to_index(),
Coords{2, xy.y}.to_index(), Coords{2, xy.y}.to_index(),
.castle_side = CastleSide::QueenSide
}); });
} }

View File

@ -21,6 +21,7 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
ret.push_back(Move{ ret.push_back(Move{
xy.to_index(), xy.to_index(),
left.to_index(), left.to_index(),
.is_capturing = true,
.promoting_to = (int8_t) (my_colour | piece) .promoting_to = (int8_t) (my_colour | piece)
}); });
else else
@ -42,6 +43,7 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
ret.push_back(Move{ ret.push_back(Move{
xy.to_index(), xy.to_index(),
right.to_index(), right.to_index(),
.is_capturing = true,
.promoting_to = (int8_t) (my_colour | piece) .promoting_to = (int8_t) (my_colour | piece)
}); });
else else
@ -53,8 +55,14 @@ std::vector<Move> pawn_moves(const Board& b, const Coords xy) {
if (b.en_passant_target != -1) { if (b.en_passant_target != -1) {
Coords c = Coords::from_index(b.en_passant_target); Coords c = Coords::from_index(b.en_passant_target);
int dy = my_colour == Colour::White ? 1 : -1; int dy = my_colour == Colour::White ? 1 : -1;
if (c.y == xy.y + dy && (c.x == xy.x - 1 || c.x == xy.x + 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()}); ret.push_back(Move{
xy.to_index(),
c.to_index(),
.is_capturing = true,
.en_passant = true
});
}
} }
// -- Normal move + promotion // -- Normal move + promotion

View File

@ -53,11 +53,18 @@ legal_moves(int8_t p, const Board& b, const Coords xy, bool looking_for_check) {
std::optional<Move> std::optional<Move>
move_for_position(const Board& board, const Coords source, const Coords dest) { 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 {}; return {};
int8_t piece = board.squares[dest.to_index()];
if (piece == Piece::None)
return Move{source.to_index(), dest.to_index()}; 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 {};
} }
std::vector<Move> std::vector<Move>
@ -67,11 +74,11 @@ look_direction(const Board& board, const Coords xy, int mult_dx, int mult_dy) {
int dx = mult_dx * d; int dx = mult_dx * d;
int dy = mult_dy * d; int dy = mult_dy * d;
Coords target{xy.x + dx, xy.y + dy}; std::optional<Move> move =
std::optional<Move> move = move_for_position(board, xy, target); move_for_position(board, xy, Coords{xy.x + dx, xy.y + dy});
if (move.has_value()) { if (move.has_value()) {
ret.push_back(move.value()); ret.push_back(move.value());
if (board.squares[target.to_index()] != Piece::None) if (move.value().is_capturing)
break; break;
} else { } else {
break; break;

View File

@ -2,7 +2,6 @@
#include "board.hpp" #include "board.hpp"
#include "move.hpp" #include "move.hpp"
#include "threadpool.hpp"
#include <chrono> #include <chrono>
#include <map> #include <map>
@ -103,9 +102,7 @@ static std::map<std::string, std::map<int, int>> pos2expected{
static std::stringstream res; static std::stringstream res;
int move_generation_test( int move_generation_test(Board& b, int depth, int max_depth) {
const Board& b, int depth, int max_depth, ThreadPool& pool
) {
if (depth == max_depth) { if (depth == max_depth) {
res.str(""); res.str("");
res.clear(); res.clear();
@ -121,34 +118,15 @@ int move_generation_test(
return moves.size(); return moves.size();
int num_pos = 0; 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) { for (const Move& move : moves) {
Board tmp_board = b.make_move(move); Board tmp_board = b.make_move(move);
futures.push_back(pool.enqueue( // std::cout << ">" << move << std::endl;
move_generation_test, int n = move_generation_test(tmp_board, depth - 1, max_depth);
tmp_board, // std::cout << "<" << move << std::endl;
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) if (depth == max_depth)
res << move << ": " << n << std::endl; res << move << ": " << n << std::endl;
num_pos += n; num_pos += n;
} }
}
return num_pos; return num_pos;
} }
@ -161,12 +139,11 @@ void perft(std::string pos) {
std::cout << pos << std::endl; std::cout << pos << std::endl;
std::map<int, int> expected = pos2expected[pos]; std::map<int, int> expected = pos2expected[pos];
Board b = Board::setup_fen_position(pos); Board b = Board::setup_fen_position(pos);
ThreadPool pool(std::thread::hardware_concurrency());
for (const auto& [depth, expected_n_moves] : expected) { for (const auto& [depth, expected_n_moves] : expected) {
std::cout << "Depth: " << depth << " " << std::flush; std::cout << "Depth: " << depth << " " << std::flush;
auto start = std::chrono::steady_clock::now(); auto start = std::chrono::steady_clock::now();
int moves = move_generation_test(b, depth, depth, pool); int moves = move_generation_test(b, depth, depth);
auto end = std::chrono::steady_clock::now(); auto end = std::chrono::steady_clock::now();
auto elapsed = auto elapsed =
std::chrono::duration_cast<std::chrono::milliseconds>(end - start) std::chrono::duration_cast<std::chrono::milliseconds>(end - start)

View File

@ -1,73 +0,0 @@
#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;
};