0

I think thats my question? Basically this code won't work no matter how many ways I try it. The state won't set to the given array.

if(difficulty == "easy")  {
    state= new int[] {1,3,4,8,6,2,7,0,5};
    blankTile = 7;
    Node start = new Node(difficulty,state,blankTile);
    openList.add(start);
 }
 else if(difficulty == "medium")  {
    state= new int[] {2,8,1,0,4,3,7,6,5};
    blankTile = 3;
    Node start = new Node(difficulty,state,blankTile);
    openList.add(start);
 }
 else if(difficulty == "hard")  {
    state= new int[] {2,8,1,4,6,3,0,7,5};
    blankTile = 6;
    Node start = new Node(difficulty,state,blankTile);
    openList.add(start);

 }
 else if(difficulty == "worst"){  
    state= new int[] {5,6,7,4,0,8,3,2,1};
    blankTile = 4;
    Node start = new Node(difficulty,state,blankTile);
    openList.add(start);
 }

I had this in the constructor of Node at first, where I would just pass in difficulty and it would set the state and blankTile in the constructor. but that wasn't working either. Would somebody be kind enough to help me?

DeadApe
  • 43
  • 6
  • Maybe I'm missing something here, but shouldn't state and start be declared outside the if statements? You can initialize them after the fact within the if statements, but you lose scope once you are no longer in the if statements. – Rabbit Guy Sep 23 '15 at 23:19
  • So state is declared outside, I just didn't show it here. Start wasn't, but after fixing it in my code, it still doesn't change anything. When I go to print state, to see if was assigned correctly, I get a null pointer. – DeadApe Sep 23 '15 at 23:23
  • Would you please edit your code so we can understand it better? – Rabbit Guy Sep 23 '15 at 23:41

1 Answers1

2

This is not how you compare strings in Java. You are comparing if two references to a string object are the same, not if the strings themselves are.

This is why state is never assigned.

You need to use String.equals()

if(difficulty.equals("easy"))  {
    state= new int[] {1,3,4,8,6,2,7,0,5};
    blankTile = 7;
    ...
 }
 else if(difficulty.equals("medium"))  {
    state= new int[] {2,8,1,0,4,3,7,6,5};
    blankTile = 3;
    ...
 }

As a side note, you are repeating the lines

Node start = new Node(difficulty,state,blankTile);
openList.add(start);

several times. It might be useful to move them outside your if statements.

PC Luddite
  • 5,883
  • 6
  • 23
  • 39