A two-dimensional array (diagram available in class) represents an island surrou
ID: 3564428 • Letter: A
Question
A two-dimensional array (diagram available in class) represents an island surrounded by water (outside edges of the array). Two bridges lead off of the island - the location of each will be part of the input or generated with a random number. Make sure the bridges are not in the corners because they will be unreachable. A mouse is placed on one of the remaining land squares of the array - the location of which will also be determined by the input or random number. Write a program to make the mouse take a walk across the island. The mouse is allowed to travel one square at a time, either horizontally or vertically. A random number from 1 through 4 should be used to decide which direction the mouse is to take. The mouse drowns when he hits the water; he escapes when he enters a bridge. You may generate a random number for the walk up to 20 times. If the mouse does not find his way off the island by the 20th try, he will die of starvation. Restart the mouse in a reinitialized array (read new input) and go back and repeat the whole process 5 times. Count how many times he escapes, drowns, or starves and print those counts at the end of the program.
Input the size of the array (including the border of water - not larger than 10X10, so array can be easily seen on the screen), two bridges, and the starting position of the mouse. Bridges and mouse location can be also generated randomly. Set up the array with -1 for the water edges, 99 for the two bridge exits, and a 1 in the cell where the mouse starts. Print the starting array.
Output: After each trip of 20 tries (or less), print the following:
Explanation / Answer
class Array{ public static void main(String[] args) { // declares an array of integers int[] anArray; // allocates memory for 10 integers anArray = new int[10]; // initialize first element anArray[0] = 100; // initialize second element anArray[1] = 200; // etc. anArray[2] = 300; anArray[3] = 400; anArray[4] = 500; anArray[5] = 600; anArray[6] = 700; anArray[7] = 800; anArray[8] = 900; anArray[9] = 1000; System.out.println("Element at index 0: " + anArray[0]); System.out.println("Element at index 1: " + anArray[1]); System.out.println("Element at index 2: " + anArray[2]); System.out.println("Element at index 3: " + anArray[3]); System.out.println("Element at index 4: " + anArray[4]); System.out.println("Element at index 5: " + anArray[5]); System.out.println("Element at index 6: " + anArray[6]); System.out.println("Element at index 7: " + anArray[7]); System.out.println("Element at index 8: " + anArray[8]); System.out.println("Element at index 9: " + anArray[9]); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.