please do it in java language! thank you so much! e treasure is hniddei SoInewhh
ID: 3752120 • Letter: P
Question
please do it in java language! thank you so much!
e treasure is hniddei SoInewhhere l Che y-lu gr ces 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 -- RoundM- 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 nowY in trueX and trueY 4. Receive the guess (that is, the values for nowX and nowY)Explanation / Answer
Explanation::
Code in JAVA::
import java.util.Random;
import java.util.Scanner;
public class TreasureHunt {
/**
* Six global int variables are declared below.
**/
static int prevX, prevY, nowX, nowY, trueX, trueY;
public static void main(String[] args) {
/**
* We use Scanner class object to take input from user.
* We use Random class to generate random numbers.
**/
Scanner sc=new Scanner(System.in);
Random random=new Random();
System.out.println("#######################################################");
System.out.println("# A treasure has been hidden at a location in a 10x10 #");
System.out.println("# grid. Guess where it is. You have 10 chances. #");
System.out.println("#######################################################");
/**
* Assigning -1 to nowX and nowY.
**/
nowX=-1;
nowY=-1;
/**
* Assigning random integer value between 1 to 10 to both trueX and trueY separately.
**/
trueX=random.nextInt(10)+1;
trueY=random.nextInt(10)+1;
/**
* For loop execution
**/
for(int i=1;i<=10;i++) {
if(nowX!=trueX || nowY!=trueY) {
System.out.println(" ---- Round "+i+" ----");
if(i>=2) {
show();
}
prevX=nowX;
prevY=nowY;
System.out.print("Enter your guess as x y ::");
nowX=sc.nextInt();
nowY=sc.nextInt();
checkDistance();
if(i>=2 && (nowX != trueX || nowY!=trueY)) {
advice();
}
}
}
System.out.println("Treasure was at location :: "+trueX+" , "+trueY);
}/*Main method ends here*/
public static void show() {
for(int y=10;y>=1;y--) {
for(int x=1;x<=10;x++) {
if(x==nowX && y==nowY) {
System.out.print("C");
}else if(x==prevX && y==prevY) {
System.out.print("P");
}else {
System.out.print(".");
}
}/*Inner for loop ends*/
System.out.println(" "+y);
}
}
public static void checkDistance() {
final int CLOSE=3;
final int FAR=6;
int distance = Math.abs(trueX-nowX) + Math.abs(trueY-nowY);
if(distance==0) {
System.out.println("You have found the treasure!");
}else if(distance>=1 && distance<=3) {
System.out.println("The distance is no more than 3.");
}else if(distance>=4 && distance<=6) {
System.out.println("The distance is no more than 6.");
}else {
System.out.println("The distance is more than 6.");
}
}/*checkDistance method ends here*/
public static void advice() {
int presentDistance = Math.abs(trueX-nowX) + Math.abs(trueY-nowY);
int previousDistance = Math.abs(trueX-prevX) + Math.abs(trueY-prevY);
int difference=presentDistance-previousDistance;
if(difference==0) {
System.out.println("The same distance");
}else if(difference>=1) {
System.out.println("You are farther.");
}else {
System.out.println("You are closer.");
}
}/*advice method ends here*/
}
OUTPUT::
#######################################################
# A treasure has been hidden at a location in a 10x10 #
# grid. Guess where it is. You have 10 chances. #
#######################################################
---- Round 1 ----
Enter your guess as x y ::3 4
The distance is no more than 6.
---- Round 2 ----
.......... 10
.......... 9
.......... 8
.......... 7
.......... 6
.......... 5
..C....... 4
.......... 3
.......... 2
.......... 1
Enter your guess as x y ::5 6
The distance is no more than 6.
The same distance
---- Round 3 ----
.......... 10
.......... 9
.......... 8
.......... 7
....C..... 6
.......... 5
..P....... 4
.......... 3
.......... 2
.......... 1
Enter your guess as x y ::8 9
The distance is more than 6.
You are farther.
---- Round 4 ----
.......... 10
.......C.. 9
.......... 8
.......... 7
....P..... 6
.......... 5
.......... 4
.......... 3
.......... 2
.......... 1
Enter your guess as x y ::2 3
The distance is no more than 6.
You are closer.
---- Round 5 ----
.......... 10
.......P.. 9
.......... 8
.......... 7
.......... 6
.......... 5
.......... 4
.C........ 3
.......... 2
.......... 1
Enter your guess as x y ::3 4
The distance is no more than 6.
The same distance
---- Round 6 ----
.......... 10
.......... 9
.......... 8
.......... 7
.......... 6
.......... 5
..C....... 4
.P........ 3
.......... 2
.......... 1
Enter your guess as x y ::4 4
The distance is no more than 6.
You are farther.
---- Round 7 ----
.......... 10
.......... 9
.......... 8
.......... 7
.......... 6
.......... 5
..PC...... 4
.......... 3
.......... 2
.......... 1
Enter your guess as x y ::5 5
The distance is no more than 6.
The same distance
---- Round 8 ----
.......... 10
.......... 9
.......... 8
.......... 7
.......... 6
....C..... 5
...P...... 4
.......... 3
.......... 2
.......... 1
Enter your guess as x y ::6 5
The distance is more than 6.
You are farther.
---- Round 9 ----
.......... 10
.......... 9
.......... 8
.......... 7
.......... 6
....PC.... 5
.......... 4
.......... 3
.......... 2
.......... 1
Enter your guess as x y ::7 6
The distance is more than 6.
The same distance
---- Round 10 ----
.......... 10
.......... 9
.......... 8
.......... 7
......C... 6
.....P.... 5
.......... 4
.......... 3
.......... 2
.......... 1
Enter your guess as x y ::7 6
The distance is more than 6.
The same distance
Treasure was at location :: 2 , 8
Please provide the feedback!!
Thank You!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.