Question

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 randomly setting 20 of the squares, or asking the user for sets of coordinates. The game is now ready to begin, and this involves advancing through time one step at a time. A cell's fate depends on the state of its 8 closest neighbors. • If a cell is alive, and 2 or 3 of its neighbors are also alive, the cell remains alive. • If a cell is alive and it has more than 3 alive neighbors, it dies of overcrowding. • If a cell is alive and it has fewer than 2 alive neighbors, it dies of loneliness. • If a cell is dead and it has exactly 3 neighbors it becomes alive again. Those 4 seemingly simple rules can result in wildly differing sequences. Sometimes an initial state will create an unpredictable, chaotic sequence. Other times, it will create a repeating sequence (such as the glider, pulsar, and spaceship). And other times, all cells will quickly die off or stabilize into a static formation, known as a still life, such as a 2x2 square. Your program should allow printing every generation or every 5th generation.

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

#include <iostream>
#include <fstream>

using namespace std;

// to print the grid
void print(int cells[12][12], std::ostream &myfile) {
myfile << "\t0 1 2 3 4 5 6 7 8 9 10 11" << endl;
  
   for (int i = 0; i < 12; i++) {
   myfile << i << "\t";
       for (int j = 0; j < 12; j++) {
           myfile << cells[i][j] << " ";
       }
   myfile << endl;
   }
}

// check if i, j is valid indices for grid
bool isValid(int i, int j) {
   if (i >= 0 && i < 12 && j >= 0 && j < 12)
       return true;
   else
       return false;
}

// find no of live neighbors
int findActiveNeighbours(int cells[12][12], int i, int j) {
   int activeNeighbours = 0;

   if (isValid(i - 1, j - 1) && cells[i - 1][j - 1] == 1)
       activeNeighbours++;
   if (isValid(i - 1, j) && cells[i - 1][j] == 1)
       activeNeighbours++;
   if (isValid(i - 1, j + 1) && cells[i - 1][j + 1] == 1)
       activeNeighbours++;

   if (isValid(i, j - 1) && cells[i][j - 1] == 1)
       activeNeighbours++;
   if (isValid(i, j + 1) && cells[i][j + 1] == 1)
       activeNeighbours++;

   if (isValid(i + 1, j - 1) && cells[i + 1][j - 1] == 1)
       activeNeighbours++;
   if (isValid(i + 1, j) && cells[i + 1][j] == 1)
       activeNeighbours++;
   if (isValid(i + 1, j + 1) && cells[i + 1][j + 1] == 1)
       activeNeighbours++;

   return activeNeighbours;
}

// this method simulates a round
// it changes the original array
void simulate(int cells[12][12]) {
   int newCells[12][12];
  
   // not processing border cells
   for (int i = 1; i < 11; i++) {
       for (int j = 1; j < 11; j++) {
          
           // find no of live neighbors
           int n = findActiveNeighbours(cells, i, j);
          
           if (cells[i][j] == 1) { // if cell is live
               if (n == 0 || n == 1) {
                   newCells[i][j] = 0; // mark for deletion
               } else if (n >= 4) {
                   newCells[i][j] = 0; // mark for deletion
               } else {
               newCells[i][j] = cells[i][j];
               }
           } else { // if cell is dead
               if (n == 3) {
                   newCells[i][j] = 1; // mark for generation
               } else {
               newCells[i][j] = cells[i][j];
               }
           }
       }
   }
  
   // restoring values into original array
   for (int i = 1; i < 11; i++) {
       for (int j = 1; j < 11; j++) {
       cells[i][j] = newCells[i][j];
       }
   }
}

// driver function
int main()
{
ofstream myfile;
myfile.open ("output.txt");
  
int life[12][12] =
{{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,1,1,1,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,1,0,1,0,1,0,0,0,0},
{0,0,0,0,1,0,1,0,1,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}};
  
   myfile << "\t\t\t Game of Life\n\t\t\t--------------" << endl;
  
   int round = 1;
   myfile << "\t\tOriginal configuration" << endl;
   print(life, myfile);
   while(round <= 4) {
   myfile << "\n\t\t\t\tRound" << round << endl;
  
   simulate(life);
  
   print(life, myfile);
  
   round++;
   }
}


Sample Output:

           Game of Life
           --------------
       Original configuration
   0 1 2 3 4 5 6 7 8 9 10 11
0   0 0 0 0 0 0 0 0 0 0 0 0
1   0 0 0 0 1 0 1 0 1 0 0 0
2   0 0 0 1 0 1 0 1 0 0 0 0
3   0 0 0 0 0 0 0 0 0 0 0 0
4   0 0 0 0 1 0 1 0 1 0 0 0
5   0 0 0 1 0 1 0 1 0 0 0 0
6   0 1 1 1 1 1 1 1 1 1 1 0
7   0 0 0 0 1 1 1 1 0 0 0 0
8   0 0 0 0 1 0 1 0 1 0 0 0
9   0 0 0 1 0 1 0 1 0 0 0 0
10   0 0 0 0 1 0 1 0 1 0 0 0
11   0 0 0 0 0 0 0 0 0 0 0 0

               Round1
   0 1 2 3 4 5 6 7 8 9 10 11
0   0 0 0 0 0 0 0 0 0 0 0 0
1   0 0 0 0 1 1 1 1 0 0 0 0
2   0 0 0 0 1 1 1 1 0 0 0 0
3   0 0 0 0 1 1 1 1 0 0 0 0
4   0 0 0 0 1 1 1 1 0 0 0 0
5   0 0 0 0 0 0 0 0 0 0 0 0
6   0 0 1 0 0 0 0 0 0 1 0 0
7   0 0 1 0 0 0 0 0 0 0 0 0
8   0 0 0 1 0 0 0 0 1 0 0 0
9   0 0 0 1 0 0 0 0 1 0 0 0
10   0 0 0 0 1 1 1 1 0 0 0 0
11   0 0 0 0 0 0 0 0 0 0 0 0

               Round2
   0 1 2 3 4 5 6 7 8 9 10 11
0   0 0 0 0 0 0 0 0 0 0 0 0
1   0 0 0 0 1 0 0 1 0 0 0 0
2   0 0 0 1 0 0 0 0 1 0 0 0
3   0 0 0 1 0 0 0 0 1 0 0 0
4   0 0 0 0 1 0 0 1 0 0 0 0
5   0 0 0 0 0 1 1 0 0 0 0 0
6   0 0 0 0 0 0 0 0 0 0 0 0
7   0 0 1 1 0 0 0 0 0 0 0 0
8   0 0 1 1 0 0 0 0 0 0 0 0
9   0 0 0 1 0 1 1 0 1 0 0 0
10   0 0 0 0 1 1 1 1 0 0 0 0
11   0 0 0 0 0 0 0 0 0 0 0 0

               Round3
   0 1 2 3 4 5 6 7 8 9 10 11
0   0 0 0 0 0 0 0 0 0 0 0 0
1   0 0 0 0 0 0 0 0 0 0 0 0
2   0 0 0 1 1 0 0 1 1 0 0 0
3   0 0 0 1 1 0 0 1 1 0 0 0
4   0 0 0 0 1 1 1 1 0 0 0 0
5   0 0 0 0 0 1 1 0 0 0 0 0
6   0 0 0 0 0 0 0 0 0 0 0 0
7   0 0 1 1 0 0 0 0 0 0 0 0
8   0 0 0 0 0 0 0 0 0 0 0 0
9   0 0 1 1 0 0 0 0 0 0 0 0
10   0 0 0 0 1 0 0 1 0 0 0 0
11   0 0 0 0 0 0 0 0 0 0 0 0

               Round4
   0 1 2 3 4 5 6 7 8 9 10 11
0   0 0 0 0 0 0 0 0 0 0 0 0
1   0 0 0 0 0 0 0 0 0 0 0 0
2   0 0 0 1 1 0 0 1 1 0 0 0
3   0 0 0 0 0 0 0 0 0 0 0 0
4   0 0 0 1 0 0 0 0 1 0 0 0
5   0 0 0 0 1 0 0 1 0 0 0 0
6   0 0 0 0 0 0 0 0 0 0 0 0
7   0 0 0 0 0 0 0 0 0 0 0 0
8   0 0 0 0 0 0 0 0 0 0 0 0
9   0 0 0 1 0 0 0 0 0 0 0 0
10   0 0 0 1 0 0 0 0 0 0 0 0
11   0 0 0 0 0 0 0 0 0 0 0 0


Hi. Please find the answer above.. In case of any doubts, you may ask in comments. You may upvote the answer if you feel i did a good work!

Add a comment
Know the answer?
Add Answer to:
I have a question about my assignment. Could you please make program and give me some...
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
  • 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...

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

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

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

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

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

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

  • Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT...

    Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT DESCRIBED BELOW). Title: Game of Life Description In this assignment, you will code a simulation called "Game of Life". It is a very famous 'game' and the formed the basis of an entire field of simulation models called "cellular automata". Before continuing reading this assignment, I suggest you read a bit more about Game of Life at: https://en.wikipedia.org/wiki/Conway's_Game_of_Life or http://www.math.com/students/wonders/life/life.html (the latter also has...

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