I have a case statement here that I use:
String color = this.getPlayer().getColor();
System.out.print("\nCOLOR IS: " + color + "\n");
switch(color) {
case "Black":
/*
we need the front two diag pieces. Are they opposition color?
*/
Piece p = board[fromX - 1][fromY - 1].getPiece();
if ((p != null) & (p.getPlayer().getColor() != this.getPlayer().getColor())) {
if (p instanceof Pawn) {
return true;
}
}
p = board[fromX + 1][fromY - 1].getPiece();
if ((p != null) & (p.getPlayer().getColor() != this.getPlayer().getColor())) {
if (p instanceof Pawn) {
return true;
}}
case "White":
System.out.print("\n\nYOU ARE IN WHITE CASE STATEMENT");
p = board[fromX - 1][fromY + 1].getPiece();
if ((p != null) & (p.getPlayer().getColor() != this.getPlayer().getColor())) {
if (p instanceof Pawn) {
return true;
}
}
p = board[fromX + 1][fromY + 1].getPiece();
if ((p != null) & (p.getPlayer().getColor() != this.getPlayer().getColor())) {
if (p instanceof Pawn) {
return true;
}
}}
and running this code will land me in the white case even though the String color variable is definitely Black. here is proof of this fact
COLOR IS: Black
YOU ARE IN WHITE CASE STATEMENTException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at King.underThreat(King.java:233)
at Game.move(Game.java:118)
at main.main(main.java:19)
Process finished with exit code 1
This means that this.getPlayer().getColor()
is definitely "black" and that it is going into the opposite case in the switch statement that I have set up. Can anyone please explain the situations in which this would occur? it seems very illogical to me.