Matthew Cliatt - 5 months ago 19

Java Question

This may be a little hard to explain without a picture but, I am in the process of checking to see if the king is in check. To do so, I am starting at the king's location and going up, left, down, right, then all the diagonal patterns.

To simplify my code, I have a path checker method which takes in a starting location and ending location and returns true if there are any threats to the king in that path. So, I am calling this method like:

`board.incheckPath(kingLocation, new Location(8, kingY))`

This would check from the king to the top row, same column. I have a similar statement for left, down, and right.

The problem is I am trying to use the same fashion for the diagonal patterns, and I can't figure out a simple algorithm to figure out where the last location is. If you are higher than you are to the right, then if you go up and right diagonally, you're going to hit the top row before you hit the right most column. I figured out the algorithm for that location is:

`if x > y {`

row = 8; column = 8-(x-y)

} else {

row = 8-(x-y); column = 8;

}

Because where you land at is going to be the difference between x and y away from either the top row or right column. But I cannot figure out what the result would be for going up and left, down and left, or down and right.

Answer

Suppose, you coordinates are

```
/|\ y
| col8
+---+ ... +---+---+
| | | | | <- row 8
+---+ ... +---+---+
| | | | |
+---+ ... +---+---+
...............
+---+ ... +---+---+
| | | | | <- row 1
+---+ ... +---+---+--->
x
```

Extending your solution it will looks like

```
// Up right
if (y > x) { row = 8; column = 8-(y-x) } else { row = 8-(x-y); column = 8; }
// Down left
if (x > y) { row = 1; column = 1+(x-y) } else { row = 1+(y-x); column = 1; }
// Up left
if (9-x < y) { row = 8; column = x+y-8 } else { row = x+y-1; column = 1; }
// Down right
if (9-x > y) { row = 1; column = x+y-1 } else { row = x+y-8; column = 8; }
```

Source (Stackoverflow)

Comments