Question

This is for C++ The mathematician John Horton Conway invented the “Game of Life.” Though not...

This is for C++

The mathematician John Horton Conway invented the “Game of Life.” Though not a “game” in any traditional sense, it provides interesting behavior that is specified with only a few rules. This project asks you to write a program that allows you to specify an initial configuration. The program follows the rules of Life (listed shortly) to show the continuing behavior of the configuration.

LIFE is an organism that lives in a discrete, two-dimensional world. While this world is actually unlimited, we do not have that luxury, so we restrict the array to 80 characters wide by 22 character positions high. If you have access to a larger screen, by all means use it.

This world is an array with each cell capable of holding one LIFE cell. Generations mark the passing of time. Each generation brings births and deaths to the LIFE community. The births and deaths follow this set of rules:

1. We define each cell to have eight neighbor cells. The neighbors of a cell are the cells directly above, below, to the right, to the left, diagonally above to the right and left, and diagonally below, to the right and left.

2. If an occupied cell has zero or one neighbor, it dies of loneliness. If an occupied cell has more than three neighbors, it dies of overcrowding.

3. If an empty cell has exactly three occupied neighbor cells, there is a birth of a new cell to replace the empty cell.

4. Births and deaths are instantaneous and occur at the changes of generation. A cell dying for whatever reason may help cause birth, but a newborn cell cannot resurrect a cell that is dying, nor will a cell’s death prevent the death of another, say, by reducing the local population.

*

Examples: *** becomes * then becomes *** again, and so on.

*

Notes: Some configurations grow from relatively small starting configurations. Others move across the region. It is recommended that for text output you use a rectangular char array with 80 columns and 22 rows to store the LIFE world’s successive generations. Use an * to indicate a living cell and use a blank to indicate an empty (or dead) cell. If you have a screen with more rows than that, by all means make use of the whole screen.

Suggestions: Look for stable configurations. That is, look for communities that repeat patterns continually. The number of configurations in the repetition is called the period. There are configurations that are fixed, that is, that continue without change. A possible project is to find such configurations.

Hints: Define a void function named generation that takes the array we call world , an 80-column by 22-row array of type char , which contains the initial configuration. The function scans the array and modifies the cells, marking the cells with births and deaths in accord with the rules listed previously. This involves examining each cell in turn and either killing the cell, letting it live, or, if the cell is empty, deciding whether a cell should be born. There should be a function display that accepts the array world and displays the array on the screen. Some sort of time delay is appropriate between calls to generation and display . To do this, your program should generate and display the next generation when you press Return. You are at liberty to automate this, but automation is not necessary for the program.

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

The code would be:

#include<iostream>
#include<stdio.h>

using namespace std;
void Display(const int gen[][80],int genNum);
void Generation(int gen[][80]);
int aLive(int gen[][80]);
void dieBorn(int geb[][80],int succ[][80],int neigbr,int i,int j);
void copy(int a[][80],int b[][80]);
int check(int gen[][80],int i,int j);


int main()
{
   char c;
   int world[22][80] = {0};
   int row,col,gen = 0;
   char backspace = 0;
   bool cntr = true;
   while(cntr==true)
   {
       do
       {
           cout<<"Enter row index of cell(0-21):";
           cin>>row;
       }while(row<0 || row>21);
       do
       {
           cout<<"Enter column index of cell(0-79):";
           cin>>col;
       }while(col<0 || col>79);
       world[row][col] = 1;
       cout<<"Cell [ "<<row<<"]["<<col<<"] is alive!\n";
       cout<<"\nTo mark another cell?press(y/n):";
       do
       {
           cout<<backspace;
           cin>>c;
       }while(c!='y'&&c!='Y'&&c!='n'&&c!='N');
       if(c=='n'||c=='N')
       {
           cntr = false;
       }
   }
   Display(world,gen);
   while(aLive(world))
   {
       Generation(world);
       gen++;
       int x,y;
       for(x=0;x<2000;x++)
       {
           for(y=0;y<200000;y++)
           {
           }
       }
       if(aLive(world)==0)
       {
           cout<<"\n\tAll LIFE has died out.\n";
       }
       else
       {
           Display(world,gen);
       }
   }
   cout<<"\n\nPress any key to exit:";
   cin.get();
   cin.get();
   return 0;
}

int aLive(int gen[][80])
{
   for(int i=0;i<22;i++)
   {
       for(int j=0;j<80;j++)
       {
           if(gen[i][j] == 1)
           {
               return 1;
           }
       }
   }
   return 0;
}


void Display(const int gen[][80],int genNum)
{
   cout<<"\n\nGeneration " << genNum <<":\n\n";
   for(int i=0;i<22;i++)
   {
       for(int j=0;j<80;j++)
       {
           cout<<gen[i][j];
       }
       cout<<endl;
   }
   return;
}
void Generation(int world[][80])
{
   int neigbr = 0;
   int i,j;
   int succ[22][80];
   for(i=0;i<22;i++)
   {
       for(j=0;j<80;j++)
       {
           neigbr = check(world,i,j);
           dieBorn(world,succ,neigbr,i,j);
       }
   }
   copy(succ,world);
   return;
}
int aLive(const int gen[][80])
{
   for(int i=0;i<22;i++)
   {
       for(int j=0;j<80;j++)
       {
           if(gen[i][j] == 1)
           {
               return 1;
           }
       }
   }
   return 0;
}
void copy(int a[][80],int b[][80])
{
   for(int i=0;i<22;i++)
   {
       for(int j=0;j<80;j++)
       {
           b[i][j] = a[i][j];
       }
   }
   return;
}
int check(int gen[][80],int i,int j)
{
   int neigbr = 0;
   if(((i-1)>=0)&&((j-1)>=0)&&(gen[i-1][j-1] == 1))
   {
       neigbr++;
   }
   if(((i-1)>=0)&&(gen[i-1][j] == 1))
   {
       neigbr++;
   }
   if(((i-1)>=0)&&((j+1)<80)&&(gen[i-1][j+1] == 1))
   {
       neigbr++;
   }
   if(((j-1)>=0)&&(gen[i][j-1] == 1))
   {
       neigbr++;
   }
   if(((j+1)<80)&&(gen[i][j+1] == 1))
   {
       neigbr++;
   }
   if(((i+1)<22)&&((j-1)>=0)&&(gen[i+1][j-1] == 1))
   {
       neigbr++;
   }
   if(((i+1)<22)&&(gen[i+1][j] == 1))
   {
       neigbr++;
   }
   if(((i+1)<22)&&((j+1)<80)&&(gen[i+1][j+1] == 1))
   {
       neigbr++;
   }
   return neigbr;
}
void dieBorn(int gen[][80],int succ[][80],int neigbr,int i,int j)
{
   if(neigbr>3&&gen[i][j] == 1)
   {
       succ[i][j] = 0;
       cout<<"\n\tCell ["<<i<<"]["<<j<<"]dies of overcrow of "<<neigbr<<" neighbour,";
   }
   else if(neigbr<=1 && gen[i][j] == 1)
   {
       succ[i][j] = 0;
       cout<<"\n\tCell ["<<i<<"]["<<j<<"]dies of loneliness from having "<<neigbr<<" neighbour,";
   }
   else if(neigbr == 3&&gen[i][j] == 0)
   {
       succ[i][j] = 1;
       cout<<"\n\tCell ["<<i<<"]["<<j<<"]is born from having "<<neigbr<<" neighbour,";
   }
   else
   {
       succ[i][j] = gen[i][j];
       return;
   }
}

The screenshot of code and output:

I hope the code would help you, and if you have any doubt please ask in the comments section.

If you did like my code, please do give a thumbs up!!!

Add a comment
Know the answer?
Add Answer to:
This is for C++ The mathematician John Horton Conway invented the “Game of Life.” Though not...
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 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...

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

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

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

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

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

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

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

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