• Post Reply Bookmark Topic Watch Topic
  • New Topic

Im trying to create solution for classic 8 puzzle problem in java via breadth first search  RSS feed

 
shadman kudchikar
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to create solution for classic 8 puzzle problem in java via breadth first search
However my expand() is not working properly
Here is the code
You can also donwload same java file via attachments

 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are using the Clone method. Clone creates a shallow copy (i.e. does not change the inner references), so when you manipulate the array in the four expand methods, you also change it in the original int array. If you go through with step debugging, you'll see that the 2D int array holds three regular int arrays. When you clone, you get a new reference for the 2D array but the references to each normal array inside of it remains the same. You'll need to write a method to deep copy the int array (make new references for everything).

Also you're copy pasting a lot of code. Might want to consider refactoring that into a general "expand" function.

When written with a deep copy, you will get the expected output.
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, shadman!

And congrats on the diagnosis, Zachary. I'm giving you a cow for taking the time to go through the code and figure it out.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!