Click here to Skip to main content
15,743,179 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was assigned to do this sliding puzzle assignment but I am not sure on where to really start. The assignment rules are as follows:

Each 8-puzzle has tiles labeled 1-8 and one empty space denoted by a 0.
The commands given signify where you should attempt to move the empty space in relation to its current position.
The only moves you are allowed to make are swapping the empty space with an adjacent tile to it.
If asked to make an impossible swap (ie. move the blank tile out of the grid bounds), don't terminate the program, simply ignore that command.
The Assignment
For this assignment, we are asking you to take an 8-puzzle and follow a sequence of movement commands UP, DOWN, LEFT, and RIGHT. At the end of the sequence, you should check whether the puzzle has been solved.

As input you will be given some initial board state in the form of a sequence of numbers where 0 corresponds to the "blank" tile. The example below would be the form of the "initial state" example above. After that will be a sequence of commands "UP", "DOWN", "LEFT", and "RIGHT" represented by characters on a single line separated by spaces. (The example command sequence below has no connection to the example graphic)

Example input:
1 8 2 0 4 3 7 6 5

Your Task
After storing the initial board state, you will perform the commands given (so long as they are allowed, as specified in the rules section). Run through all the commands, then check whether the puzzle is solved correctly.

Your output should be Solution is good! if the puzzle is in solved order as specified above, and Wrong solution! if the puzzle is not in the "goal state" specified above.

What I have tried:

I have not tried much with this program as I was stumped on where to exactly begin. If anyone can offer and advice for me on where to start or how to solve this program let me know.
Updated 14-Nov-19 11:33am
Richard MacCutchan 14-Nov-19 11:00am    
Start with research, planning and design. Draw your puzzle on a piece of paper and work out what you need to do to move tiles from one location to another.

Start by reading the question carefully - it tells you in some detail what to do.
Break the question down into a set of steps:
1) Read your input.
2) Store the input.
4) Find the start position.
3) Loop
3.1) Read a command. Check it is valid - up, down, left, right only - if it is, do it. If it isn't, do something sensible with it - report a problem, ignore it, whatever you think best. just don't crash your app!
3.2) Check for more commands and loop if there are.
4) Check if the puzzle is solved, and if it is, say so and exit

Then implement each step one by one - testing carefully before you move onto the next.
Share this answer
If anyone can offer and advice for me on where to start or how to solve this program let me know.

Simply play the puzzle: cut little squares of paper, write numbers on them and play the puzzle by hand. Draw a grid, number rows and columns. Pay attention to when a move is legal or not, what happen to numbers when a move is done.
A simple observation of what happen when you play the puzzle is your algorithm.
I have not tried much with this program as I was stumped on where to exactly begin.

Your programm will roughly look like that:
// Read initial puzzle

// read moves

// play puzzle

// give result
Share this answer
Stefan_Lang 15-Nov-19 3:43am    
I think that's the first time I've seen an advice to solve a programming question by cutting out paper tiles :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900