I can't find the problem. I'm programming an chess game and I want to display the moves made by the player. For example: E4 to E5. The code below compares an old String before something moved with a new one. The problem is I´m using lower case letter for the black figures and upper case letter for the white figures, now I want to tell the player what figure he took. It is not relevant what kind of color the figure had. Because of that I´m making the letter upper case and compare it in an if statement if it is a "P" or a "K" or so on. But it doesn't work. .toUpperCase is working but it always fail at the if statement. It isn't a big problem I can use an or for asking the upper and lower case but I really want to know what the reason is. I hope someone can help me and sorry for my bad english skills.
package projekt;
public class MoveList
{
private String startPosition;
private String endPosition;
private String[][] position =
{
{ "A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8" },
{ "A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7" },
{ "A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6" },
{ "A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5" },
{ "A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4" },
{ "A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3" },
{ "A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2" },
{ "A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1" } };
private String[][] chessBoard;
private String[][] oldChessBoard;
public MoveList(String[][] chessBoard, String[][] oldChessBoard)
{
this.chessBoard = chessBoard;
this.oldChessBoard = oldChessBoard;
move();
}
public void move()
{
for (int i = 0; i < oldChessBoard.length; i++)
{
for (int j = 0; j < oldChessBoard.length; j++)
{
if (oldChessBoard[i][j] != chessBoard[i][j])
{
if (oldChessBoard[i][j] != " " && chessBoard[i][j] == " ")
{
startPosition = position[i][j];
}
else if (oldChessBoard[i][j] == " " && chessBoard[i][j] != " ")
{
endPosition = " to " + position[i][j];
}
else if (oldChessBoard[i][j] != " " && chessBoard[i][j] != " ")
{
endPosition = " take " + position[i][j] + " " + takenFigure(oldChessBoard[i][j].toUpperCase());
}
}
}
}
if (startPosition != null)
{
System.out.println(startPosition + endPosition);
}
}
private String takenFigure(String figure)
{
System.out.println(figure);
if (figure == "P")
{
return "Pawn";
}
else if (figure == "K")
{
return "Knight";
}
else if (figure == "B")
{
return "Bishop";
}
if (figure == "R")
{
return "Rook";
}
else if (figure == "Q")
{
return "Queen";
}
else if (figure == "A")
{
return "King";
}
return "Nothing";
}
}