Checkmate

A position is checkmate if it is check (i.e. a king could be captured if a pass is made on all present boards) and there are no legal  movesets. If the position is not check and there are no legal movesets, the result is a draw.

## Checkmate detection

In 2D chess, checkmate detection is easy since there are a bounded number of possible moves (a player has at most 16 pieces, each of which has at most 27 moves), so it's possible to iterate through all of them and test whether any of them escape check. There's at most one king, which makes it even easier, since it becomes obvious which moves might deal with the check.

In 5D chess, this becomes harder. If there are n active present timelines, each of which has m possible  moves, then there are n^m movesets to consider (more if branches can be created from more than one timeline, in which case order matters). Because of this, computer programs can take a very long time when trying to determine if a position is checkmate. The original release of the official client suffered from this problem, and the updated version incorrectly considers some positions to be checkmate (this is not known to have affected any real games). By carefully eliminating large parts of the search space simultaneously, it is possible to achieve practical performance alongside correctness.

#### Theoretical Complexity

For this section, we assume that the maximum board size is 8x8, and we are given a position with n timelines and at most m possible moves originating from each timeline. Assume we are interested in whether black has just won by checkmate (i.e. "is there a legal move for white?").

Although a naive algorithm for determining checkmate would test O(n!\*n^m) positions, the problem "Is a given a position checkmate?" may not be NP-hard. A sketch of the ideas can be seen in 2 simplified cases:

1. Assume that all playable boards are in the present and there are no branching moves. If a move results in an black piece being able to capture a king in a way that does not involve any other present boards, then that move can be eliminated from consideration. Taking that into account, the only pieces that can threaten a king on present board more than 8 boards away are rooks moving along the L-dimension and Queens moving as Rooks do. This allows for a linear time algorithm which proceeds through L from top to bottom while tracking the set of possible situations that can be achieved by some configuration of moves on higher boards. This can be done in linear time because we may consider situations equivalent if they have the same pattern of white rooks arriving and leaving through the L-dimension and the same pattern of white kings exposed to rooks along the L-dimension and black rooks threatening kings along the L-dimension. There are around 5^64 such patterns, which is a constant factor.
2. Assume that there is exactly one possible move from each timeline and that move is branching. The question then becomes whether there is an ordering of these new boards without any checks. If the tiles were unbounded in size, this would look a bit like the bounded Post correspondence Problem, but since they are bounded in size, there are finitely many distinct new boards (~13^64), so we can use dynamic programming to find which 'situations'(as above) can be reached using up to (x\_0, x\_1, ..., x\_(13^64)) boards of each type. This algorithm is O(n^(13^64)) but don't worry, we still need the constant factor of 5^64 as well.

Combining these and filling in the remaining details is left as an exercise to the reader. One case that may be deserving of extra attention is a triangle-shaped frontier of playable boards where bishops are no longer limited to 8 timelines and rooks can cause problems on the other side of the triangle.