-1

Currently In the Process of making a Chess Game, While making the Code for 'CheckMate', I got a problem where the if statement did not execute, even though all conditions were met.

if (boardCopy[x][y] != null)
{
    System.out.print(boardCopy[x][y].getColour() + " == " + turn.charAt(0)+" && "+ boardCopy[x][y].getName().substring(0,2)+" == ki && Checkmate == "+ CheckMate[x][y]);
    if (boardCopy[x][y].getColour() == turn.charAt(0) && boardCopy[x][y].getName().substring(0,2) == "ki" && CheckMate[x][y])
    { 
        System.out.println(turn + "'s King is in Check");
        check = true;
    }
System.out.println(" | Check = "+check);
}

The Output from the Code (the important part, since it was in a for loop it was repeated with slight variations multiple times

W == W && ki == ki && Checkmate == true | Check = false

All conditions are met yet the code isn't executing. Thoughts? (all variables types are correct, no misspelling, W refers to the colour of the piece, ki is the first two letters of the name of the piece ('king'), checkmate is a boolean array containing where a location is being 'attacked' by an enemy piece.)

  • Note that on your last condition, you have an assignment, not an equality test. Also, there's no need for `if (x == true || y == false)`, that's just `if (x || !y)`. – Joshua Taylor May 17 '16 at 01:19
  • So you're printing out some of the values, which is good for debugging, but even better, try printing out the actual boolean values that you're checking. E.g., try printing `boardCopy[x][y].getName().substring(0,2) == "ki"`. The result may be false, even if the string is equal to "ki". – Joshua Taylor May 17 '16 at 01:22

1 Answers1

0

Please notice, strings cannot be compared using "==" actually, String(a)== String(b) -> False. You need to do boardCopy[x][y].getColour().equals(turn.charAt(0))