Question
Suzie is a mouse in the big city. She needs to collect a lot of cheese that will last her throughout the cold winter months.
Suzie is a mouse in the big city. She needs to collect a lot of cheese that will last her throughout the cold winter months. Each day, Suzie travels to a new city to gather up all the cheese crumbs she can find. Cheese crumbs are gathered in boxes, so Suzie has to open each box to steal the cheese. News of the cheese prowler has spread across neighbouring cities like wildfire. Concerned cities determined to protect their precious cheese have hired exterminators to take out the notorious Suzie. So, she has to be extra careful now. Unbeknown to Suzie, exterminators now hide traps in some of these boxes, to trap Suzie when she opens it. Your goal is to lead Suzie to the cheese in these dangerous times A city is defined by an MxN grid (M may not be equal to N). The city will show the starting position of Suzie (denoted by s below). Suzies starting position is always at (0, 0).
The city will also show the boxes (denoted by b). Valid paths through the city are denoted by the . character.
The following is a 4x5 city grid (4 rows and 5 columns), with 3 boxes which may contain cheese or a trap! s . . b . . . . . . . . . . . . b . . b
First create the city grid, named class BigCity.
The BigCity class should have:
The following instance variables: a 2D array of char to hold the grid an int to hold the number of boxes in the grid an int to hold the number of cheese boxes. This value is less than or equal to the number of boxes in the grid a 2D array of int to hold the positions of the boxes that contain cheese
A constructor BigCity(int rows, int cols, int numBoxes, int numCheese, int [ ][ ] cheesePositions) which creates a grid. It assigns the 2D array instance of cheese positions to the cheesePositions parameter passed.
A private method fillGrid() that:
marks Suzies start position at (0, 0) with s
marks the positions in cheesePositions as b. cheesePositions will never contain (0,0).
marks (numBoxes minus numCheese) random positions in the grid as b. It cannot mark (0, 0) or any positions in cheesePositions. You can create a private method to do this.
marks the remaining positions as .
This method should be called from the constructor you defined above.
A standard toString method, which returns a text representation of the city. Given the grid above, the constructor may be passed the following:
rows = 4 cols = 5 numBoxes = 3 numCheese = 2 (could be 3, 1, or 0 but not greater than 3)
cheesePositions = {{3, 1}, {0, 3}} (the number of rows is always numCheese, the number of columns is always 2. Each row is are coordinates of a piece of cheese.)
Given this sample input, you can tell that the box at (3, 4) contains a trap for the grid shown above, as it is not in the list of cheesePositions provided.
public static void main(String [] parms) { testGenerateGrid(); } public static void testGenerateGrid() { int [][] cheeseGridOne = {{3, 1}, {0, 3}}; int [][] cheeseGridTwo = {{2, 2}}; int [][] cheeseGridThree = {{3, 1}, {0, 2}, {4, 3}, {1, 0}}; int [][] cheeseGridFour = {{4, 3}}; BigCity game1 = new BigCity(4, 5, 3, 2, cheeseGridOne); System.out.println("This should print BigCity with: " + "s at (0,0), " + "b at (0,3), (3,1), with one random b: "); System.out.println(game1); BigCity game2 = new BigCity(3, 3, 1, 1, cheeseGridTwo); System.out.println("This should print BigCity with: " + "s at (0,0), " + "b at (2,2) " + "and no extra b's "); System.out.println(game2); BigCity game3 = new BigCity(7, 5, 8, 4, cheeseGridThree); System.out.println("This should print BigCity with: " + "s at (0,0), " + "b at (0,2), (1,0), (3,1), (4,3) " + "and 4 random b's "); System.out.println(game3); BigCity game4 = new BigCity(6, 6, 6, 1, cheeseGridFour); System.out.println("This should print BigCity with: " + "s at (0,0), " + "b at (4,3) " + "and 5 random b's "); System.out.println(game4); } }
the programing language is Java.
the codes below are a sample test, where a class should be created and the fillgrid method from the constructor should fill the positions and a toString should be used to print out the contents as they are described in the test phase" should print:".
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