-1

I'm making chess in Java using Swing, and I have already figured out how to check legal moves for every piece. There is nothing wrong with that part. But now, I am having problems with checking whether a path is free. For example, all the pieces in chess cannot jump over a piece unless it's a knight. If a pawn is blocking a bishop's path, it cannot go over than pawn. I called the method that checks whether a path is free isVectorFree(). It's working fine for horizontal and vertical paths but it's not working properly for diagonal paths.

Here is the code for isVectorFree():

public boolean isVectorFree(int x1, int y1, int x2, int y2) {
    if (x1 == x2 && y1 > y2) { // horizontal north
        for (int i = y1 - 1; i > y2; i--) {
            if (piecePositions[i][x1] != null) {
                return false;
            }
        }
    } else if (x1 < x2 && y1 == y2) { // horizontal east
        for (int i = x1 + 1; i < x2; i++) {
            if (piecePositions[y1][i] != null) {
                return false;
            }
        }
    } else if (x1 == x2 && y1 < y2) { // horizontal south
        for (int i = y1 + 1; i < y2; i++) {
            if (piecePositions[i][x1] != null) {
                return false;
            }
        }
    } else if (x1 > x2 && y1 == y2) { // horizontal west
        for (int i = x1 - 1; i > x2; i--) {
            if (piecePositions[y1][i] != null) {
                return false;
            }
        }
    }
    else if (x1 < x2 && y1 > y2) { // diagonal northeast
        // these diagonals aren't working properly
        for (int row = x1 + 1; row < x2; row++) {
            for (int col = y1 - 1; col >= y2; col--) {
                if (piecePositions[row][col] != null) {
                    return false;
                }
            }
        }
    } else if (x1 < x2 && y1 < y2) { // diagonal southeast
        for (int row = x1 + 1; row < x2; row++) {
            for (int col = y1 + 1; col < y2; col++) {
                if (piecePositions[row][col] != null) {
                    return false;
                }
            }
        }
    } else if (x1 > x2 && y1 < y2) { // diagonal southwest
        for (int row = x1 - 1; row >= x2; row--) {
            for (int col = y1 + 1; col < y2; col++) {
                if (piecePositions[row][col] != null) {
                    return false;
                }
            }
        }
    } else if (x1 > x2 && y1 > y2) { // diagonal northwest
        for (int row = x1 - 1; row >= x2; row--) {
            for (int col = y1 - 1; col >= y2; col--) {
                if (piecePositions[row][col] != null) {
                    return false;
                }
            }
        }
    }
    return true;
}

This method takes the piece's x coordinate x1 and y coordinate y1 in the matrix piecePositions and the desired x coordinate x2 and desired y coordinate y2 in order to see if the path between the current coordinates and desired coordinates is free, but as I said earlier, this isn't working properly for the diagonals. Specifically, isVectorFree() is being affected by surrounding pieces. In the starting position of the chess board, if I move the pawns away to make way for the bishop, even if the path is now free for the bishop, it still cannot move anywhere throughout the path, for some reason.

How can I fix this?

parrot15
  • 309
  • 3
  • 13
  • 3
    step by step debug it – Philipp Sander Jan 08 '18 at 15:41
  • 1
    [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Jan 08 '18 at 15:41
  • 1
    Please read [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) before attempting to ask more questions. –  Jan 08 '18 at 15:41
  • Well after debugging, I think the for loops for the diagonals are incorrect and aren't actually checking diagonally. – parrot15 Jan 08 '18 at 15:51

2 Answers2

1

On a specific diagonal, each row and each column are unique, aren't they?

If (2,4) is on your diagonal, (2,3) can't be on the same diagonal.

Now look at your for loops, for each row value you loop through several cols.

You should be having one loop with, say an index i, and then increment/decrement row and col with the value of i.

Jos
  • 422
  • 4
  • 10
  • Thanks, I just realized that from debugging. Actually, before I used nested for loops, I did actually use a single loop to increment/decrement row and col, but I couldn't figure out how to make the loop stop at the end of the board. How would I do that? – parrot15 Jan 08 '18 at 15:54
  • `if()` statements exist for a reason –  Jan 08 '18 at 15:55
  • As (x1,y1) and (x2,y2) are both on the board, the range for your incrementor, Math.abs(x2 - x1), will not let you move off the board. (Provided that both points are on the same diagonal indeed; that is: Math.abs(x2-x1) == Math.abs(y2-y1), maybe you should check that.) – Jos Jan 08 '18 at 17:00
  • Nevermind, I got it to work perfectly. I had to flip the x and y values. For some reason I've had to do that every time I've been dealing with coordinates so far. Thanks for the help. – parrot15 Jan 08 '18 at 18:05
1

Your nested for loops mean you're checking everything in that direction diagonally (the whole square). So, if you were at (0,0) and trying to move to (3,3) your code is checking (1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), and (3,3). You only want to check the path (1,1), (2,2), (3,3).

Assuming that the move is valid (ie, abs(y1 - y2) == abs(x1 - x2)), loop through the difference not the coordinates themselves:

} else if (x1 < x2 && y1 > y2) { // diagonal northeast
    for (int i = x2 - x1; i > 0; i--) {
        if (piecePositions[x1 + i][y1 - i] != null) {
            return false;
        }
    }
}
Andy Hubbard
  • 118
  • 8
  • I agree with your assessment of how my for loops aren't working, but your code doesn't seem to be working. I still have the same problem as earlier after using this. – parrot15 Jan 08 '18 at 16:16
  • That was an example for one of the four directions to use as a pattern for you to fix the other three. Since your (0,0) is on the top left, let's move from (3,5) to (6,2). `i` starts out as `x2-x1`, which is `6-3==3`. First loop, we check `(x1+i,y1-i)` or `(3+3,5-3)==(6,2)`. Then we decrement `i`, `3-1==2`, and check `(3+2,5-2)==(5,3)`, then again `i=1` and check `(3+1,5-1)==(4,4)` and we're done. The other directions will need to be changed as to whether you add or subtract by i and whether i starts at `x1 - x2` or `x2 - x1`. – Andy Hubbard Jan 09 '18 at 20:45