Java Program
Your objective Your goal is to lead Suzie to the cheese in these dangerous times. Phase 1: The BigCity 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). Suzie's 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 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 in the 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 [1] cheese Positions) which creates a grid. It assigns the 2D array instance of cheese positions to the cheese Positions parameter passed. A private method fillGrid() that: marks Suzie's start position at (0,0) with 's' marks the positions in cheese Positions as 'b'. 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 (also could be 3, 1, or O but not greater than 3) cheese Positions = {{3, 1) {0,3}} (the number of rows is always numCheese, the number of of columns is always 2. Each row is the 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. Phase 2: Loading BigCity from files We want to be able to load a city from a file. The format is as follows: First line - The number of rows and the number of columns of the city grid The next (#rows) lines - The city grid separated by whitespace The following line - The number of boxes Next line - The number of cheese Next (numCheese) lines - The positions of cheese separated by a space in the format: row column For example: 65 S. b bb 4 2 3 @ 12 is a valid file with a 6x5 city grid with 4 boxes and 2 cheese at locations (3,0) and (1,2). To do this, add the following to your BigCity class: • Add a second constructor BigCity(String fileName). This constructor reads the data in the text file specified by "fileName". Do not forget to initialize your instance variables. This method should throw an IOException with an appropriate message if the data is not valid. Special exception messages include: File specified was not found Bad dimensions for the grid Inaccurate number of rows of data to read Inaccurate number of columns of data to read
Your error messages should have as much detail as possible. See the sample output for what is expected Sample output 1 Input file: S. b 1 1 11 Exception Thrown: java.io.IOException: No dimension to read Sample output 2 Input file: 22 S. .b 1 1 Exception Thrown: java.io.IOException: Inaccurate number of rows of cheese positions in the file. Saw o expected 1 Sample output 3 Input file: 4 5 S. b 1 1 11 Exception Thrown: java.io.IOException: Inaccurate number of columns of cheese in the file. Saw 4 expected 5
Do not bother error-checking the individual characters on the grid - the only thing you should check is if the number of rows and columns match what was provided. If they do, assume that the grid is "good". Note: you do not need to do all the error handling in this phase to start working on the next phase. Phase 3: Moving through BigCity Add the following to enable moving along the city grid. Create 2 count variables: one to track how many cheese crumbs Suzie has collected, and one to track how many moves Suzie has made moving through the grid. Do not forget to update both constructors to initialize them. Create 1 boolean instance variable to indicate that Suzie is still loose and roaming the city. Create a method void move(char direction). This will move Suzie in the direction specified. The following characters will represent the movements along the grid 'w'-up 's' - down 'a'-left d'-right Throw appropriate IndexOutOfBoundsException for moving off the grid. If the move is valid, then process the move using the following helper method. Create method private void process Move(char direction), which determines the next outcome of the game based on the direction from the move() method. A move onto a valid path() increases the number of moves Suzie has made. A move onto a 'b' can either: increase Suzie's cheese collection by 1 (as well as her move count), or end the game (and increase her move count) by calling the endTerror() method you will implement in the next phase. Update the grid to show Suzie's movements. If she moves, replace her last position in the grid with and set the char at the new position to 's'. If Suzie collects a cheese, replace the 'b' with 's' to show where she currently is. When she moves from that location, update the grid accordingly.
Java Program
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am