added the check for insufficient material

This commit is contained in:
Karma Riuk 2025-02-06 22:34:48 +01:00
parent fced9757c2
commit 6e567f2f11
2 changed files with 29 additions and 1 deletions

View File

@ -5,6 +5,7 @@
#include "../utils/move.hpp" #include "../utils/move.hpp"
#include "../utils/utils.hpp" #include "../utils/utils.hpp"
#include <SFML/Graphics/BlendMode.hpp>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <map> #include <map>
@ -269,6 +270,28 @@ Board Board::make_move(Move move) const {
return ret; return ret;
} }
bool Board::insufficient_material_for(Colour current_colour) const {
int n_bishop = 0, n_knight = 0;
for (int i = 0; i < 64; i++) {
Colour colour = colour_at(i);
if (colour != current_colour)
continue;
Piece piece = piece_at(i);
if (piece == Piece::Pawn || piece == Piece::Queen
|| piece == Piece::Rook)
return false;
if (piece == Piece::Bishop)
n_bishop++;
if (piece == Piece::Knigt && colour == Colour::White)
n_knight++;
}
return (n_bishop == 0 && n_knight == 0) || (n_bishop == 1 && n_knight == 0)
|| (n_bishop == 0 && n_knight == 1);
}
int8_t Board::get_king_of(int8_t colour) const { int8_t Board::get_king_of(int8_t colour) const {
for (int i = 0; i < 64; i++) for (int i = 0; i < 64; i++)
if (squares[i] == (colour | Piece::King)) if (squares[i] == (colour | Piece::King))

View File

@ -25,7 +25,12 @@ struct Board {
Board make_move(Move) const; Board make_move(Move) const;
std::string to_fen() const; std::string to_fen() const;
bool is_check_for(int8_t) const; bool is_check_for(int8_t) const;
bool insufficient_material() const; bool insufficient_material_for(Colour) const;
bool insufficient_material() const {
return insufficient_material_for(White)
&& insufficient_material_for(Black);
};
std::vector<Move> all_legal_moves() const; std::vector<Move> all_legal_moves() const;