TheGamerPlayz TheGamerPlayz - 2 months ago 9
Java Question

For loop and if statements acting odd

I am making a chess game and so far everything is good and I am writing the rules of each piece now. The problem is that a for loop is acting oddly. Its for the bishop so x is going down and y is going up. At this point it acts oddly whenever I try to add the point to a possible move

for (int i = 0; i < 8; i++) {
Point newLoc = new Point(x-i, y+i);

if(team.equals("white")) {
if(containsPiece(newLoc)) {
if(ChessBoard.black.containsKey(newLoc)) {
possibilities.put(newLoc, rating);
break;
}
else {
break;
}
} else
possibilities.put(newLoc, rating);
}


containsPiece()
is working just fine and
possibilities
is the
HashMap
I am storing the possible moves in.

The way I see it it should be working perfect because if the tile at
newLoc
is white it shouldn't add it to the possible moves and stop getting any moves after it in that direction. Does anyone see why it seems to abandon all the previous possible moves added to
possibilities


What it should look like

What it is looking like with the code above

Answer

i should start in 1, not 0, since when i==0, newLoc is the position of the bishop ((x-0,y+0)), so you break from the loop, since the bishop is a white tile.

Comments