Description
The 15-Tile Puzzle is a game where a player moves tiles on a 4 x 4 board to try to arrange them into a predetermined order. You are to do a simulation of this game.
Let's Do It!
+---+---+---+---+ | 1 | 2 | 3 | 4 | +---+---+---+---+ | 5 | 6 | 7 | 8 | +---+---+---+---+ | 9 |10 |11 |12 | +---+---+---+---+ |13 |14 |15 | | +---+---+---+---+
The 4 x 4 board consists of 15 tiles, where the intial position is shown above. There is always one blank space where there is no tile.
In your simulation, allow the user to control the movement of the tiles using the following commands:
'R': Moves a tile to the right.
'L': Moves a tile to the left.
'U': Moves a tile up.
'D': Moves a tile down.
'Q': Quit the program.
Suppose the current board is as shown in the initial position above. The command:
'R' will move the tile 15 to the right.
'L' is an invalid move since it is not possible to move any tile to the left.
'D' will move the tile 12 down.
'U' is an invalid move since it is not possible to move any tile up.
Assume the user will always enter one character. Both upper case and lower case characters are allowed. If an invalid command is entered, display "Wrong command!". If an invalid move occurs, display "Invalid move!".
Your program should generate a random puzzle (i.e, 15 numbers placed on the 4 x 4 board) for the user to solve. The goal of the user is to move the tiles from the position in the random puzzle until the initial position is reached.
Tips and Hints: Use a 4x4 2D array to store the tile numbers. Also keep track of the location of the blank space in the 2D array. You can use a special value (e.g., 0 or -1) to represent the blank space in the 2D array. There are many different ways to generate a random board. One simple way is: place 1-15 and the value you choose for the blank space in a 1D array, Loop from position 15 to 1 randomly generate an integer, index, which is smaller than current position i, using the method nextInt(i) in class Random swap the values at i and index Assign the values in the 1D array to the 2D array |
Your program must do the following:
Display the random puzzle generated. Keep looping, printing the puzzle and prompting
for the user to enter a command. Exit the program when the user enters 'Q' or 'q'.
If the input is an invalid command, print out the error message and prompt the user again. If the input is an invalid move, print out the error message and prompt the user again. If the input is valid, move the correct tile accordingly. Your program output should follow the sample run below (user's input is in RED):
I have generated a random puzzle for you to solve: +---+---+---+---+ |13 |15 | 7 | 5 | +---+---+---+---+ | 9 | 6 |14 | 8 | +---+---+---+---+ | 1 | 2 | 4 |12 | +---+---+---+---+ |11 | |10 | 3 | +---+---+---+---+ Enter command: N Wrong command! Enter command: u Invalid move! Enter command: d +---+---+---+---+ |13 |15 | 7 | 5 | +---+---+---+---+ | 9 | 6 |14 | 8 | +---+---+---+---+ | 1 | | 4 |12 | +---+---+---+---+ |11 | 2 |10 | 3 | +---+---+---+---+ Enter command: L +---+---+---+---+ |13 |15 | 7 | 5 | +---+---+---+---+ | 9 | 6 |14 | 8 | +---+---+---+---+ | 1 | 4 | |12 | +---+---+---+---+ |11 | 2 |10 | 3 | +---+---+---+---+ Enter command: D +---+---+---+---+ |13 |15 | 7 | 5 | +---+---+---+---+ | 9 | 6 | | 8 | +---+---+---+---+ | 1 | 4 |14 |12 | +---+---+---+---+ |11 | 2 |10 | 3 | +---+---+---+---+ Enter command: d +---+---+---+---+ |13 |15 | | 5 | +---+---+---+---+ | 9 | 6 | 7 | 8 | +---+---+---+---+ | 1 | 4 |14 |12 | +---+---+---+---+ |11 | 2 |10 | 3 | +---+---+---+---+ Enter command: d Invalid move! Enter command: l +---+---+---+---+ |13 |15 | 5 | | +---+---+---+---+ | 9 | 6 | 7 | 8 | +---+---+---+---+ | 1 | 4 |14 |12 | +---+---+---+---+ |11 | 2 |10 | 3 | +---+---+---+---+ Enter command: q Bye! |
Files:
Lab7.java, the main classGrading
How to hand in the assignment