Question

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 read or 2) by initializing some percentage of the grid cells.

• A sample life pattern file is included in the homework folder on eLearning. It is named GosperGliderGun.txt. Use this file to start, but you can download other files from the web and use them for your simulations as well. But set the default file name to open to “GosperGliderGun.txt”.

4. Using gridOne, calculate the conditions for all cells for the next generation of the game, and store the results in gridTwo. Flip back and forth from gridOne to gridTwo, using one grid to calculate the other. Print the next generation grid at each turn, just as we did in Wolfram’s one-dimensional cellular automata. In other words, if you’re examining gridOne, use gridTwo to store the next state of the world; and if you’re examining gridTwo, store the next state in gridOne. Flip back and forth this way, and print out the appropriate grid at each step. NOTE: Be sure to clear the screen before printing out a grid. In Windows, you can use system("CLS");.

5. Once the simulation begins, the user might want to pause the game to inspect the state of the cells. Add a pause feature just as we did in the Snake Game, using _kbhit() from conio.h. Pause when the user hits one key and resume when the user hits another.

6. But if the user hits the “s” key, stop the game and write the current grid to a file named “LifeGrid-Final.txt”. Use the same ‘.’ and ‘O’ notation as used in the input pattern file. You can then quit the game.

7. Make sure you are handling your edge cells properly, but don’t wrap the world around. Just let edge cells die a lonely death if they want to.

8. When printing a grid on the user’s screen, use ‘.’ or a space for dead cells. For live cells, use ‘O’ or ‘X’.

9. Once things are working, try using larger and larger grid sizes (or even smaller, if you want). Also, try finding other Life Patterns on the web and running them with your program. And be sure to use random initializations at some points and play around with different percentages of

initial live cells. Check out the Life Lexicon at

http://www.conwaylife.com/ref/lexicon/lex.htm for more patterns.

Game of Life Rules

1. Each cell has at most 8 neighbors (cells on the edge of the world will have fewer):

1 2 3

4 Cell 5

6 7 8

2. If a cell is alive but has fewer than 2 neighbors, the cell dies of loneliness.

3. If a cell is alive and has 4 or more neighbors, it dies from overcrowding.

4. If an empty cell has exactly 3 neighbors, it becomes alive.

5. All births and deaths happen simultaneously. Thus this is a discrete-time simulation system.

Input:

If the user asks you to randomly initialize the grid, ask the user for a percentage of cells to turn on (e.g., 20 or 70, meaning 20% or 70%, etc.). Then go through gridOne turning cells either on or off with that percentage. (Use a random number generator to determine which.)

On the other hand, if the user asks you to initialize the grid by reading from a file, ask the user for the name of the file. Check for the file’s existence by attempting to open it for reading. Assume the file is a plain text file that contains a Life grid pattern. The file will be in plain ASCII format. There can be both comment lines and pattern lines in the file.

Comment Lines: Comment lines will start with the ‘#’ symbol. Read these comment lines and output them to the user. Then skip right past them, as they are only informative comment lines.

Pattern Lines: Pattern lines will contain a series of ‘.’ and ‘O’ characters. A ‘.’ character indicates that the corresponding cell is dead while a ‘O’ character indicates that the cell is alive. Read each pattern line and set the cells in the corresponding row of gridA accordingly.

Example Life Pattern File: Here is a sample of what a Life Pattern file looks like when coded as a plain ASCII text file:

#Name: Gosper glider gun

#Author: Bill Gosper

#The first known gun and the first known finite pattern with unbounded growth.

#www.conwaylife.com/wiki/index.php?title=Gosper_glider_gun

#9x36 – size of this pattern

#

........................O

......................O.O

............OO......OO............OO

...........O...O....OO............OO

OO........O.....O...OO

OO........O...O.OO....O.O

..........O.....O.......O

...........O...O

............OO

Start loading the pattern into your gridOne at staring location 10,10 (i.e., don’t start loading the pattern near the edges of your grid.) Make the 10, 10 location variable, not hardwired.

Try this:

1. While the simulation is running, try to predict what’s going to happen. Is the automata going to completely die out and leave an empty world behind? Will it reach a stable state where there are still live cells but nothing is happening (there are many stable organisms). Will it reach a cyclic state where it repeats itself every n generations? Or perhaps it will continue to change but never really increase in size or complexity. Can you guess from looking at it?

Annotations for the code:

1. The main function can be at either the beginning or the end of the program. I don’t care which.

2. Add comments at the top of your program file to include your name, the name of the program, and any relevant notes on how your design works when executed. Add a change log in your comments that list the major changes you make to your logic – nothing too terribly detailed, but a list of breadcrumbs that will remind you and others of what you’ve done as your program becomes more sophisticated and/or nearly complete.

3. Point out (in the comments at the top of your program) any special features or techniques you added using a comment saying something like “// Special Features.”

4. Comment your code effectively, as we discussed in class. Use descriptive variable names everywhere so that the code becomes as self-documenting as possible. Use additional commentary only to improve readability and comprehensibility by other people.

5. You absolutely MUST use consistent indentation and coding styles throughout the program. Failure to do so will result in a loss of three points.

6. Don’t make your code unnecessarily hard to read by forcing all variables to be local variables. You can use global variables when appropriate.

7. If the program does not work at all, or works incorrectly, at least 10 points will be deducted.

8. No late submissions will be accepted. Please meet the deadline.

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

public class GameOfLife

{

    public static void main(String[] args)

    {

        int M = 10, N = 10;

  

        // Intiliazing the grid.

        int[][] grid = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },

            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }

        };

  

        // Displaying the grid

        System.out.println("Original Generation");

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

        {

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

            {

                if (grid[i][j] == 0)

                    System.out.print(".");

                else

                    System.out.print("*");

            }

            System.out.println();

        }

        System.out.println();

        nextGeneration(grid, M, N);

    }

  

    // Function to print next generation

    static void nextGeneration(int grid[][], int M, int N)

    {

        int[][] future = new int[M][N];

  

        // Loop through every cell

        for (int l = 1; l < M - 1; l++)

        {

            for (int m = 1; m < N - 1; m++)

            {

                // finding no Of Neighbours that are alive

                int aliveNeighbours = 0;

                for (int i = -1; i <= 1; i++)

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

                        aliveNeighbours += grid[l + i][m + j];

  

                // The cell needs to be subtracted from

                // its neighbours as it was counted before

                aliveNeighbours -= grid[l][m];

  

                // Implementing the Rules of Life

  

                // Cell is lonely and dies

                if ((grid[l][m] == 1) && (aliveNeighbours < 2))

                    future[l][m] = 0;

  

                // Cell dies due to over population

                else if ((grid[l][m] == 1) && (aliveNeighbours > 3))

                    future[l][m] = 0;

  

                // A new cell is born

                else if ((grid[l][m] == 0) && (aliveNeighbours == 3))

                    future[l][m] = 1;

  

                // Remains the same

                else

                    future[l][m] = grid[l][m];

            }

        }

  

        System.out.println("Next Generation");

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

        {

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

            {

                if (future[i][j] == 0)

                    System.out.print(".");

                else

                    System.out.print("*");

            }

            System.out.println();

        }

    }

}

Add a comment
Know the answer?
Add Answer to:
Objective: Write a program that implements the Game of Life cellular automata system invented by John...
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
  • 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...

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

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

  • 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 a program that consists of a 10 by 10 game board, with 10 generations. Project...

    Write a program that consists of a 10 by 10 game board, with 10 generations. Project The Game of Life The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell. Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has...

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

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? I need c++ codes. Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by randomly setting 20 of...

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

  • Please use C++, thank you! The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the...

    Please use C++, thank you! The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has more than 3 neighbors- dies of crowding. Its cell will be empty in the next...

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