1

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";
    }
} 
Timo K.
  • 63
  • 5
  • Use `equalsIgnoreCase()` – Ashraful Islam May 12 '17 at 19:39
  • 6
    see this: [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – quackenator May 12 '17 at 19:39
  • 1
    Hint: do not *decode* information in string casiness. Interpreting strings at runtime to determine the player type or what piece... sorry that is just wrong. You should use various classes to represent such information, you should read about using enums for example. Meaning: good OO design is about creating helpful abstractions. You don't have any abstractions. The consequences is that expressing more complicated things will become incredibly complicated to do. I honestly think you are overburdening yourself with such kind of exercise. – GhostCat May 12 '17 at 19:50
  • 1
    Meaning: programming a chess game is a really advanced topic. Maybe you should look for something less complicated for now. – GhostCat May 12 '17 at 19:53

2 Answers2

2

This is a typical case of String comparison in Java, for which using the == operator is not the correct approach.

For case-based equality, the equals() method should be used, otherwise the equalsIgnoreCase().

For example, the statement

if (figure == "P")
{
  return "Pawn";
}

should really be

if (figure.equals("P"))
{
  return "Pawn";
}

More details are provided in another StackOverflow question.

Event better, for the takenFigure() method, a switch statement should be used instead of the consecutive if statements, e.g.

switch (figure) {
  case "P":
    return "Pawn";
  case "K":
    return "Knight";
  ...
}
Community
  • 1
  • 1
PNS
  • 19,295
  • 32
  • 96
  • 143
1

Try to use .equals() to compare Strings

    if (figure.toe.equals("P"))
    {
        return "Pawn";
    }
dvdLucka
  • 51
  • 7