0

OK, I have this chess app built with bitboard and I want to check if a given move put the opponent pieces in checkmate.

Verifying a check situation is easy. You build the bitmask of the enemy pieces' attack and you AND that with the bitmask of the opposite king, if the result is not zero, you have a check.

But what about check mate? A checkmate is something that will happen after the check. I mean, I move a piece, the app detects that the move generated a check. Then how do I know if this check is a check mate? Do I have to generate all possible bitboards for all possible plays of the opponent and check if there is a move that can remove the king from check? This not appears to be practical. Is there another way?

Duck
  • 34,902
  • 47
  • 248
  • 470
  • Why don't you think checking all of the possible moves by the opponent is practical? That's the definition of checkmate. – David Aug 11 '16 at 21:34
  • Some chess engines have a special `generateCheckEvasion()` function to avoid generating all moves when in check. – Arnauld Aug 11 '16 at 21:35
  • because that will generate thousands of combinations of movements including some pieces that are irrelevant for bringing the king out of check. – Duck Aug 11 '16 at 21:35
  • knowing that a function exists does not help. Knowing what it does, helps. – Duck Aug 11 '16 at 21:37
  • @SpaceDog - No. You only need to test all pseudo-legal moves in the current position and see if you're still in check. That's a 1-ply search. – Arnauld Aug 11 '16 at 21:37
  • what is a pseudo-legal move? – Duck Aug 11 '16 at 21:38
  • As a rough guess, shouldn't there be less than 1,000 possible moves to test to see if it is a checkmate? Is the problem that this would take too long so you want to narrow/optimize the search space? – David Aug 11 '16 at 21:44
  • both. If the user has 10 pieces and 6 are so far away or blocked that cannot participate in any way to remove the user from check than I would be checking for several hundreds of plays of those irrelevant pieces wasting CPU cycles for nothing. It must be a better way... – Duck Aug 11 '16 at 21:53

2 Answers2

3

I don't think there is any other way. The final algorithm to me looks like:

  • Obviously, firstly check if by moving the king the opponent may run away from the check.
  • For each opponent's piece check if it is "pinned" (that means that by moving it the opponents clears the way for any other check). Just temporarily "remove" it from the board and see if after that a new check is created.
  • If it is not pinned: check if by moving that piece the opponent may block the check condition. This is done by intersecting two subsets of cells: one is a set of cells, where the opponent's piece may move, the other is a "line" between the opponent's king and the figure that attacks, and, therefore, "checks" it.
  • If these subsets intersect and the condition in the second step is passed - looks like the current situation is **not** a checkmate.
  • If there is no figure that is not forked __AND__ can block the check - the situation __is__ a checkmate.
  • TheDeafOne
    • 93
    • 10
    sx107
    • 309
    • 3
    • 11
    • what do you mean by "just remove it from the board"... remove what? – Duck Aug 11 '16 at 22:00
    • @SpaceDog Sorry for being so unclear. Temporarly take the piece off the board while checking if it is forked and see if on the board any other piece is attacking the king after that "removal". – sx107 Aug 11 '16 at 22:06
    • sorry, I don't get it. – Duck Aug 11 '16 at 22:07
    • @SpaceDog sorry, i've confused a fork and a pin: https://en.wikipedia.org/wiki/Pin_(chess). As I said, you need to check two conditions on every opponent's piece:
      1) is it "pinned" - that piece from the board, see, if the king is hit by a new piece. A new attack may "open" by just removing temporarily opponent's piece from the board. If a new attack on the king does not occur - that means, that this opponent's piece can move. Only then we should check the condition described in third step in my post.
      – sx107 Aug 12 '16 at 00:22
    • thanks I will try to digest your answer and get back later. – Duck Aug 12 '16 at 00:41
    0

    I placed each type of piece where the king is, and got it's moves from there. If its moves touched enemy piece of the same type, I knew it would check the king. Knowing which piece was attacking was also useful for other functions.