Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this project you build upon the code you wrote in your recent lab to implement a working 15-Puzzle computer game. Your program will allow

In this project you build upon the code you wrote in your recent lab to implement a working 15-Puzzle computer game. Your program will allow a user to solve the 15-Puzzle by making repeated tile moves until the puzzle is in a solved configuration. When the puzzle is solved, the program should print a congratulatory message and terminate. There should also be a way to stop the program gracefully mid-game. From your lab, you will utilize: your puzzle array(s), initSolution(), printPuzzle() and puzzleEqual() as well as code provided for you. A big part of this project is integrating code you have already written, with code provided, and with code you will write. Your program will consist of several components, each of which can be implemented as a separate method. We will provide for you a main() method and methods swap() and swapper() which should be used without modification. There are two methods that you will need to write. They are described below. initializer(int[][] p) - This method will initialize a 15-puzzle to a "random" configuration. It should accept one parameter, your puzzle array. Approach: Begin with a puzzle initialized to a solved puzzle using initSolution() from your lab. Then, interchange 5 random pairs of tiles to scramble the puzzle. Outline: 1) Using initSolution(), begin by creating a solved 15-Puzzle in an array called puzzle (from lab). Also, create a second solved puzzle array (for checking for a solved puzzle), and call it solution. 2) Using randomInt() from Project 2, find row and column random coordinates; call them row1 and col1. Do the same for a second set of coordinates; call them row2 and col2. 3) Using your puzzle array, swap the values found at puzzle[row1][col1] and puzzle[row2][col2]. The open spot on the board (the 0 tile) is included in the possible locations to swap. (swap() is one of the methods provided for you.)

4) Repeat steps two and three 7 times to do 7 interchanges. playLoop(int[][] p, int[][] sol) - This method will repeatedly allow the user to move a tile by selecting a number between 1 and 15, inclusive. It accepts two parameters, your puzzle array and the solution array. Approach: The input is only a number?no direction to move the tile is needed, since there is only one open spot on the board in which to move a tile. Only a tile that is adjacent to the open spot can be moved. Therefore, a legal move must not only be in the range 1-15, inclusive, but it must also be a tile that is adjacent to the open spot. Use textIO to get tile the number from the user. Outline: 1) Print out the puzzle array so that the user can see the current configuration of the puzzle (using printPuzzle() from lab). 2) Prompt the user for an input value between 1 and 15, inclusive using TextIO. Also, allow input of -1 to quit early. 3) Call method swapper() (which is provided for you) to verify that the input is the range 1?15, inclusive, verify that the tile is adjacent to the open spot, and swap the tile selected with the open spot. If the tile selected is out of range or not adjacent to the open spot, false is returned and no tile movement is performed; otherwise, true is returned and the tile selected is swapped with the open spot. 4) The puzzle array should be checked for a solution. Use puzzleEqual() to compare your puzzle array with the solution puzzle. 5) Repeat steps one through four above until the puzzle is solved or a -1 is entered. 6) A congratulatory note should be printed when the puzzle is solved, and a simple "Bye" should be printed if -1 is entered. A sample dialog of the running puzzle is shown below:

image text in transcribed
\f

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

Define union security and explain its importance to labor leaders.

Answered: 1 week ago

Question

4 What is the benefit of the use of hypertext links?

Answered: 1 week ago