can someone help me create a java game of life using these java headers, and met
ID: 3545780 • Letter: C
Question
can someone help me create a java game of life using these java headers, and methods? the headers , main methods are in red color, and wht to do with them are in black. theout put should look like the one below.
/*******************************************************
Useful class constants. Put these immediately after the
class declaration header and before the main method.
*/
static final int DEAD = 0 ;
static final int ALIVE = 1 ;
/*******************************************************
Count the ALIVE neighbors of a cell.
@param x row in grid
@param y col in grid
@param grid the grid of ALIVE and DEAD cells
@return the number of ALIVE neighbors of grid[x][y] not counting
grid[x][y] itself
*/
static int countNeighbors( int x, int y, int[][] grid )
For example with the Step 1 grid shown above:
countNeighbors( 4, 4, grid ) is 1
countNeighbors( 3, 5, grid ) is 3
/*******************************************************
Compute one generation of the game
@param fromGrid the starting generation
@param toGrid the next step generation from the fromGrid
*/
static void oneGeneration( int[][] fromGrid, int [][] toGrid )
For example, if grid1 was the first grid shown and grid2 was the second
then this would compute grid2 from grid1
oneGeneration( grid1, grid2 )
/*******************************************************
Draw a game grid
@param the grid to be drawn
*/
static void drawgrid( int[][] grid )
/*******************************************************
Prompt the user to enter the ALIVE cell locations for a grid
*/
static void getAliveCells(int[][] grid)
This prompts the user as is shown in the first image and sets
the ALIVE locations. It does not accept bad locations (no index
out of bounds exceptions).
/*******************************************************
Run the game under user input
*/
static void doGame(int[][] grid1, int[][] grid2 )
This method asks the user for the number of steps to run and
displays each step. It repeats that until the user enters a
zero number of steps upon which it returns.
Here is what the startup of the game should look like. The command line specifies a 30 by 30 grid. The user enters the locations of three ALIVE cells. And, the program shows the initial grid using '*' for an ALIVE cell and '-' for a DEAD cell. Finally, the program prompts the user for the number of steps to run. Zero or negative steps exit the program. A positive number of steps causes the program to compute the next grid that many times and show the grid after each generation.
Here is what it should look like after the user specifies to go one step:
Explanation / Answer
import java.util.Scanner;
public class GameOfLife
{
/*******************************************************
Useful class constants. Put these immediately after the
class declaration header and before the main method.
*/
static final int DEAD = 0 ;
static final int ALIVE = 1 ;
/*******************************************************
Count the ALIVE neighbors of a cell.
@param x row in grid
@param y col in grid
@param grid the grid of ALIVE and DEAD cells
@return the number of ALIVE neighbors of grid[x][y] not counting
grid[x][y] itself
*/
static int countNeighbors( int x, int y, int[][] grid )
{
int count = 0;
for (int dx = x-1; dx <= x+1; dx++)
{
for (int dy=y-1; dy<=y+1; dy++)
{
if (dx >= 0 && dx < grid.length &&
dy >= 0 && dy < grid[0].length &&
grid[dx][dy] == ALIVE)
{
count++;
}
}
}
if (grid[x][y] == ALIVE)
count--;
return count;
}
/*******************************************************
Compute one generation of the game
@param fromGrid the starting generation
@param toGrid the next step generation from the fromGrid
*/
static void oneGeneration( int[][] fromGrid, int [][] toGrid )
{
for (int i = 0; i < fromGrid.length; i++)
{
for (int j = 0; j < fromGrid[0].length; j++)
{
int alive = countNeighbors(i, j, fromGrid);
if (fromGrid[i][j] == DEAD && alive == 3)
toGrid[i][j] = ALIVE;
else if (fromGrid[i][j] == ALIVE && alive <= 3 && alive >= 2)
toGrid[i][j] = ALIVE;
else
toGrid[i][j] = DEAD;
}
}
}
/*******************************************************
Draw a game grid
@param the grid to be drawn
*/
static void drawgrid( int[][] grid )
{
for (int i = 0; i < grid.length; i++)
{
for (int j = 0; j < grid[0].length; j++)
{
System.out.print((grid[i][j] == ALIVE) ? '*' : '-');
}
System.out.println();
}
}
/*******************************************************
Prompt the user to enter the ALIVE cell locations for a grid
*/
static void getAliveCells(int[][] grid)
{
System.out.println("Enter locations for live cells as i j on one line.");
System.out.println("Enter -1 for i to finish.");
Scanner scan = new Scanner(System.in);
System.out.print(" i j:");
int i = scan.nextInt();
while (i >= 0)
{
int j = scan.nextInt();
if (i >= 0 && i < grid.length && j >= 0 && j < grid[0].length)
grid[i][j] = ALIVE;
System.out.print(" i j:");
i = scan.nextInt();
}
}
/*******************************************************
Run the game under user input
*/
static void doGame(int[][] grid1, int[][] grid2 )
{
int step = 0;
Scanner scan = new Scanner(System.in);
do
{
System.out.println("Step=" + step);
drawgrid(grid1);
System.out.print("Num steps:");
step = scan.nextInt();
for (int i = 0; i < step; i++)
{
oneGeneration(grid1, grid2);
int[][] temp = grid1;
grid1 = grid2;
grid2 = temp;
}
} while (step > 0);
}
/**
* @param args
*/
public static void main(String[] args)
{
int size = Integer.parseInt(args[0]);
int[][] grid1 = new int[size][size];
System.out.println("Game of Life for " + size + " by " + size + " board.");
System.out.println();
getAliveCells(grid1);
doGame(grid1, new int[size][size]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.