Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are going to be implementing the classic computer science simulation, Conway

ID: 3569634 • Letter: Y

Question

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.

PROGRAM REQUIREMENT 2 : YOU'RE GOING TO ADD THREE METHODS
Here is a list of the three methods that you must write, as described.

Explanation / Answer

import java.util.Scanner;
import java.util.Random;


public class id_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();
}
}

public static void clearGrid ( boolean[][] board )
{
  
for (int row=0; row<board.length; row++)
{
for(int col=0; col<board[row].length;col++)
{
board[row][col]=false;
  
}
}
}

public static void genNextGrid ( boolean[][] board )
{
boolean[][] temp=new boolean[GRIDSIZE][GRIDSIZE];
for(int row=0;row<18;row++)
{
for(int col=0;col<18;col++)
{
temp[row][col]=board[row][col];
int count=countNeighbors(temp,row,col);
switch(count)
{
case 0: case 1:
temp[row][col]=false;
break;

case 2:
if(temp[row][col])
{
temp[row][col]=true;
}
if(!temp[row][col])
{
temp[row][col]=false;
}
break;
case 3:
temp[row][col]=true;
break;
case 4:case 5: case 6: case 7: case 8:
temp[row][col]=false;
break;
}
}
}
for(int row=0;row<18;row++)
{
for(int col=0;col<18;col++)
{
board[row][col]=temp[row][col];
}
}
}

public static int countNeighbors ( final boolean[][] board, final int row, final int col )
{
int ncount=0;
for(int r=row-1; r<= row+1; r++)
{
if(row<0||row>=GRIDSIZE)
{
continue;
}
for(int c=col-1; c<=col+1;c++)
{
if(col<0||col>=GRIDSIZE||(r==row && c==col))
{
continue;
}
if(board[row][col])
{
ncount++;
}
}
}
return ncount;
}
}

public static void genNextGrid ( boolean[][] board )
{
boolean[][] temp=new boolean[GRIDSIZE][GRIDSIZE];
for(int row=0;row<18;row++)
{
for(int col=0;col<18;col++)
{
temp[row][col]=board[row][col];
int count=countNeighbors(temp,row,col);
switch(count)
{
case 0: case 1:
temp[row][col]=false;
break;

case 2:
if(temp[row][col])
{
temp[row][col]=true;
}
if(!temp[row][col])
{
temp[row][col]=false;
}
break;
case 3:
temp[row][col]=true;
break;
case 4:case 5: case 6: case 7: case 8:
temp[row][col]=false;
break;
}
}
}
for(int row=0;row<18;row++)
{
for(int col=0;col<18;col++)
{
board[row][col]=temp[row][col];
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote