Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exercise IV - Treasure Hunt.java You woke up in the middle of a dark cave. You're told by a voice in the shadows that your

image text in transcribedimage text in transcribedimage text in transcribed

Exercise IV - Treasure Hunt.java You woke up in the middle of a dark cave. You're told by a voice in the shadows that your goal is to look for a gold chest. Since you can't see around you, you do not have any clues, other than the voice in the shadows telling you the Manhattan distance between you and the chest after every move. More formally, you can think of the layout of the cave as a 11 x 11 grid, and the position of the chest is randomly generated by the program at every run. While you haven't found the chest yet, and as long as you're still in the 11 x 11 grid, you are asked to make a move, and after every move, you're told how far you are from the chest. If you mistakenly exit the grid, the program terminates The Manhattan distance is equal to the sum of the absolute difference in x and the absolute difference in y (Hint for generating the coordinates of the gold chest: Math.random() returns a double between 0 and 1, so use that in a mathematical expression to randomly generate the chest's coordinates at the beginning of your program) Below you can find a drawn example: The player (in green) is initially located at the cell (0, 0), and the grid extends from (-5, -5) (bottom left) to (5,5) (top right). The coordinates for the chest (in yellow) are randomly generated (here, they're equal to (2, 3)). Note that the randomly generated coordinates for the chest should NOT be at the border of the grid, i.e. the x and y coordinates of the chest should be at least -4 and at most 4. Below you can find an example run (using the same randomly generated coordinates for the chest as the figure above): Welcome to treasure hunt! Treasure coordinates randomly generated... Manhattan distance to chest: 5 Enter direction (U, D, L, R): L Manhattan distance to chest: 6 Enter direction (U, D, L, R): R Manhattan distance to chest: 5 Enter direction (U, D, L, R): R Manhattan distance to chest: 4 Enter direction (U, D, L, R): R Manhattan distance chest: 3 Enter direction (U, D, L, R): R Manhattan distance to chest: 4 Enter direction (U, D, L, R): L Manhattan distance to chest: 3 Enter direction (U, D, L, R): D Manhattan distance to chest: 4 Enter direction (U, D, L, R): U Manhattan distance to chest: 3 Enter direction (U, D, L, R): U Manhattan distance to chest: 2 Enter direction (U, D, L, R): U Manhattan distance to chest: 1 Enter direction (U, D, L, R): U Treasure found! Below you can find an example run where the player goes out of bounds (using the same randomly generated coordinates for the chest as the figure above): Welcome to treasure hunt! Treasure coordinates randomly generated... Manhattan distance to chest: 5 Enter direction (U, D, L, R): L Manhattan distance to chest: 6 Enter direction (U, D, L, R): L Manhattan distance to chest: 7 Enter direction (U, D, L, R): L Manhattan distance to chest: 8 Enter direction (U, D, L, R): L Manhattan distance to chest: 9 Enter direction (U, D, L, R): L Manhattan distance to chest: 10 Enter direction (U, D, L, R): L You reached the end of the cave... you lost! Exercise IV - Treasure Hunt.java You woke up in the middle of a dark cave. You're told by a voice in the shadows that your goal is to look for a gold chest. Since you can't see around you, you do not have any clues, other than the voice in the shadows telling you the Manhattan distance between you and the chest after every move. More formally, you can think of the layout of the cave as a 11 x 11 grid, and the position of the chest is randomly generated by the program at every run. While you haven't found the chest yet, and as long as you're still in the 11 x 11 grid, you are asked to make a move, and after every move, you're told how far you are from the chest. If you mistakenly exit the grid, the program terminates The Manhattan distance is equal to the sum of the absolute difference in x and the absolute difference in y (Hint for generating the coordinates of the gold chest: Math.random() returns a double between 0 and 1, so use that in a mathematical expression to randomly generate the chest's coordinates at the beginning of your program) Below you can find a drawn example: The player (in green) is initially located at the cell (0, 0), and the grid extends from (-5, -5) (bottom left) to (5,5) (top right). The coordinates for the chest (in yellow) are randomly generated (here, they're equal to (2, 3)). Note that the randomly generated coordinates for the chest should NOT be at the border of the grid, i.e. the x and y coordinates of the chest should be at least -4 and at most 4. Below you can find an example run (using the same randomly generated coordinates for the chest as the figure above): Welcome to treasure hunt! Treasure coordinates randomly generated... Manhattan distance to chest: 5 Enter direction (U, D, L, R): L Manhattan distance to chest: 6 Enter direction (U, D, L, R): R Manhattan distance to chest: 5 Enter direction (U, D, L, R): R Manhattan distance to chest: 4 Enter direction (U, D, L, R): R Manhattan distance chest: 3 Enter direction (U, D, L, R): R Manhattan distance to chest: 4 Enter direction (U, D, L, R): L Manhattan distance to chest: 3 Enter direction (U, D, L, R): D Manhattan distance to chest: 4 Enter direction (U, D, L, R): U Manhattan distance to chest: 3 Enter direction (U, D, L, R): U Manhattan distance to chest: 2 Enter direction (U, D, L, R): U Manhattan distance to chest: 1 Enter direction (U, D, L, R): U Treasure found! Below you can find an example run where the player goes out of bounds (using the same randomly generated coordinates for the chest as the figure above): Welcome to treasure hunt! Treasure coordinates randomly generated... Manhattan distance to chest: 5 Enter direction (U, D, L, R): L Manhattan distance to chest: 6 Enter direction (U, D, L, R): L Manhattan distance to chest: 7 Enter direction (U, D, L, R): L Manhattan distance to chest: 8 Enter direction (U, D, L, R): L Manhattan distance to chest: 9 Enter direction (U, D, L, R): L Manhattan distance to chest: 10 Enter direction (U, D, L, R): L You reached the end of the cave... you lost

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

Step: 3

blur-text-image

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

Introduction To Finance

Authors: Lawrence J Gitman, Jeff Madura

1st Edition

0201635372, 9780201635379

More Books

Students also viewed these Finance questions

Question

Evaluate three pros and three cons of e-prescribing

Answered: 1 week ago