user7518095
user7518095

Reputation: 93

Chess - Avoiding Infinite Recursion when Preventing Check

I have an algorithm as follows to prevent a player from choosing a move that puts it into check:

  1. Fake the move (by copying the board and working on the copied board)
  2. Get all legal opponent moves after the move has been faked.
  3. If one of the legal moves can attack the player's king, then the originally faked move cannot be chosen.

However, in step 2 (bolded), the program must check that whatever the other player does does not move the other player into check. This creates an infinite recursion that I'm not sure algorithmically how to avoid. What suggestions do people have for this situation?

Upvotes: 1

Views: 394

Answers (1)

foo
foo

Reputation: 186

You can define another function which returns true if the player's piece can move to the cell of the opponent's king and false otherwise.

Now call this function for the opponent when you fake a move.

Upvotes: 2

Related Questions