Question

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 oneInitial state: XIX XIX Number of generations- 10.

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 generion 2 A creature that has less than 2 neighbors dies of loneliness 3. If an empty cell that has exactly 3 neighbors, a new creature is born in that cell. Write a program that consists of a 10 x 10 cell grid board. The program will prompt for the initial state of the world and the number of generations to be played. After each generation the program will update the state of the board and will display the updated board. The program can stop ifit has reached the number of required generations or if no further changes occur (i.e., no more creatures alive or it has reached a steady or repeating pattern) Note that the changes in the board in each generation are done simultaneously on all the cells, so you may need to define and use two boards for computing the changes The Input 1. An integer indicating the number of stages that should be played. 2. The initial state of the board the locations of the creatures on the board. The Output The state of the board in each generation, starting with the initial state: Pfor an empty cell and 'X' for a creature. Run the program with the following inputs:
Initial state: XIX XIX Number of generations- 10.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to implement Life game

#include <iostream>

using namespace std;

// function declaration

void initializeBoard(int board[][10], int n);

int evolve(int board[][10] ,int n, int row, int col);

void printGeneration(int board[][10],int n);

void copy_board(int board[][10], int new_board[10][10],int n);

int main() {

               int n = 10, row, col, changes, num_alive;

               int num_gen;

               int board[10][10], copy[10][10]; // declare a 10X10 array of integers

               // initialize the board with all 0

               initializeBoard(board,n);

               // loop to input the coordinates for live cells

               do

               {

                              cout<<"Enter row and column for a live cell(separated by space) (-1 and -1 to end) : ";

                              cin>>row>>col;

                              if(row >= 0 && row < n && col >= 0 && col < n)

                                             board[row][col] = 1;

               }while(row != -1 && col != -1); //enter -1 and -1 to break from loop

               // input of number of generations

               cout<<"Enter the number of generations : ";

               cin>>num_gen;

               cout<<"Initial Board : "<<endl;

               printGeneration(board,n);

               // loop to generate the generations

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

               {

                              changes = 0, num_alive = 0;

                              for(int row=0;row<n;row++)

                              {

                                             for(int col=0;col<n;col++)

                                             {

                                                            int neighbors = evolve(board,n,row,col); // get the number of alive neighbors

                                                            // update the board

                                                            if(board[row][col] == 1)

                                                            {

                                                                           if(neighbors < 2 || neighbors > 3)

                                                                                          copy[row][col] = 0;

                                                                           else

                                                                                          copy[row][col] = 1;

                                                            }else{

                                                                           if(neighbors == 3)

                                                                                          copy[row][col]=1;

                                                                           else

                                                                                          copy[row][col]=0;

                                                            }

                                                            // if any changes made update changes

                                                            if(board[row][col] != copy[row][col])

                                                                           changes++;

                                                            // if any live cells update num_alive

                                                            if(copy[row][col] == 1)

                                                                           num_alive++;

                                             }

                              }

                              copy_board(copy,board,n); // copy the new generation into old

                              cout<<"\nGeneration : "<<(i+1)<<endl;

                              printGeneration(board,n);

                              if(changes == 0 || num_alive == 0) // if no changes or no live cells present

                                             break;

               }

               return 0;

}

// function to initialize the board

void initializeBoard(int board[][10], int n)

{

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

               {

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

                                             board[i][j] = 0;

               }

}

// function to return the number of live neighbors for a given cell

int evolve(int grid[][10] ,int n, int row, int col)

{

               if(row == 0)

               {

                              if(col == 0)

                                             return(grid[row][col+1]+grid[row+1][col]+grid[row+1][col+1]);

                              else if(col == n-1)

                                             return(grid[row][col-1]+grid[row+1][col-1]+grid[row+1][col]);

                              else

                                             return(grid[row][col-1]+grid[row+1][col-1]+grid[row+1][col]+grid[row+1][col+1]+grid[row][col+1]);

               }else if(row == n-1)

               {

                              if(col == 0)

                                             return(grid[row][col+1]+grid[row-1][col]+grid[row-1][col+1]);

                              else if(col == n-1)

                                             return(grid[row][col-1]+grid[row-1][col-1]+grid[row-1][col]);

                              else

                                             return(grid[row][col-1]+grid[row-1][col-1]+grid[row-1][col]+grid[row-1][col+1]+grid[row][col+1]);

               }else if(col == 0)

                              return(grid[row-1][col]+grid[row-1][col+1]+grid[row][col+1]+grid[row+1][col+1]+grid[row+1][col]);

               else if(col == n-1)

                              return(grid[row-1][col]+grid[row-1][col-1]+grid[row][col-1]+grid[row+1][col-1]+grid[row+1][col]);

               else

                              return(grid[row-1][col-1] + grid[row-1][col] + grid[row-1][col+1]+grid[row][col-1]+

                                                            grid[row][col+1]+grid[row+1][col-1]+grid[row+1][col]+grid[row+1][col+1]);

}

// function to print a given board

void printGeneration(int board[][10],int n)

{

               for(int i=0;i<n;i++){

                              cout<<endl;

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

                              {

                                             if(board[i][j] == 1)

                                                            cout<<"X ";

                                             else

                                                            cout<<"- ";

                              }

               }

}

// function to copy values from board to new board

void copy_board(int board[][10], int new_board[][10],int n)

{

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

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

                                             new_board[i][j] = board[i][j];

}

//end of program

Output:

Enter row and column for a live cell(separated by space) (-1 and -1 to end)1 1 Enter row and column for a live cell(separatedGeneration: 1 Generation 2 Generation: 3

Generation4

Add a comment
Know the answer?
Add Answer to:
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...
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
  • 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...

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

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

  • 1. The Pentagonal Game of Life is a variant of the Game of Life played on a grid in which each cell has 5 neighbours. L...

    1. The Pentagonal Game of Life is a variant of the Game of Life played on a grid in which each cell has 5 neighbours. Live cells are shown in grey and dead cells are shown in white. The rules are as follows Two cells are considered to be neighbours if they share an edge. A dead cell comes to life it if has exactly 2 living neighbours. .A live cell remains alive if it has 2 or 3 living...

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

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

  • First, you will need to create the Creature class. Creatures have 2 member variables: a name,...

    First, you will need to create the Creature class. Creatures have 2 member variables: a name, and the number of gold coins that they are carrying. Write a constructor, getter functions for each member, and 2 other functions: - void addGoldCoins(int) adds gold coins to the Creature. - void identify() prints the Creature information. The following should run in main: Creature d{"dragon", 50}; d.addGoldCoins(10); d.identify(); // This prints: The dragon is carrying 60 gold coins. Second, you are going to...

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

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