I am programming a tablebase for fairy chess, and would like to conclude the game if mate cannot be reached in any way. I guess this has to be detected when the evaluation (win in... / loss in... / draw) is created. So far, my (working) algorithm for evaluating winnable positions looks like this in pseudocode:
- Start with all mate positions and evaluate them
- For each position which has changed evaluation, make one move backwards
- Evaluate the resulting positions by checking all forward moves and pick the best result (unexplored = draw)
- If the "best" result has changed, add the position to the queue for next iteration
- Return to (2)
activePositions = findAllMatePositions()
for(ap in activePositions) ap.result=[Loss,0]
while(activePositions not empty)
{
newActivePositions = []
for (ap in activePositions)
{
for(backmove in ap.getBackMoves())
{
newPosition = ap.makeBackMove(backmove)
for(move in newPosition.getMoves())
{
// find the best move and evaluation of resulting position
}
if(/*result of newPosition has changed */) newActivePositions.push(newPosition)
ap.undoBackMove(backmove)
}
}
activePositions = newActivePositions
}
Ideally, I would like for the dead position be detected during the main evaluation, but I guess I would need to run it again. Basically the issues are:
- Where to start? Should I need to pick all positions which were not evaluated with win or loss, for the first iteration?
- Would it be easier if I first went for white cannot lose / black cannot lose detection?
Edit: So to determine draw by both sides, it is rather easy:
- Mark all drawn positions as dead candidates
- For each position, if any of the moves does not lead to dead position candidates, remove it from the candidates as well
- Continue until no candidates are removed in each iteration
So now only remains (the harder, but not as useful for me) solution how to determine this for a player individually (cannot be helpmated by the other player, but can mate the opponent).