2

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.

Sam
  • 7,252
  • 16
  • 46
  • 65
Matt C
  • 4,470
  • 5
  • 26
  • 44
  • what do you mean "last" location? – cammil Dec 17 '13 at 18:50
  • Going in every diagonal direction from the king whats the last spot you will come to when you go: (1) left and up, (2) left and down, (3) right and up, and (4) right and down. I already figured out what the last spot is when moving right and up. – Matt C Dec 17 '13 at 18:58
  • Last as in, last before leaving the board. So I can tell my path checker method to check until it gets to Location X. – Matt C Dec 17 '13 at 18:58

3 Answers3

2

I would suggest, that you define a path in another more suitable way:

 int pathDeltas[][] = {
     {1, 0}, {0, 1}, {-1, 0}, {0, -1}, // Up, down, left, right
     {1, 1}, {-1, 1}, {1, -1}, {-1, -1}, // diagonal paths
 };

Then you can start from the kind position and adds deltas to x and y coordinates until you hit 1 or 8 value. Also you could calculate the knight paths like this:

int knightDeltas[][] {{1, 2}, {2, 1}, {-1, 2}, {-2, 1},
                     {1, -2}, {2, -1}, {-1, -2}, {-2, -1}};
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
  • That's a cool way to look at it, I wouldn't have thought of that. I will most likely implement this but, I really want to figure this out now that I have the problem in my head. Any ideas for a simple algorithm to get the last possible location for each direction? – Matt C Dec 17 '13 at 19:07
2

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; }
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
  • Thank you so much. That is exactly what I was looking for. Could you explain what logic you used to get to your answers? – Matt C Dec 17 '13 at 20:30
  • 1
    I drew an example taking one spot and finding the "last" location. Then I noted, how this "last" location changes, if x or y increase. If it also increases, then you have to add x or y. If it decreases you have to subtract x or y. Then I looked at the diagonal, where 9-x=y or x=y and figured out the -8 or +8 constants: (consider "down right": if y=9-x then row=x+y-C=x+(9-x)-C=9-C===1 => C=8) – Boris Brodski Dec 18 '13 at 09:46
1

to M Cliatt, 2013, number of bishop diagonal steps from square defined by (R,C) (where R and C are from [1..8]) in each direction until you hit the wall on 8x8 board

NE = Min(8 - R, 8 - C)
NW = Min(8 - R, C - 1)
SE = Min(R - 1, 8 - C)
SW = Min(R - 1, C - 1)

a moment's reflection on the board shows these split along the diagonals (NE, SW split this way) and anti-diagonals (NW, SE)...e.g. for NE above diagonal 8-R branch is always chosen and below diagonal the 8-C branch is always chosen. They are equal on the diagonal.

So last element of ray in NE direction starting on squares above diagonal from (R,C) = (R + 8-R, C + 8-R)

btw - thinking about this in the context of bishop legal moves obstruction (at the moment)...interested in where you ended up on your problem

jweil
  • 11
  • 2