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& controller = manual;
controller.start(); // controller.start();
// perft(pos); perft();
return 0; return 0;
} }

View File

@ -94,10 +94,10 @@ Board Board::setup_fen_position(std::string fen) {
index = fen.find(' ', index) + 1; index = fen.find(' ', index) + 1;
board.n_full_moves = std::stoi(fen.substr(index)); board.n_full_moves = std::stoi(fen.substr(index));
board.w_check = board.is_check_for(White); board.w_check = board._is_check_for(White);
board.b_check = board.is_check_for(Black); board.b_check = board._is_check_for(Black);
board.w_nlm = board.no_legal_moves_for(White); board.w_nlm = board._no_legal_moves_for(White);
board.b_nlm = board.no_legal_moves_for(Black); board.b_nlm = board._no_legal_moves_for(Black);
return board; return board;
} }
@ -175,7 +175,7 @@ std::string Board::to_fen() const {
return ret; return ret;
} }
Board Board::make_move(Move move) const { Board Board::make_move(Move move, bool recurse_call) const {
Board ret; Board ret;
std::copy( std::copy(
std::begin(this->squares), std::begin(this->squares),
@ -278,6 +278,17 @@ Board Board::make_move(Move move) const {
if (!white_to_play) if (!white_to_play)
ret.n_full_moves = n_full_moves + 1; 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; return ret;
} }
@ -314,7 +325,7 @@ int8_t Board::get_king_of(int8_t colour) const {
throw std::domain_error(ss.str()); 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); int8_t king_idx = this->get_king_of(colour);
std::vector<Move> all_moves; std::vector<Move> all_moves;
all_moves.reserve(50); 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()); all_moves.insert(all_moves.end(), moves.begin(), moves.end());
} }
for (const Move& move : all_moves) for (const Move& move : all_moves) {
if (move.target_square == king_idx) // 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; return true;
}
}
all_moves.clear(); all_moves.clear();
} }
return false; 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++) { for (int i = 0; i < 64; i++) {
if (colour_at(i) == colour) { if (colour_at(i) == colour) {
std::vector<Move> moves = std::vector<Move> moves =
@ -363,6 +382,28 @@ bool Board::no_legal_moves_for(int8_t colour) const {
return true; 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> Board::all_legal_moves() const {
std::vector<Move> ret; std::vector<Move> ret;
for (int i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {

View File

@ -9,7 +9,8 @@
struct Board { struct Board {
private: private:
int8_t get_king_of(int8_t) const; 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; bool w_nlm = false, b_nlm = false, w_check = false, b_check = false;
public: public:
@ -23,8 +24,9 @@ struct Board {
static Board setup_fen_position(std::string fen); 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; std::string to_fen() const;
bool no_legal_moves_for(int8_t) const;
bool is_check_for(int8_t) const; bool is_check_for(int8_t) const;
bool insufficient_material_for(Colour) const; bool insufficient_material_for(Colour) const;
@ -35,25 +37,11 @@ struct Board {
std::vector<Move> all_legal_moves() const; std::vector<Move> all_legal_moves() const;
bool is_checkmate_for(int8_t colour) const { bool is_checkmate_for(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(int8_t colour) const { bool is_stalemate_for(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_terminal() 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);
}
inline Piece piece_at(int8_t idx) const { inline Piece piece_at(int8_t idx) const {
return (Piece) (squares[idx] & 0b00111); return (Piece) (squares[idx] & 0b00111);

View File

@ -119,43 +119,45 @@ int move_generation_test(
res.clear(); res.clear();
} }
if (b.is_terminal()) if (b.is_checkmate_for(b.white_to_play ? White : Black)
return 0; || b.is_stalemate_for(b.white_to_play ? White : Black))
return 1;
if (depth == 0) if (depth == 0)
return 1; return 1;
std::vector<Move> moves = b.all_legal_moves(); std::vector<Move> moves = b.all_legal_moves();
if (depth == 1) // if (depth == 1)
return moves.size(); // return moves.size();
int num_pos = 0; int num_pos = 0;
if (depth == max_depth) { // if (depth == max_depth) {
// Parallel execution at the top level // // Parallel execution at the top level
std::vector<std::future<int>> futures; // 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( // futures.push_back(pool.enqueue(
move_generation_test, // move_generation_test,
tmp_board, // tmp_board,
depth - 1, // depth - 1,
max_depth, // max_depth,
std::ref(pool) // std::ref(pool)
)); // ));
} // }
//
for (auto& future : futures) // for (auto& future : futures)
num_pos += future.get(); // Retrieve the result of each task // num_pos += future.get(); // Retrieve the result of each task
} else { // } else {
// Regular sequential execution // Regular sequential execution
for (const Move& move : moves) { for (const Move& move : moves) {
Board tmp_board = b.make_move(move); std::cout << "Looking at " << move << std::endl;
int n = move_generation_test(tmp_board, depth - 1, max_depth, pool); Board tmp_board = b.make_move(move);
if (depth == max_depth) int n = move_generation_test(tmp_board, depth - 1, max_depth, pool);
res << move << ": " << n << std::endl; if (depth == max_depth)
num_pos += n; res << move << ": " << n << std::endl;
} num_pos += n;
} }
// }
return num_pos; return num_pos;
} }

View File

@ -10,7 +10,7 @@ static bool is_clear_king_side(const Board& b, const Coords xy) {
return false; return false;
std::optional<Move> move = move_for_position(b, xy, c); 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))) if (board_after_move.is_check_for(b.colour_at(xy)))
return false; return false;
} }
@ -24,7 +24,7 @@ static bool is_clear_queen_side(const Board& b, const Coords xy) {
return false; return false;
std::optional<Move> move = move_for_position(b, xy, c); 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))) if (dx < 3 && board_after_move.is_check_for(b.colour_at(xy)))
return false; 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); return keep_only_blocking(ret, b);
}
// -- Castles // -- Castles
int8_t castling_rights = b.colour_at(xy) == Colour::White 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); Colour my_colour = board.colour_at(candidates[0].source_square);
std::vector<Move> ret; std::vector<Move> ret;
for (Move move : candidates) { 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)) if (!board_after_move.is_check_for(my_colour))
ret.push_back(move); ret.push_back(move);
else
std::cout << "removing " << move << std::endl;
} }
return ret; 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); os << std::to_string(i);
return os; return os;
} }