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.

In each of these examples, we are focusing on what happens to the middle cell in these examples from one generation to the next.

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 threelive neighbors stays alive in the next generation (survival) The middle cell would survive to the next *The middle cell would survive because it *generation becau se it has two live has three live neighbors neighbors . In all other cases, the middle cell dies or remains dead (overcrowding or loneliness) The middle cell would die in the next The middle cell would die in the next generation because it has only one live neighbor (loneliness) generation because it has four live neighbors (overcrowding) The middle cell would remain dead in the next generation because it has only two ive neighbors (loneliness). *The middle cell would remain dead in the next generation because it has four live neighbors (overcrowding)

Program 1 Requirement:

You will be able to copy the skeleton of a program out of my directory to get started. This skeleton code will set up your main() method, a method that actually drives the simulation, a method that displays the matrix to the screen and a method that sets a default pattern in the matrix. Studying the code given to you will help you understand how the entire program fits together

// PUT YOUR HEADER DOCUMENTATION HERE!

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

// Change NetID to your NetID
public class NetID_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:\n\n", x++);
displayGrid(board);
genNextGrid(board);
System.out.print ("\n(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();
}
}
/*******************************************************************************/

// put the three methods you must write here and make sure to document
// them!

public static void clearGrid ( boolean[][] grid )
{
}

public static void genNextGrid ( boolean[][] grid )
{
}

public static int countNeighbors ( final boolean[][] grid, final int row, final int col )
{
}
}
  

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

? public static void clearGrid ( boolean[][] grid ) This method will simply reset the two-dimensional array that has been passed to it to all false values. This method shouldn't do any input or output.

? public static int countNeighbors ( final boolean[][] grid, final int row, final int col ) This method counts the number of neighbors in grid for an element at position row and column. For instance, if the method call was int test = countNeighbors ( thing, 4, 6 ); then you would be counting the neighbors for thing[4][6]. Remember that "neighbors" consist of the 8 cells immediately adjacent to the one being examined. Make sure to take into account if you are on an edge of the matrix (bottom or top rows, leftmost or rightmost columns), because you will need to make sure you don't try to count the neighbors that would appear off the matrix. The return value of this method should be a number between 0 and 8. There will be no input or output done in this method.

? public static void genNextGrid (boolean[][] grid) This method will actually generate the next generation of the simulation. It should use the two-dimensional array grid that is passed to it as the "current" generation. It should create a second, temporary two-dimensional array that will hold the "next" generation. You will need to create this temporary matrix to work with as you can't make changes to the current matrix because you would risk losing all of the information you need in order to successfully create the next generation. This method will need to call the countNeighbors() method you created to help decide if a cell will be alive or dead in the next generation. This method will need to implement the birth/survival/loneliness-overcrowding algorithm discussed earlier in the assignment. Once you have created the next generation of the simulation into your temporary two-dimensional array, you will need to copy that array back over the current generation (called grid, the method's parameter) so that the next generation can leave the method through the parameter. This method shouldn't do any input or output.

PROGRAM REQUIREMENT 3 : THE GRID As you can see if you run the example program, the program prints asterisks (*) to the screen. However, the grid itself, which is a boolean array, stores a value of false for dead and true for alive. Make sure you work with the grid array as an array of Boolean values and not an array of characters.

PROGRAM REQUIREMENT 4 : RUNNING THE PROGRAM When you run the program, you should input either r for a random pattern to start with, q to start with the Queen Bee Shuttle pattern or g to start with a Glider pattern. This is already handled for you with the code you have copied to start with; you do not need to do anything to implement this behavior. Then you will either enter a q to quit or any other character followed by ENTER to see the next generation.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Provided code as per your requirements.

  • "Screen shot program code" for better understanding.
  • "Code to copy" for the run the program execution.

Screen shot program code:

---------------------------------------------------------------------------------------

Code to copy:

//Header file section
import java.util.Scanner;
import java.util.Random;

// main class
public class NetID_Life
{

// the size of the grid (GRIDSIZE x GRIDSIZE)
final private static int GRIDSIZE = 18;

// main method
public static void main(String args[])
{
  boolean[][] board = new boolean[GRIDSIZE][GRIDSIZE];
  char choice;
  int x = 1;
  //Create a Scanner class's object
  Scanner sc = new Scanner(System.in);
        //Use do-while to read valid input
  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');
  //Call the methods and display the result
  //by use do-while loop here
  clearGrid(board);
  setup(board, choice);
    
  do
  {
   System.out.printf("Viewing generation #%d:\n\n", x++);
   displayGrid(board);
   genNextGrid(board);
   System.out.print("\n(q)uit or any other key + ENTER to continue: ");
   choice = sc.next().charAt(0);
  } while (choice != 'q');

}
//Method definition of setup
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;
    }
   }
  }

}
   //metho defintion of displayGrid
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 definition of clearGrid.
// This method reset the two-dimensional array
// that has been passed to it to all false values
// This method shouldn't do any input or output
public static void clearGrid(boolean[][] grid)
{
  for (int r = 0; r < grid.length; r++)
  {
   for (int c = 0; c < grid[r].length; c++)
   {
    grid[r][c] = false;

   }
  }
}

// Method definition of genNextGrid
// Generate the next generation of the simulation
public static void genNextGrid(boolean[][] grid)
{
  boolean[][] tmp = new boolean[GRIDSIZE][GRIDSIZE];
  for (int r = 0; r < 18; r++)
  {
   for (int c = 0; c < 18; c++)
   {
    tmp[r][c] = grid[r][c];
    int count = countNeighbors(tmp, r, c);
    switch (count)
    {
    case 0:
    case 1:
     tmp[r][c] = false;
     break;

    case 2:
     if (tmp[r][c])
     {
      tmp[r][c] = true;
     }
     if (!tmp[r][c])
     {
      tmp[r][c] = false;
     }
     break;
    case 3:
     tmp[r][c] = true;
     break;
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
     tmp[r][c] = false;
     break;
    }
   }
  }
  for (int r = 0; r < 18; r++)
  {
   for (int c = 0; c < 18; c++)
   {
    grid[r][c] = tmp[r][c];
   }
  }
}

// Method definition of countNeighbors
// This method counts the number of neighbors in grid
// for an element at position row and column.
public static int countNeighbors(final boolean[][] grid, final int row,
   final int col)
{
  int count = 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 (grid[row][col])
    {
     count++;
    }
   }
  }
  return count;
}
}

-----------------------------------------------------------------------------------------

Output:

Add a comment
Know the answer?
Add Answer to:
You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

  • C++ there is an issue with my if/else statements, I have tried several different ways to...

    C++ there is an issue with my if/else statements, I have tried several different ways to make it match the instructions but each time i get different errors. Need help geting this to match the instructions peoperly. *******************instructions***************************** Function: nextGeneration This function has no parameters. This function returns an int. The purpose of this function is to modify the grid to represent the next generation. Here's how you are supposed to do that for this assignment: Set each element of...

  • Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's...

    Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's Game of Life (for details and ex amples, see https://en.vikipedia.org/wiki/Convay's.Game of Life). The basic gam ts played on each generation, each cell changes according to the following instructions: nal grid of square cells, each of which is either alive or dead. With 1. Any dead cell with exactly three live neighbors (out of its eight nearest neighbors) comes to life [reproduction]. 2. Any live...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • I am really struggling with how to approach this program. Conway's Game of life, whereby, 1....

    I am really struggling with how to approach this program. Conway's Game of life, whereby, 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 2. Any live cell with two or three live neighbours lives on to the next generation. 3. Any live cell with more than three live neighbours dies, as if by over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Submit...

  • JAVA. Threading the Game of Life Write a Java program to implement the Game of Life,...

    JAVA. Threading the Game of Life Write a Java program to implement the Game of Life, as defined by John Conway: Any live cell with fewer than two live neighbors dies, as if caused by under-population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by...

  • Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques...

    Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths...

    JAVA QUESTION!!! please help! The following codes are a Maze program. There are TWO traversable paths possible in this maze. The program uses recursion. Can someone please explain to me why the program will pick one path if multiple traversable paths are available? Why does the program choose one path over a different path? (I believe it has something to do with the recursion terminating when a solution is found but I'm not sure.) Thank you!!!! The codes: public class...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT