Question
Create program using Java CREATING THE ROOM Well be storing a single rooms worth of empty (.) versus wall-occupied (#) locations in a two dimensional
Create program using Java
CREATING THE ROOM
Well be storing a single rooms worth of empty (.) versus wall-occupied (#) locations in a two dimensional char array that is declared within your programs main method. In the future, we may wish to change the dimensions of this array or to store additional characters in it, so your code should be as general as possible with respect to these kinds of changes. For now, this room will be 20 characters wide by 10 characters tall. Each character represents the existence versus absence of a wall at a different location within the room, and these characters should be organized in row major order. This means that your two dimensional room array should initially contain 10 char arrays, which themselves each contain 20 characters.
After declaring (allocating memory for) your room array in the main method, youll implement and call a method with the following signature to initialize its contents. This method should correctly initialize any room array with any dimensions, to include any number of walls, and should make use of the provided random number generator to determine where those walls should be.
1 | public static void placeWalls(char[][] room, int numberOfWalls, Random randGen) {} |
The result of calling this method is that numberOfWalls randomly chosen characters within the room array will contain the hashtag symbol (#) representing walls, and that all of the other characters will contain a period (.) representing an empty location. After this method is implemented, tested, and working, you should call it from main() so that 20 walls are randomly positioned throughout the room. Note: You dont need to worry about cases where the number of chars in room is less than numberOfWalls for this assignment we wont attempt to test your code under such circumstances.
ADDING SOME CHEESE AND A MOUSE
Since the locations of cheese are likely to change throughout our simulation, well store and track them a bit differently than the walls. The position of each piece of cheese will be stored in an int array. The x-positions which increase from left (0) to right (19) should be stored at index zero within these arrays. And the y-positions which increase from top (0) to bottom (9) should be stored at index one within these arrays. Since well have many cheese to keep track of, well group their position arrays into a single two-dimensional array of ints. You should declare a single two-dimensional int array within main for this purpose, and it should be large enough to hold 10 cheese positions.
In order to initialize these cheese to begin at random starting locations, well implement and call a method with the following signature.
1 | public static void placeCheeses(int[][] cheesePositions, char[][] room, Random randGen) {} |
Note that we dont need to pass in a numberOfCheeses parameter, because that can be accessed through cheesePositions.length, and that we can also access the rooms dimensions through the room parameter. We do need to be careful about the random locations that we choose for these cheese though. Make sure that these cheese are not placed in locations that are occupied by walls, and also make sure that there is never more than one cheese placed at the same location.Since there is only a single mouse in our simulation, well simply declare two int variables in the main() method to separately store its x and y position. After declaring these variables, initialize them to a random unoccupied position: a location that does not contain cheese and does not contain a wall.
DRIVING THE SIMULATION
Implement and call the following method to help visualize the randomly positioned contents of our room:
1 | public static void printRoom(char[][] room, int[][] cheesePositions, int mouseX, int mouseY) {} |
The priorities that these objects should be displayed (in case they are ever in the same positions) is that the mouse should always appear in its specified position, cheese pieces should have the second highest priority, and in positions that contain neither mouse nor cheese, you should display whatever character is stored in room for that position (usually hashtags and periods, but should work for any other characters that might be stored in there as well). Each row of room contents should be displayed on a separate line, so that the result appears similar to the example at the top of this write-up. In order to move the mouse around this room, well be relying on movement commands entered by the user. Implement and call the following method to help facilitate moving this mouse around. Note that this method does not read any input directly from the user, but instead relies on the char parameter named move for this input.
1 | public static int[] moveMouse(int mouseX, int mouseY, char[][] room, char move) {} |
The move char passed into this method is expected to be either upper or lower case: W (for move up), A (for move left), S (for move down), or D (for move right). When any other move character is passed into this method, it should print the following warning message to System.out before returning null: WARNING: Didnt recognize move command:
WARNING: Mouse cannot move outside the room. when the move command would otherwise moves the mouse outside the bounds of the room array.
WARNING: Mouse cannot move into wall. when the move command would otherwise move the mouse into a position occupied by a wall.
When none of these potential problems are detected, this method should return an array of two integers representing the updated mouse position that results from moving the original mouseX, mouseY position according to the move command. The zero index of this array that is returned should hold the updated mouseX, and the one index should hold the updated mouseY.
Your main method will drive this simulation. After randomly positioning the initial walls, cheeses, and mouse, it should ask the user to enter the number of steps that the simulation should be run. This number of steps does not include steps in which a WARNING is printed. Each of these simulation steps will involve 1) printing the rooms contents, 2) asking the user to enter the next move command for the mouse, and then 3) moving the mouse according to that command. Heres an example trace demonstrating how your program should behave at this point. The bold orange text represent user input, and the rest of this text was printed by the program.
Welcome to the Cheese Eater simulation. ======================================= Enter the number of steps for this simulation to run: 3 .#..#..%#........#.. ......%#..#......... .#.....%...###....#. ......%#.....#.....# .................... ...#%#....%......... #.......%........#.. ........#%.....@.... ....%.......#....... ....%............... Enter the next step you'd like the mouse to take (WASD): s .#..#..%#........#.. ......%#..#......... .#.....%...###....#. ......%#.....#.....# .................... ...#%#....%......... #.......%........#.. ........#%.......... ....%.......#..@.... ....%............... Enter the next step you'd like the mouse to take (WASD): d .#..#..%#........#.. ......%#..#......... .#.....%...###....#. ......%#.....#.....# .................... ...#%#....%......... #.......%........#.. ........#%.......... ....%.......#...@... ....%............... Enter the next step you'd like the mouse to take (WASD): d .#..#..%#........#.. ......%#..#......... .#.....%...###....#. ......%#.....#.....# .................... ...#%#....%......... #.......%........#.. ........#%.......... ....%.......#....@.. ....%............... ================================================== Thank you for running the Cheese Eater simulation.
Be sure to include the exact Welcome , Thank you , and Enter the messages/prompts displayed in this example, so that the automated grading tests are able to find them when running your program. There is also another example in the next step that illustrates the display of some WARNING messages.
The final step of this assignment is to implement and make use of one final method:
1 | public static boolean tryToEatCheese(int mouseX, int mouseY, int[][] cheesePositions) {} |
After each move of the mouse your main() simulation loop should call this method to give the mouse a chance to eat any cheese that it shares a location with. When the mouse and any cheese are in the same position, this method should update the position of that cheese to the sentinel position { -1, -1 } before returning true to indicate that this cheese was eaten by the mouse. If the mouse is not sharing a position with any cheese when this method is called, it should instead return false to indicate that no cheese was eaten. Note that moving eaten cheeses to position -1,-1 should effectively remove them from the simulation, since that position is never draw to the screen and the mouse cannot reach that position to eat such cheeses again.
Welcome to the Cheese Eater simulation. ======================================= Enter the number of steps for this simulation to run: 3 The mouse has eaten 0 cheese! ............#.@..##% ...%...#.......##... ......%...%..#..#... .....#..#..#.......% #.......%...#...#... .#.......%.........# ...%..........%..... .................... .....#..#.........#. .#........%......... Enter the next step you'd like the mouse to take (WASD): d The mouse has eaten 0 cheese! ............#..@.##% ...%...#.......##... ......%...%..#..#... .....#..#..#.......% #.......%...#...#... .#.......%.........# ...%..........%..... .................... .....#..#.........#. .#........%......... Enter the next step you'd like the mouse to take (WASD): S WARNING: Mouse cannot move into wall. The mouse has eaten 0 cheese! ............#..@.##% ...%...#.......##... ......%...%..#..#... .....#..#..#.......% #.......%...#...#... .#.......%.........# ...%..........%..... .................... .....#..#.........#. .#........%......... Enter the next step you'd like the mouse to take (WASD): w WARNING: Mouse cannot move outside the room. The mouse has eaten 0 cheese! ............#..@.##% ...%...#.......##... ......%...%..#..#... .....#..#..#.......% #.......%...#...#... .#.......%.........# ...%..........%..... .................... .....#..#.........#. .#........%......... Enter the next step you'd like the mouse to take (WASD): A The mouse has eaten 0 cheese! ............#.@..##% ...%...#.......##... ......%...%..#..#... .....#..#..#.......% #.......%...#...#... .#.......%.........# ...%..........%..... .................... .....#..#.........#. .#........%......... Enter the next step you'd like the mouse to take (WASD): s The mouse has eaten 0 cheese! ............#....##% ...%...#......@##... ......%...%..#..#... .....#..#..#.......% #.......%...#...#... .#.......%.........# ...%..........%..... .................... .....#..#.........#. .#........%......... ================================================== Thank you for running the Cheese Eater simulation.
The last thing that wed like you to add to this simulation is a printout displaying the number of cheese pieces that have been eaten by the mouse. This should appear just before the room is printed (and every time it is printed), as is demonstrated above.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started