Question: Please provide the JAVA source code needed to run this program. The goal of this lab is to write a program, TreasureHunting, that plays the

 Please provide the JAVA source code needed to run this program.

The goal of this lab is to write a program, TreasureHunting, thatplays the game of finding a treasure with the user. The treasureis hidden somewhere in the 10-by-10 grid. The user has 10 chances

Please provide the JAVA source code needed to run this program.

The goal of this lab is to write a program, TreasureHunting, that plays the game of finding a treasure with the user. The treasure is hidden somewhere in the 10-by-10 grid. The user has 10 chances to guess its location. If the treasure is not at the guessed location, the program informs the user about how far the treasure is from the location guessed presently. Also, the second round on, the program presents a diagram that shows the present guess and the previous guess. Optionally, the program advises the user with the information of whether or not the guess got closer than the previous guess. The program uses six global int variables: static int prevx, prevy, nowx, nowy, truex, trueY; These are in pairs and represent the previous guess, the present guess, the true location. For preparation, the program explains the rule of the game as ########################### ################# # A treasure has been hidden at a location in a 10x10 # # gird. Guess where it is. You have 10 chances. # ####################################################### It sets -1 to nowX and nowy, assigns a random integer between 1 and 10 to truex, and assigns another random integer between 1 and 10 to trueY. Then the program enters a for-loop that iterates over the round numbers 1 .. 10 with some int variable i. The body of the loop is executed only if either nowX != trueX or nowy != truey. The actions to be performed in the body of the if statement is as follows: 1. Announce the round is the following format: ---- Round M ---- Here M represents the round number and should be substituted with an appropriate value. 2. If the round is greater than or equal to 2, show the locations by calling the method show (see below). 3. Store the values of nowX and now in prevX and prevy (earlier, it said: in trueX and true, which was wrong) 4. Receive the guess (that is, the values for nowX and nowy). 5. Check the correctness by calling the method checkDistance (see below). 1 6. If the round number is greater than or equal to 2 and if (either nowX != trueX or nowy != trueY), call advise (see below). After quitting the loop, the program announces the true location, regardless of whether the user has correctly guessed the location in 10 rounds or not. The method show This method uses a double for-loop to print the locations of the present and the previous guesses. Here is an example of an output generated by the method: 10 9 8 7 6 5 4 3 2 1 @ = current, P = previous The numbers appearing at the end of the lines are the y-coordinates. The character P represents the previous location. The character represents the current location. The period . represents the rest. The external for-loop generates the value for y going down from 10 to 1 and the internal for-loop generates the value for x going up from 1 to 10. In the body of the internal loop, the method checks whether the combination of x and y is equal to the combination of nowX and nowy, and if so, print "Q"; otherwise, the method checks whether the combination of x and y is equal to the combination of prevX and prevy, and if so, print "P"; otherwise, it prints ".". After completing the internal for-loop, the method prints one whitespace and the value of y. After completing the external for-loop, the method prints the line that says " = current, P = previous". The method checkDistance The method computes the Manhattan distance between nowx, nowy and truex, truey, which is as the sum of the absolute difference between nowX and trueX and the absolute difference between nowy and truey. It uses int constants CLOSE and FAR. Their values are set to 3 and 6. After calculating the value of the Manhattan distance, the method prints a statement according to its value: 0: "You have found the treasure!" 1, 2, 3: "The distance is no more than 3." 4, 5, 6: "The distance is no more than 6." 7 or higher: "The distance is more than 6." 2 The method advise The method computes the difference of the present distance from the previous distance. The previous distance can be computed using the formula used in checkDistance with prevX and prevY replacing nowX and nowy. Depending on the sign of this difference, the method prints the following: 0: "The same distance" 1 or higher: "You are farther." -1 or smaller: "You are closer." Execution Examples In this example, the user does not find it. % java TreasureHunting ####################################################### # A treasure has been hidden at a location in a 10x10 # # gird. Guess where it is. You have 10 chances. # ####################################################### ---- Round 1 ---- Enter your guess for X, Y: 5 5 The distance is no more than 6. Round 2 10 9 8 7 6 5 4 3 2 1 Q = current, P = previous Enter your guess for X, Y: 77 The distance is no more than 6. The same distance. Round 3 ---- 10 9 8 3 Round 7 10 9 8 .@.P.. 7 6 5 4 3 2 1 @ = current, P = previous Enter your guess for X, Y: 5 7 The distance is no more than 3. You are closer. Round 8 ---- 10 9 8 7 QP 6 5 4 3 2 1 @ = current, P = previous Enter your guess for X, Y: 4.7 You have found the treasure! The treasure was at (4,7). The goal of this lab is to write a program, TreasureHunting, that plays the game of finding a treasure with the user. The treasure is hidden somewhere in the 10-by-10 grid. The user has 10 chances to guess its location. If the treasure is not at the guessed location, the program informs the user about how far the treasure is from the location guessed presently. Also, the second round on, the program presents a diagram that shows the present guess and the previous guess. Optionally, the program advises the user with the information of whether or not the guess got closer than the previous guess. The program uses six global int variables: static int prevx, prevy, nowx, nowy, truex, trueY; These are in pairs and represent the previous guess, the present guess, the true location. For preparation, the program explains the rule of the game as ########################### ################# # A treasure has been hidden at a location in a 10x10 # # gird. Guess where it is. You have 10 chances. # ####################################################### It sets -1 to nowX and nowy, assigns a random integer between 1 and 10 to truex, and assigns another random integer between 1 and 10 to trueY. Then the program enters a for-loop that iterates over the round numbers 1 .. 10 with some int variable i. The body of the loop is executed only if either nowX != trueX or nowy != truey. The actions to be performed in the body of the if statement is as follows: 1. Announce the round is the following format: ---- Round M ---- Here M represents the round number and should be substituted with an appropriate value. 2. If the round is greater than or equal to 2, show the locations by calling the method show (see below). 3. Store the values of nowX and now in prevX and prevy (earlier, it said: in trueX and true, which was wrong) 4. Receive the guess (that is, the values for nowX and nowy). 5. Check the correctness by calling the method checkDistance (see below). 1 6. If the round number is greater than or equal to 2 and if (either nowX != trueX or nowy != trueY), call advise (see below). After quitting the loop, the program announces the true location, regardless of whether the user has correctly guessed the location in 10 rounds or not. The method show This method uses a double for-loop to print the locations of the present and the previous guesses. Here is an example of an output generated by the method: 10 9 8 7 6 5 4 3 2 1 @ = current, P = previous The numbers appearing at the end of the lines are the y-coordinates. The character P represents the previous location. The character represents the current location. The period . represents the rest. The external for-loop generates the value for y going down from 10 to 1 and the internal for-loop generates the value for x going up from 1 to 10. In the body of the internal loop, the method checks whether the combination of x and y is equal to the combination of nowX and nowy, and if so, print "Q"; otherwise, the method checks whether the combination of x and y is equal to the combination of prevX and prevy, and if so, print "P"; otherwise, it prints ".". After completing the internal for-loop, the method prints one whitespace and the value of y. After completing the external for-loop, the method prints the line that says " = current, P = previous". The method checkDistance The method computes the Manhattan distance between nowx, nowy and truex, truey, which is as the sum of the absolute difference between nowX and trueX and the absolute difference between nowy and truey. It uses int constants CLOSE and FAR. Their values are set to 3 and 6. After calculating the value of the Manhattan distance, the method prints a statement according to its value: 0: "You have found the treasure!" 1, 2, 3: "The distance is no more than 3." 4, 5, 6: "The distance is no more than 6." 7 or higher: "The distance is more than 6." 2 The method advise The method computes the difference of the present distance from the previous distance. The previous distance can be computed using the formula used in checkDistance with prevX and prevY replacing nowX and nowy. Depending on the sign of this difference, the method prints the following: 0: "The same distance" 1 or higher: "You are farther." -1 or smaller: "You are closer." Execution Examples In this example, the user does not find it. % java TreasureHunting ####################################################### # A treasure has been hidden at a location in a 10x10 # # gird. Guess where it is. You have 10 chances. # ####################################################### ---- Round 1 ---- Enter your guess for X, Y: 5 5 The distance is no more than 6. Round 2 10 9 8 7 6 5 4 3 2 1 Q = current, P = previous Enter your guess for X, Y: 77 The distance is no more than 6. The same distance. Round 3 ---- 10 9 8 3 Round 7 10 9 8 .@.P.. 7 6 5 4 3 2 1 @ = current, P = previous Enter your guess for X, Y: 5 7 The distance is no more than 3. You are closer. Round 8 ---- 10 9 8 7 QP 6 5 4 3 2 1 @ = current, P = previous Enter your guess for X, Y: 4.7 You have found the treasure! The treasure was at (4,7)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!