\'m in java one and need help with this code. please follow the given directions
ID: 3688774 • Letter: #
Question
'm in java one and need help with this code. please follow the given directions and test your program to make sure it works as it is required. I will apreciate your help. thank you.please use the given code.
here is the code cp ~svlasnik/1400samples/Life.java NetID_Life.java
You are going to be implementing the classic computer science simulation, Conway's Game of Life Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix in the display empty Each cell has a "neighborhood" consisting of the eight cells in every direction around it, including diagonals. Cells on the edges of the matrix may not have eight "true" neighbors, but we will assume that their neighbors that would be off the matrix are considered to always be dead cells From generation to generation, whether or not a cell survives or has life born into it depends on how many neighbors it has. Some examples follow. Please note that these eight examples are not the only possible scenarios that your program might encounter. In each of these examples, we are focusing on what happens to the middle cell in these examples from one generation to the next .A dead middle cell with exactly three live neighbors becomes a live cell in the next generation (birth) The middle cell would become live in the next generation because it has exactly 3 live neighbors The middle cell would become live in the * next generation because it has exactly 3 live neighbors A live middle cell with two or three live neighbors stays alive in the next generation (survival) The middle cell would survive to the next *The middle cell would survive because it **generation because it has two live has three live neighbors neighborsExplanation / Answer
Life.java
// This program uses a user chosen grid pattern to show the cell lifecycle by generation of little stars.
import java.util.Scanner;
import java.util.Random;
public class Life
{
// the size of the grid (GRIDSIZE x GRIDSIZE)
final private static int GRIDSIZE = 18;
/********************************************************************************/
public static void main ( String args[] )
{
boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE];
char choice;
int x = 1;
Scanner sc = new Scanner ( System.in );
do
{
System.out.print ( "Start with a (r)andom board, the (q)ueen bee shuttle or the (g)lider pattern? ");
choice = sc.next().charAt(0);
} while ( choice != 'r' && choice != 'q' && choice != 'g' );
clearGrid (board);
setup(board,choice);
do
{
System.out.printf ("Viewing generation #%d: ", x++);
displayGrid(board);
genNextGrid(board);
System.out.print (" (q)uit or any other key + ENTER to continue: ");
choice = sc.next().charAt(0);
} while ( choice != 'q' );
}
/********************************************************************************/
public static void setup (boolean[][] board, char which )
{
Random randomNumbers = new Random();
clearGrid(board);
if ( which == 'q' )
{
// Set up the Queen Bee Shuttle pattern
board[5][1] = true;board[5][2] = true;board[6][3] = true;board[7][4] = true;
board[8][4] = true;board[9][4] = true;board[10][3] = true;board[11][2] = true;
board[11][1] = true;
}
else if ( which == 'g' )
{
// Set up a Glider
board [17][0] = true; board[16][1] = true; board[15][1] = true;
board[16][2] = true;
board [17][2] = true;
}
else
{
// set up random
for (int row = 0; row < board.length; row++ )
{
for (int col = 0; col < board[row].length; col++ )
{
if ( randomNumbers.nextInt() % 2 == 0 )
board[row][col] = true;
}
}
}
}
/********************************************************************************/
public static void displayGrid (boolean[][] grid)
{
// Start printing the top row of numbers
System.out.print (" ");
for (int x = 1; x <= grid.length; x++)
{
if ((x / 10) != 0)
System.out.printf ( "%d", x / 10 );
else
System.out.print ( " " );
}
System.out.println();
System.out.print( " " );
for (int x = 1; x <= grid.length; x++)
{
System.out.printf ( "%d", x % 10 );
}
System.out.println();
for (int r = 0; r < grid.length; r++)
{
System.out.printf ( "%d", r+1 );
if (r + 1 < 10)
System.out.print ( " " );
else
System.out.print ( " " );
for (int c = 0; c < grid.length; c++)
{
if (grid[r][c] == true)
System.out.print ( "*" );
else
System.out.print ( " " );
}
System.out.println();
}
}
/*******************************************************************************/
// method to clear the grid
public static void clearGrid ( boolean[][] grid )
{
for (int row = 0; row < grid.length; row++ )
{
// for that sets cells to false
for (int col = 0; col < grid[row].length; col++ )
{
grid[row][col] = false;
}
}
}
// method to generate grid
public static void genNextGrid ( boolean[][] grid )
{
// copied array
boolean [][] copy = new boolean[GRIDSIZE][GRIDSIZE];
for (int row = 0; row < grid.length; row++)
{
for (int col = 0; col < grid[row].length; col++)
{
// if statement to check live cells around the cell in question and sets them to true or false
if (countNeighbors(grid, row, col) == 3 || (countNeighbors(grid, row, col) == 4 && grid[row][col]))
copy[row][col] = true;
else
copy[row][col] = false;
}
}
// copies array
for (int row = 0; row < copy.length; row++ )
{
for (int col = 0; col < copy[row].length; col++)
{
grid[row][col] = copy[row][col];
}
}
}
// method to count the living neighbors of the cell
public static int countNeighbors ( boolean[][] grid, int row, int col )
{
// initilized counter
int counter = 0;
// first for that starts at corner cell
for( int x = row - 1; x < row + 2; x++)
{
// if is limited to 17 because of grid size
if (x < 0 || x > 17)
continue;
// for loop to look at column
for(int y = col - 1; y < col + 2; y++)
{
if (y < 0 || y > 17)
continue;
if (grid[x][y])
counter++;
}
}
// returns counter to be used in another method
return counter;
}
}
sample output
111111111
123456789012345678
1
2
3
4
5
6 **
7 *
8 *
9 *
10 *
11 *
12 **
13
14
15
16
17
18
(q)uit or any other key + ENTER to continue:
e
Viewing generation #2:
111111111
123456789012345678
1
2
3
4
5
6 *
7 **
8 **
9 ***
10 **
11 **
12 *
13
14
15
16
17
18
(q)uit or any other key + ENTER to continue: q
sh-4.3$ q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.