So I played many games online where I am winning but then lose on time. Sometimes the game is drawn (when there is no sequence of legal moves that would produce checkmate) and sometimes I lose (when there is such a sequence). The decision the website takes happens instantly. I am wondering, how do they determine whether the game is a draw or not? If they use brute force, then the decision would take longer to arrive at.
2 Answers
As far as I know, every chess website simply looks at the opponents material to determine if they have mating material or not. It doesn't matter even if mate is possible.
The ruleset you are referring to are the FIDE rules, and for simplicity chess websites do not use these rules because the percentage of positions where self-mate is possible is too low.
This comes from python-chess:
def is_insufficient_material(self):
"""Checks for a draw due to insufficient mating material."""
# Enough material to mate.
if self.pawns or self.rooks or self.queens:
return False
# A single knight or a single bishop.
if popcount(self.occupied) <= 3:
return True
# More than a single knight.
if self.knights:
return False
# All bishops on the same color.
if self.bishops & BB_DARK_SQUARES == 0:
return True
elif self.bishops & BB_LIGHT_SQUARES == 0:
return True
else:
return False
-
2
if self.knights or self.rooks or self.queens or self.kings: return False
- It is not at all obvious what this line is doing. I mean, I would ordinarily assume that it's checking whether you have one of the listed pieces - but then it would return false if you had a king, which makes no sense at all.– D MJun 30, 2018 at 15:40 -
2That doesn't seem to make sense either; it returns an immediate
False
if the king has a legal move?– D MJun 30, 2018 at 19:58 -
2Not sure where you got that source file, but that
or self.kings
isn't there anymore in the current version of python-chess. And those variables are bitmasks showing where each piece is, so it just means "if there's any king on the board". Jul 1, 2018 at 11:16 -
1@FedericoPoloni yeah, I think I took the wrong chess variant, not the original chess. thank you for the correction, replaced the code with the current version!– lenikJul 1, 2018 at 11:42
-
1Thank you, can you please explain what this code does? I have no knowledge of python at all. Jul 3, 2018 at 16:06