1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-29 16:23:09 +00:00

Cleanup and optimize Position::has_mate_threat()

There is a functional change because we now skip
more moves and because do_move() / undo_move() is
well known to be not reversible we end up with a
change in node count, although there is actually
no change but a bit speed up.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2010-08-09 11:45:02 +01:00
parent f26e0fec64
commit 252537fd9c
3 changed files with 40 additions and 43 deletions

View file

@ -1733,82 +1733,79 @@ bool Position::is_draw() const {
bool Position::is_mate() const { bool Position::is_mate() const {
MoveStack moves[256]; MoveStack moves[256];
return is_check() && (generate_moves(*this, moves, false) == moves); return is_check() && (generate_moves(*this, moves) == moves);
} }
/// Position::has_mate_threat() tests whether a given color has a mate in one /// Position::has_mate_threat() tests whether the side to move is under
/// from the current position. /// a threat of being mated in one from the current position.
bool Position::has_mate_threat(Color c) { bool Position::has_mate_threat() {
MoveStack mlist[256], *last, *cur;
StateInfo st1, st2; StateInfo st1, st2;
Color stm = side_to_move(); bool mateFound = false;
// If we are under check it's up to evasions to do the job
if (is_check()) if (is_check())
return false; return false;
// If the input color is not equal to the side to move, do a null move // First pass the move to our opponent doing a null move
if (c != stm)
do_null_move(st1); do_null_move(st1);
MoveStack mlist[120]; // Then generate pseudo-legal moves that give check
bool result = false; last = generate_non_capture_checks(*this, mlist);
Bitboard pinned = pinned_pieces(sideToMove);
// Generate pseudo-legal non-capture and capture check moves
MoveStack* last = generate_non_capture_checks(*this, mlist);
last = generate_captures(*this, last); last = generate_captures(*this, last);
// Loop through the moves, and see if one of them is mate // Loop through the moves, and see if one of them gives mate
for (MoveStack* cur = mlist; cur != last; cur++) Bitboard pinned = pinned_pieces(sideToMove);
CheckInfo ci(*this);
for (cur = mlist; cur != last && !mateFound; cur++)
{ {
Move move = cur->move; Move move = cur->move;
if (!pl_move_is_legal(move, pinned)) if ( !pl_move_is_legal(move, pinned)
|| !move_is_check(move, ci))
continue; continue;
do_move(move, st2); do_move(move, st2, ci, true);
if (is_mate()) if (is_mate())
result = true; mateFound = true;
undo_move(move); undo_move(move);
} }
// Undo null move, if necessary
if (c != stm)
undo_null_move(); undo_null_move();
return mateFound;
return result;
} }
/// Position::init_zobrist() is a static member function which initializes the /// Position::init_zobrist() is a static member function which initializes at
/// various arrays used to compute hash keys. /// startup the various arrays used to compute hash keys.
void Position::init_zobrist() { void Position::init_zobrist() {
for (int i = 0; i < 2; i++) int i,j, k;
for (int j = 0; j < 8; j++)
for (int k = 0; k < 64; k++) for (i = 0; i < 2; i++) for (j = 0; j < 8; j++) for (k = 0; k < 64; k++)
zobrist[i][j][k] = Key(genrand_int64()); zobrist[i][j][k] = Key(genrand_int64());
for (int i = 0; i < 64; i++) for (i = 0; i < 64; i++)
zobEp[i] = Key(genrand_int64()); zobEp[i] = Key(genrand_int64());
for (int i = 0; i < 16; i++) for (i = 0; i < 16; i++)
zobCastle[i] = genrand_int64(); zobCastle[i] = Key(genrand_int64());
zobSideToMove = genrand_int64(); zobSideToMove = Key(genrand_int64());
zobExclusion = genrand_int64(); zobExclusion = Key(genrand_int64());
} }
/// Position::init_piece_square_tables() initializes the piece square tables. /// Position::init_piece_square_tables() initializes the piece square tables.
/// This is a two-step operation: /// This is a two-step operation: First, the white halves of the tables are
/// First, the white halves of the tables are /// copied from the MgPST[][] and EgPST[][] arrays. Second, the black halves
/// copied from the MgPST[][] and EgPST[][] arrays. /// of the tables are initialized by mirroring and changing the sign of the
/// Second, the black halves of the tables are initialized by mirroring /// corresponding white scores.
/// and changing the sign of the corresponding white scores.
void Position::init_piece_square_tables() { void Position::init_piece_square_tables() {

View file

@ -264,8 +264,8 @@ public:
bool is_mate() const; bool is_mate() const;
bool is_draw() const; bool is_draw() const;
// Check if one side threatens a mate in one // Check if side to move could be mated in one
bool has_mate_threat(Color c); bool has_mate_threat();
// Number of plies since the last non-reversible move // Number of plies since the last non-reversible move
int rule_50_counter() const; int rule_50_counter() const;

View file

@ -1168,7 +1168,7 @@ namespace {
// Expensive mate threat detection (only for PV nodes) // Expensive mate threat detection (only for PV nodes)
if (PvNode) if (PvNode)
mateThreat = pos.has_mate_threat(opposite_color(pos.side_to_move())); mateThreat = pos.has_mate_threat();
// Initialize a MovePicker object for the current position // Initialize a MovePicker object for the current position
MovePicker mp = MovePicker(pos, ttMove, depth, H, ss, (PvNode ? -VALUE_INFINITE : beta)); MovePicker mp = MovePicker(pos, ttMove, depth, H, ss, (PvNode ? -VALUE_INFINITE : beta));