programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Eight Puzzle Algorithm Help

Greenhorn
Posts: 1
Hi all, I'm working on an assignment and want to make sure I'm going in the right direction. Any help or tips would be greatly appreciated. (I don't want answers just guidance)
These are the two classes I'm working on now. One thing I don't understand is how to "reference parent BoardState" in the BoardState class.
And for the Manhattan method I don't understand how I would begin to implement the distance between board states.

Screen-Shot-2016-07-24-at-9.10.55-PM.png

Bartender
Posts: 1843
10
Hi!  Welcome to the ranch.
Just one little administrative thing.  A subject of "Help" isn't really that useful.  I've changed it to a more meaningful subject line of "Eight Puzzle Algorithm Help" for you.

It might be your actual assignment, or just one similar.
However it explains the "EightPuzzle" and the "Manhattan method" you are asking about.

1 - How to reference "parent board state"

I think that just means you need a variable of type : "BoardState" which represents the state of the board just before the last move.
Keeping track of that prevents you from just "undoing" the move you just made.
You can check if two BoardState variables represent the same state using the "equals" method - which I presume you need to implement.

2 - "Manhattan method"
This is defined as:
"The sum of the distances (sum of the vertical and horizontal distance) from the blocks to their goal positions, plus the number of moves made so far to get to the state."

My suggestion: break the problem down.
Can you work out how far a block is from its "target position" ?
So if you have '1' in position '1' and it should be in position '4' - how many vertical/horizontal moves is it away from its target position
Draw a picture :-)
Once you figure THAT out, the rest should be easy.

 I will open the floodgates of his own worst nightmare! All in a tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS