committing to stash and compare

This commit is contained in:
Karma Riuk 2025-02-07 15:08:53 +01:00
parent 86beb9cc85
commit d28008d913
7 changed files with 105 additions and 64 deletions

View File

@ -31,8 +31,8 @@ int main(int argc, char* argv[]) {
Controller& controller = manual;
controller.start();
// controller.start();
// perft(pos);
perft();
return 0;
}

View File

@ -94,10 +94,10 @@ Board Board::setup_fen_position(std::string fen) {
index = fen.find(' ', index) + 1;
board.n_full_moves = std::stoi(fen.substr(index));
board.w_check = board.is_check_for(White);
board.b_check = board.is_check_for(Black);
board.w_nlm = board.no_legal_moves_for(White);
board.b_nlm = board.no_legal_moves_for(Black);
board.w_check = board._is_check_for(White);
board.b_check = board._is_check_for(Black);
board.w_nlm = board._no_legal_moves_for(White);
board.b_nlm = board._no_legal_moves_for(Black);
return board;
}
@ -175,7 +175,7 @@ std::string Board::to_fen() const {
return ret;
}
Board Board::make_move(Move move) const {
Board Board::make_move(Move move, bool recurse_call) const {
Board ret;
std::copy(
std::begin(this->squares),
@ -278,6 +278,17 @@ Board Board::make_move(Move move) const {
if (!white_to_play)
ret.n_full_moves = n_full_moves + 1;
if (ret.n_half_moves > 20) {
std::cerr << "too many recursions" << std::endl;
exit(1);
}
if (recurse_call) {
ret.w_check = ret._is_check_for(White);
ret.b_check = ret._is_check_for(Black);
ret.w_nlm = ret._no_legal_moves_for(White);
ret.b_nlm = ret._no_legal_moves_for(Black);
}
return ret;
}
@ -314,7 +325,7 @@ int8_t Board::get_king_of(int8_t colour) const {
throw std::domain_error(ss.str());
}
bool Board::is_check_for(int8_t colour) const {
bool Board::_is_check_for(Colour colour) const {
int8_t king_idx = this->get_king_of(colour);
std::vector<Move> all_moves;
all_moves.reserve(50);
@ -343,15 +354,23 @@ bool Board::is_check_for(int8_t colour) const {
all_moves.insert(all_moves.end(), moves.begin(), moves.end());
}
for (const Move& move : all_moves)
if (move.target_square == king_idx)
for (const Move& move : all_moves) {
// if (colour == White
// && move.target_square
// == Coords::from_algebraic("b6").to_index())
// std::cout << "am here" << std::endl;
if (move.target_square == king_idx) {
std::cout << "indeed check for " << to_string(colour)
<< std::endl;
return true;
}
}
all_moves.clear();
}
return false;
}
bool Board::no_legal_moves_for(int8_t colour) const {
bool Board::_no_legal_moves_for(Colour colour) const {
for (int i = 0; i < 64; i++) {
if (colour_at(i) == colour) {
std::vector<Move> moves =
@ -363,6 +382,28 @@ bool Board::no_legal_moves_for(int8_t colour) const {
return true;
}
bool Board::no_legal_moves_for(int8_t colour) const {
return colour == White ? w_nlm : b_nlm;
}
bool Board::is_check_for(int8_t colour) const {
return colour == White ? w_check : b_check;
}
bool Board::is_checkmate_for(Colour colour) const {
return no_legal_moves_for(colour) && is_check_for(colour);
}
bool Board::is_stalemate_for(Colour colour) const {
return no_legal_moves_for(colour) && !is_check_for(colour);
}
bool Board::is_terminal() const {
return n_half_moves == 100 || insufficient_material() || white_to_play
? is_checkmate_for(White) || is_stalemate_for(White)
: is_checkmate_for(Black) || is_stalemate_for(Black);
}
std::vector<Move> Board::all_legal_moves() const {
std::vector<Move> ret;
for (int i = 0; i < 64; i++) {

View File

@ -9,7 +9,8 @@
struct Board {
private:
int8_t get_king_of(int8_t) const;
bool no_legal_moves_for(int8_t) const;
bool _no_legal_moves_for(Colour) const;
bool _is_check_for(Colour) const;
bool w_nlm = false, b_nlm = false, w_check = false, b_check = false;
public:
@ -23,8 +24,9 @@ struct Board {
static Board setup_fen_position(std::string fen);
Board make_move(Move) const;
Board make_move(Move, bool = true) const;
std::string to_fen() const;
bool no_legal_moves_for(int8_t) const;
bool is_check_for(int8_t) const;
bool insufficient_material_for(Colour) const;
@ -35,25 +37,11 @@ struct Board {
std::vector<Move> all_legal_moves() const;
bool is_checkmate_for(int8_t colour) const {
if (colour == White)
return w_nlm && w_check;
return b_nlm && b_check;
// return no_legal_moves_for(colour) && is_check_for(colour);
}
bool is_checkmate_for(Colour) const;
bool is_stalemate_for(int8_t colour) const {
if (colour == White)
return w_nlm && !w_check;
return b_nlm && !b_check;
// return no_legal_moves_for(colour) && !is_check_for(colour);
}
bool is_stalemate_for(Colour) const;
bool is_terminal() const {
return n_half_moves == 100 || insufficient_material() || white_to_play
? is_checkmate_for(White) || is_stalemate_for(White)
: is_checkmate_for(Black) || is_stalemate_for(Black);
}
bool is_terminal() const;
inline Piece piece_at(int8_t idx) const {
return (Piece) (squares[idx] & 0b00111);

View File

@ -119,43 +119,45 @@ int move_generation_test(
res.clear();
}
if (b.is_terminal())
return 0;
if (b.is_checkmate_for(b.white_to_play ? White : Black)
|| b.is_stalemate_for(b.white_to_play ? White : Black))
return 1;
if (depth == 0)
return 1;
std::vector<Move> moves = b.all_legal_moves();
if (depth == 1)
return moves.size();
// 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 {
// 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) {
std::cout << "Looking at " << move << std::endl;
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;
}

View File

@ -10,7 +10,7 @@ static bool is_clear_king_side(const Board& b, const Coords xy) {
return false;
std::optional<Move> move = move_for_position(b, xy, c);
Board board_after_move = b.make_move(move.value());
Board board_after_move = b.make_move(move.value(), false);
if (board_after_move.is_check_for(b.colour_at(xy)))
return false;
}
@ -24,7 +24,7 @@ static bool is_clear_queen_side(const Board& b, const Coords xy) {
return false;
std::optional<Move> move = move_for_position(b, xy, c);
Board board_after_move = b.make_move(move.value());
Board board_after_move = b.make_move(move.value(), false);
if (dx < 3 && board_after_move.is_check_for(b.colour_at(xy)))
return false;
}
@ -46,8 +46,11 @@ std::vector<Move> king_moves(const Board& b, const Coords xy) {
}
}
if (b.is_check_for(b.colour_at(xy)))
if (b.is_check_for(b.colour_at(xy))) {
std::cout << b.to_fen() << std::endl;
std::cout << "Check for " << to_string(b.colour_at(xy)) << std::endl;
return keep_only_blocking(ret, b);
}
// -- Castles
int8_t castling_rights = b.colour_at(xy) == Colour::White

View File

@ -12,9 +12,11 @@ keep_only_blocking(const std::vector<Move> candidates, const Board& board) {
Colour my_colour = board.colour_at(candidates[0].source_square);
std::vector<Move> ret;
for (Move move : candidates) {
Board board_after_move = board.make_move(move);
Board board_after_move = board.make_move(move, false);
if (!board_after_move.is_check_for(my_colour))
ret.push_back(move);
else
std::cout << "removing " << move << std::endl;
}
return ret;
}

View File

@ -50,7 +50,12 @@ inline const char* to_string(Piece c) {
}
}
inline std::ostream& operator<<(std::ostream& os, const int8_t& i) {
inline std::ostream& operator<<(std::ostream& os, const Colour& i) {
os << std::to_string(i);
return os;
}
inline std::ostream& operator<<(std::ostream& os, const Piece& i) {
os << std::to_string(i);
return os;
}