java please! public static void placeWalls(char[][] room, int numberOfWalls, Ran
ID: 3879670 • Letter: J
Question
java please!
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 don’t need to worry about cases where the number of chars in room is less than numberOfWalls for this assignment… we won’t attempt to test your code under such circumstances.
Since the locations of cheese are likely to change throughout our simulation, we’ll 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 we’ll have many cheese to keep track of, we’ll 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, we’ll implement and call a method with the following signature.
1
public static void placeCheeses(int[][] cheesePositions, char[][] room, Random randGen) {}
Note that we don’t need to pass in a numberOfCheeses parameter, because that can be accessed through cheesePositions.length, and that we can also access the room’s 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.
1
public static void placeCheeses(int[][] cheesePositions, char[][] room, Random randGen) {}
Explanation / Answer
Given below are the 2 functions based on their specifications
public static void placeWalls(char[][] room, int numberOfWalls, Random randGen)
{
int rows = room.length;
int cols = room[0].length;
//intialize with .
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
room[i][j] = '.';
int n = 0, r, c;
while(n < numberOfWalls)
{
do
{
r = randGen.nextInt(rows);
c = randGen.nextInt(cols);
}while(room[r][c] != '.'); //until we don't find the position to be empty
room[r][c] = '#'; //place the wall
n++;
}
}
//==============================================
public static void placeCheeses(int[][] cheesePositions, char[][] room, Random randGen)
{
int maxX = room[0].length; //no. of columns are the max value for x
int maxY = room.length; //no. of rows are the max value for y
int x, y;
for(int i = 0; i < cheesePositions.length; i++)
{
do
{
x = randGen.nextInt(maxX);
y = randGen.nextInt(maxY);
}while(room[y][x] == '#'); //as long as there is wall at the generated x, y, generate another
cheesePositions[i][0] = x;
cheesePositions[i][1] = y;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.