Can Attack

The can attack function looks complicated, but is really quite simple. It is easy to test whether or not we are in the same row as the argument coordinate.

To test for diagonals, we observe that two queens are on a diagonal from each other if the differences in their rows is equal to the differences in their columns. Since the diagonal can either be moving upward or downward there are two different cases to test.

If we cannot attack, it does not mean that the queen that sent the argument values is safe. The last thing we do is to pass the message on to our neighbor. Only if all queens report that a position cannot be attacked can a queen feel really safe.

[audio] [real] Text to accompany slide12, in Chapter 5 of An Introduction to Object-Oriented Programming