I'm making a Checkers game for an assignment. The whole thing is running the way it should, except for one weird thing. Here's my board:
I move by giving the source row and column, then the destination row and column.
move(int srcR, int srcC, int destR, int destC)
I'm supposed to print out an error if I try to move a piece to an invalid spot (not diagonally). So if I try to move a piece from 5 2 -> 4 2, it gives me an error message.
if(destR == srcR+1 || destR == srcR-1 &&
destC == srcC+1 || destC == srcC-1){
// code code code
}else
message = "Invalid Move! Can only move diagonally one space.";
For most things it works, but if I try to move directly down one space (for example, 2 3 -> 3 3) it's moving the piece and not giving me the error message.
I'm stuck! Any ideas why this may be happening? I can post more code if needed.
According to your logic, if
destC == srcC-1
is true, the whole expression will be true. You have to pay attention to the order that Java evaluate the boolean operations~ Add () when necessary~
It is quite simple, I think. There are only four allowed moves.
int deltaX = Math.abs(srcR - destR);
int deltaY = Math.abs(srcC - destC);
boolean validMove = deltaX == 1 && deltaY == 1;
Of course, this check allows backward moves. But the direction of a backward move depends on the playing color and wether the piece is promoted by reaching the other end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With