Question

Monster ARray Summary: Write a program that shows the path of an imaginary monster moving through a two dimensional array try

But to make our output clearer, print 0s (zeros) out as periods (dots), and is as # symbols. Once your program can produce th

Requirements: The program should: A. Declare a 40x40 array B. Place the values 2 thru 9 (8 numbers total) randomly into the a

Examine the previous pages carefully and write your program to produce output at least as informative as the sample output sh

### starting array ### 4 7 Starting at position 0,0, track the progress of a monster as it heads towards the largest value

### Monster on the move! ### ### The monster wants the 7 ### 7 2 7 Output continued on next page

.. # . . . . 2 Note: the monster accidentally ate the 4 on the way to the 7, Thats ok

7. 7 Its almost there ..

6 ### Monster ate the 7 ! ### The monster has reached its target. Next, from its current place in the array, it will start mo

this is advance java

Monster ARray Summary: Write a program that shows the path of an imaginary monster moving through a two dimensional array trying to "eat" the highest values in the array. As the monster goes, it leaves a trail behind it. Imagine a 40x40 array, displayed like this, where Os are printed as periods (dots), and non-zero valués are printed as normal. Then, place the values 2 through 9 randomly on the array. The rest of the document assumes an 8 x 8 array rather than 40 x 40, just to make it easier to show. After the array has been declared and the values randomly placed, it might look like this. A 00000300 00400002 00007000 00500000 06000000
But to make our output clearer, print 0s (zeros) out as periods (dots), and is as # symbols. Once your program can produce this output, write code that will simulate a monster that starts at elementlol[이 and then heads as directly as it can to "eat" the largest value in the array. As it goes, it changes the value of each element it visits to 1. Then, starting at position 0,0, track the progress of a monster" as it heads towards the largest value in the array. Replace every cell he visits with the value "1. If he accidentally hits a cell with another lower number in it on that way ...oh well ..it's gone. The next several pages show sample output, with explanations. Print it like this, for clarity. Sample output is shown in green, comments in blue.
Requirements: The program should: A. Declare a 40x40 array B. Place the values 2 thru 9 (8 numbers total) randomly into the array. C. Print the array, replacing 0 with periods and 1 with # Use a loop, write code that will move an imaginary moster that starts at position (01IO) and move to the current largest value in the array. Each element the monster passes is replaced by a 1 on the way E. The algorithm that selects the path to follow is up to you. But no diagonal moves are allowed F. Every time the monster moves to a new position, print the array again. G. When the monster reaches its target, print a message indicating that it reached it. H. The monster should then begin moving from its current position to whatever is now the highest value in the array. Simulation ends when there are no values higher than 1 on the board. I.
Examine the previous pages carefully and write your program to produce output at least as informative as the sample output shown. J. All will be clear when you look at the sample output with comments on the next page.
### starting array ### 4 7 Starting at position 0,0, track the progress of a "monster" as it heads towards the largest value in the array. Replace every cell he visits with the value "1. If he accidentally hits a cell with another lower number in it, oh well, it's gone. As you can see in the array above, 7 is the highest value in the array, so the monster is heading to it. The monster cannot move diagonally. Howyou determine the path it follows is up to you. You don't have to implement the same algorithm I did. Since the monster begins at 0,0, we put a 1 in the array at that position. But, remember, we display Os as dots and is as # symbols.
### Monster on the move! ### ### The monster wants the 7 ### 7 2 7 Output continued on next page
.. # . . . . 2 Note: the monster accidentally ate the 4 on the way to the 7, That's ok
7. 7 It's almost there ..
6 ### Monster ate the 7 ! ### The monster has reached its target. Next, from its current place in the array, it will start moving towards the 6, which is now the highest number in the array. It will stay at the current When it reaches the six,it will move towards whatever is then the highest number in the array So, next we would see.. ### Monster on the move! ### ### The monster wants the 6 ### And the process would continue until the array nothing but 0s and 1s, the simulation is over
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// save it as Snake2d.java    thanks

import java.util.*;

public class Snake2d {

   static int rows= 40, cols= 40;   //set these values only (array size) set any numbers as you wish
   static int sleeeptime = 100; //time beatween each move milli seconds

   static int sourceR, sourceC, destR, destC;
   static int snake[][] = new int[rows][cols];


   public static void fill()
   {
       int cakes = 8; // how many cakes to eat
       Random rand = new Random();
       List<Integer> randomNumbers = new ArrayList<Integer>(cakes);

       //first fill all to 0
       for (int a = 0; a < rows; a++)
           for (int b = 0; b < cols; b++)
               snake[a][b]=0;
       //--------------------

       for (int i = 2; i<= 9; i++) {
randomNumbers.add(i);
}
       Collections.shuffle(randomNumbers);

       for (int i = 0; i< cakes; i++) {
int randomRow = rand.nextInt(((rows-1) - 0) + 1) + 0;   //random row index
           int randomCol = rand.nextInt(((cols-1) - 0) + 1) + 0;   //random col index
           if ( snake[randomRow][randomCol]== 0) // if there is already 0 then replace with randomNum
           {
               snake[randomRow][randomCol]= randomNumbers.get(i);
           }
           else
           {
               i--;
               continue;
           }
}
   }

   public static void print()
   {
       for (int a = 0; a < rows; a++)
       {
           for (int b = 0; b < cols; b++)
           {
               switch (snake[a][b])
               {
                   case -1:
                       System.out.print("@ "); // use # here if you dont want @ as monster head
                       break;
                   case 1:
                       System.out.print("# ");
                       break;
                   case 0:
                       System.out.print(". ");
                       break;
                   default:
                       System.out.print( snake[a][b]+" ");
                       break;
               }


           }
           System.out.println();
       }
       System.out.println();
       System.out.println();
   }

   public static void move(int i, int j, int r, int c)throws Exception
   {

       snake[i][j] = 1;
       if (i<r)
       {
           while (i!=r)
           {
               i++;
               snake[i][j] = -1;
               print();
               snake[i][j] = 1;
               Thread.sleep(sleeeptime);
           }
       }
       else if (i>r)
       {
           while (i!=r)
           {
               i--;
               snake[i][j] = -1;
               print();
               snake[i][j] = 1;
               Thread.sleep(sleeeptime);
           }
       }

       if (j<c)
       {
           while (j!=c)
           {
               j++;
               snake[i][j] = -1;
               print();
               snake[i][j] = 1;
               Thread.sleep(sleeeptime);
           }
       }
       else if (j>c)
       {
           while (j!=c)
           {
               j--;
               snake[i][j] = -1;
               print();
               snake[i][j] = 1;
               Thread.sleep(sleeeptime);
           }
       }


       System.out.println( i+ " " +j );
       sourceR = i; //update source row index
       sourceC = j; //update source col index
       print();
       Thread.sleep(1000); // pause for 1 sec after eating

   }

   public static void main (String args[])throws Exception
   {
       sourceR = 0;
       sourceC = 0;

       fill();       //fill array randomly

       int max = 0;
       while(max!=1)
       {
           //find max number
           for(int i=0; i<rows; i++){
               for(int j =0; j<cols; j++){
                       if(max<snake[i][j]) //max number is found at i j location
                       {  
                           max = snake[i][j];
                           destR = i; //mark destination row
                           destC = j; //mark destination col
                       }
               }
           }
           if (max==1)
           {
               break;   // if no max number is left other than 1, then exit program
           }
           else
           {
               System.out.println("### Monster on the move! ###");
               System.out.println("### Monster wants "+ max + " ###");
               Thread.sleep(2000);
           }

           max = 0;
           move(sourceR, sourceC, destR, destC);   // move towards max number
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
this is advance java Monster ARray Summary: Write a program that shows the path of an imaginary monster moving through a two dimensional array trying to "eat&#3...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA Write a program that uses a two-dimensional array to store daily minutes walked and...

    IN JAVA Write a program that uses a two-dimensional array to store daily minutes walked and carb intake for a week. Prompt the user for 7 days of minutes walked and carb intake; store in the array.   Write a sort ( your choice which sort ) to report out the sorted minute values -lowest to highest. Write another to report out the sorted carb intake - highest to lowest. These methods MUST be your original code.   Your program should output...

  • Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...

    Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...

  • Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that...

    Program 7 File Processing and Arrays (100 points) Overview: For this assignment, write a program that will process monthly sales data for a small company. The data will be used to calculate total sales for each month in a year. The monthly sales totals will be needed for later processing, so it will be stored in an array. Basic Logic for main() Note: all of the functions mentioned in this logic are described below. Declare an array of 12 float/doubles...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Summary Write a program that demonstrates the skills you’ve learned throughout this quarter. This type of...

    Summary Write a program that demonstrates the skills you’ve learned throughout this quarter. This type of project offers only a few guidelines and requirements, allowing you to invest as much time, effort and imagination as you want.  Submit your java programs (*.java) and any other I/O (*.txt) via Canvas  You’ve worked quite hard to make it this far, so have a bit of fun with this project! Design Brief: Use Case Scenario You are hired to develop a...

  • Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate...

    Python Program Eratosthenes of Cyrene lived approximately 275-195 BC. He was the first to accurately estimate the diameter of the earth. For several decades he served as the director and chief librarian of the famous library in Alexandria. He was highly regarded in the ancient world, but unfortunately only fragments of his writing have survived. The algorithm described for this assignment is known as the Sieve of Eratosthenes. The algorithm is designed to find all prime numbers within a given...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

  • I have this case study to solve. i want to ask which type of case study...

    I have this case study to solve. i want to ask which type of case study in this like problem, evaluation or decision? if its decision then what are the criterias and all? Stardust Petroleum Sendirian Berhad: how to inculcate the pro-active safety culture? Farzana Quoquab, Nomahaza Mahadi, Taram Satiraksa Wan Abdullah and Jihad Mohammad Coming together is a beginning; keeping together is progress; working together is success. - Henry Ford The beginning Stardust was established in 2013 as a...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT