Question

You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton...

You will write a Java program that implements Conway’s Game of Life, a simple cellular

automaton discussed in class. See for example:

http://www.bitstorm.org/gameoflife/

Our version has a 10 x 10 grid, numbered like this:

0 1 2 3 4 5 6 7 8 9

0

1

2

3

4

5

6

7

8

9

The grid is represented by a 10 x 10 2­dimensional integer array. If the grid point (i, j) is

"populated", the array element [i][j] contains 1; otherwise it contains 0. Elements along

the edges, i == 0 or 9, or j == 0 or 9, are always unpopulated.

When we display the grid, a populated cell is indicated by a ‘#’; an unpopulated cell is

indicated by a space.

What your program should do:

• Prompt the user to enter a list of (i,j) pairs, both non­negative integers

(stop when a negative integer is read for either i or j)

• Prompt the user to enter the number of time steps

• Initialize the grid based on the (i,j) pairs entered by the user (set the

corresponding array elements to 1)

• Display the initial state of the grid (call the displayGrid() method)

• For each time step,

* update the grid according to Conway’s rules (call the updateGrid()

method)

* display the grid (call the displayGrid() method)

We follow Conway’s standard rules for updating the cells, as follows. A "neighbor" is a

vertically, horizontally, or diagonally adjacent cell that is populated. A cell has anywhere

from 0 to 8 neighbors.

• For a cell that is "populated", if the cell has <= 1 neighbors, or >= 4 neighbors, it

dies (becomes 0). Otherwise, it survives (remains 1).

• For a cell that is not populated, if the cell has exactly 3 neighbors, it becomes

populated (becomes 1).

• Cells on the edge always remain unpopulated (0).

The displayGrid() method has prototype:

void displayGrid(int mat[][]);

It displays the borders of the grid (see sample runs below), and prints the 10 x 10 grid of

cells. Populated cells are indicated with a ‘#’ sign, unpopulated cells with a space.

The updateGrid() method has prototype:

void updateGrid(int mat[][]);

mat is the 2­dimensional array that contains the current state of the grid. The method

counts the neighbors in each cell of the grid, and updates the cells in the grid according to

Conway’s rules.

You can obviously enter arbitrary initial conditions to test your program. Some fun

examples adapted from the bitstorm.org site:

Glider: initial populated cells at (3, 4) (4, 2) (4, 4) (5, 3) (5, 4)

5 cell row: initial populated cells at (6,4) (6,5) (6,6) (6,7) (6,8)

Small explosion: initial populated cells at (4, 5) (4,6) (5,4) (5,5) (5, 7) (6, 5) (6,6)

Note that these will not behave exactly the same as the bitstorm.org display when the

populated cells run into the boundary conditions.

Sample runs:

unixlab% java Life

Please enter list of (i,j) pairs for populated cells

(negative i or j to quit): 6 4 6 5 6 6 6 7 6 8 -1 -1

Enter number of time steps: 5

Initial grid:

0123456789

0

1

2

3

4

5

##### 6

7

8

9

Time step 1

0123456789

0

1

2

3

4

### 5

### 6

### 7

8

9

Time step 2

0123456789

0

1

2

3

# 4

# # 5

# # 6

# # 7

# 8

9

Time step 3

0123456789

0

1

2

3

# 4

### 5

## ## 6

### 7

# 8

9

Time step 4

0123456789

0

1

2

3

### 4

# # 5

# # 6

# # 7

### 8

9

Time step 5

0123456789

0

1

2

# 3

### 4

# # # 5

### ## 6

# # # 7

### 8

9

unixlab% java Life

Please enter list of (i,j) pairs for populated cells

(negative i or j to quit): 4 5

4 6

5 4

5 5

5 7

6 5

6 6

-1 -1

Enter number of time steps: 5

Initial grid:

0123456789

0

1

2

3

## 4

## # 5

## 6

7

8

9

Time step 1

0123456789

0

1

2

3

### 4

# # 5

### 6

7

8

9

Time step 2

0123456789

0

1

2

# 3

### 4

# # 5

### 6

# 7

8

9

Time step 3

0123456789

0

1

2

### 3

### 4

# # 5

### 6

### 7

8

9

Time step 4

0123456789

0

1

# 2

# # 3

# # 4

# # 5

# # 6

# # 7

# 8

9

Time step 5

0123456789

0

1

# 2

### 3

## ## 4

### ### 5

## ## 6

### 7

# 8

9

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

Program:

Import statements import java.util.Scanner; /ConwaysGameofLife class public class ConwaysGameofLife //Main function. public s

for (int c= 1; c < cols-1; c++) //detects for 1s, when find 1, calc number of neighbors if (mat [r] [c] -1) num1 for = 0 ; (i

Sample output:

Please enter list of (i,j) pairs for populated cells (negative i or j to quit): 6 4 6 5 6 6 6 7 6 8 -1 1 Enter number of timeTime step 2 0123456789 1 2 3 4 ##6 ## 7 8 9

Code to copy:

//Import statements.

import java.util.Scanner;

//ConwaysGameofLife class.

public class ConwaysGameofLife

{

    //Main function.

     public static void main(String[] args)

    {

        //Variable declaration.

          final int rows = 10, cols =10;

        int[][] tab = new int[rows][cols];

        //Create an object to the Scanner.

        Scanner sc = new Scanner(System.in);

        int tempI, tempJ, steps;

        //Prompt the user to enter the list of pairs.

        System.out.println("Please enter list of (i,j) pairs for populated cells (negative i or j to quit): ");

        while (true)

        {

            tempI = sc.nextInt();

            tempJ = sc.nextInt();

            if (tempI >= 0 || tempJ >= 0){

                tab[tempI][tempJ] = 1;

            } else {

                break;

            }

        }

        //Prompt the user to enter the time steps.

       System.out.println("Enter number of time steps: ");

        steps = sc.nextInt();

        //display, update with pause and display step #

        for (int i = 0; i <= steps; i++)

        {

            if (i == 0)

            {

                System.out.println("Initial grid:");

            }

            else

            {

                System.out.println("Time step " + i);

            }

            displayGrid(tab, rows, cols);

            updateGrid(tab, rows, cols);

            try

            {

                Thread.sleep(1000);

            }

            catch (Exception e)

            {

                System.out.println("Error dumb ass lmao");

            }

        }

    }   

    //display # with cells with 1s, otherwise blank space   

    public static void displayGrid(int mat[][], int rows, int cols)

    {

        for (int i = 0; i < rows; i++)

        {

            if (i < 10)

            {

                System.out.print(i);

            } else {

                System.out.print(i%10);

            }

        }

        System.out.print("\n");

        for (int j = 0; j < rows; j++)

        {

            for (int k = 0; k < cols; k++)

            {

                if (mat[j][k] == 1){

                    System.out.print("#");

                } else {

                    System.out.print(" ");

                }

            }

            System.out.print(" " + j + "\n");

        }

    }

    //update cells method

    public static void updateGrid(int mat[][], int rows, int cols)

    {

        int numN = 0;

        int temp[][] = new int[rows][cols];

        //sync temp to mat

        for (int a = 0; a < rows; a ++)

        {

            for (int b = 0; b < cols; b++)

            {

                temp[a][b] = mat[a][b];

            }

        }

        for (int r = 1; r < rows-1; r++)

        {

            for (int c = 1; c < cols-1; c++)

            {

                //detects for 1s, when find 1, calc number of neighbors

                if (mat[r][c] == 1)

                {

                    numN = 0;

                    for (int j = r-1; j <= r+1; j++)

                    {

                        for (int k = c - 1; k <= c + 1; k++)

                        {

                            if (mat[j][k] == 1)

                            {

                                numN += 1;

                            }

                        }

                    }

                    if (numN-1 <= 1 || numN-1 >= 4)

                    {

                        temp[r][c] = 0;

                    }

                } else {

                    //otherwise find if there are 3 neighbors nearby

                    numN = 0;

                    for (int j = r-1; j <= r+1; j++)

                    {

                        for (int k = c - 1; k <= c + 1; k++)

                        {

                            if (mat[j][k] == 1)

                            {

                                numN += 1;

                            }

                        }

                    }

                    if (numN == 3)

                    {

                        temp[r][c] = 1;

                    }

                }

            }

        }

        //sync mat to temp

        for (int a = 0; a < rows; a ++)

        {

            for (int b = 0; b < cols; b++)

            {

                mat[a][b] = temp[a][b];

            }

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
You will write a Java program that implements Conway’s Game of Life, a simple cellular automaton...
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
  • Objective: Write a program that implements the Game of Life cellular automata system invented by John...

    Objective: Write a program that implements the Game of Life cellular automata system invented by John Conway. 1. Create two game grids of size at least 50x50. These grid cells can be either Boolean or integer. In the following, I’ll refer to these as gridOne and gridTwo. 2. Set all cells in both grids to false. 3. Start by initializing gridOne. Allow the user to specify two different ways of initializing the grid: 1) by specifying a pattern file to...

  • 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...

  • 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...

  • Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin...

    Please use Java for this question. ​​​​​​​ Input Format Format for Custom Testing Input from stdin will be processed as follows and passed to the function In the first line, there is a single integer n. In the second line, there is a single integer m. In the ph of the next n lines there are m space-separated integers denoting the throw of the initial grid. In the next line, there is a single integer k. In the next line,...

  • Task: Simulate the growth of a biological population. The Game of Life, invented by John H....

    Task: Simulate the growth of a biological population. The Game of Life, invented by John H. Conway, is supposed to model the genetic laws for birth, survival, and death. Our implementation of the game will use a 2-dimensional array with 12 rows and 12 columns. Each cell of the array will hold either a 1 (an organism is present) or a 0 (no organism there). You will calculate 4 generations of the organisms’ birth, survival, and death. An organism’s neighbors...

  • Java language: C Write a game program that allows the user to collect 4 tokens hidden...

    Java language: C Write a game program that allows the user to collect 4 tokens hidden in an array (cells) of size 10. The program asks the user to select a cell. Determine if there is a token in that cell or not, and give the user appropriate feedback. This program should keep letting user to select until the all tokens are collected. The program should display the total number of selections. Use rand_position Example input/output: There are four tokens....

  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    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...

  • 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...

  • C PROGRAMMING You must write a C program to check the validity of a Sudoku solution. Follow the link to know more about...

    C PROGRAMMING You must write a C program to check the validity of a Sudoku solution. Follow the link to know more about Sudoku: https://www.bigfishgames.com/blog/how-to-solve-sudoku-puzzles-quickly-andreliably/ You must at least do the following: 1- Ask the user to provide a minimum of first two rows of the Sudoku grid. For the rest of the entries, you should use random number generator. 2- Use appropriate logic to make sure random number generator generates distinct set of valid integers! 3- It should be...

  • 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