Need help writing Conways Game of Life These are the instruc

Need help writing Conway\'s Game of Life. These are the instructions:

The game of Life is intended to model life in a society of organisms. Consider a rectangular array of cells, each of which may contain an organism. If the array is considered to extend indefinitely in both directions, then each cell has eight neighbors, the eight cells surrounding it. Births and deaths occur according to the following rules:

An organism is born in any empty cell having exactly three neighbors.

An organism dies from isolation if it has fewer than two neighbors.

An organism dies from overcrowding if it has more than three neighbors.

All other organisms survive to the next generation.

Write a program to play the game of Life and investigate the patterns produced by various initial configurations. Some configurations die off rather rapidly; others repeat after a certain number of generations; others change shape and size and may move across the array.

NOTE:

For representing each organism, use \"@\" characters.

For an array of cells, use 30x30 two-dimensional array.

Each generation should be displayed on the screen and should be paused.

(You can use cin.get( ) library function to pause running your program.)

4. The source file should be called life.cpp

5. All assignments are expected to be INDIVIDUAL work. All work handed in must be original. Duplicate or very similar programs receive zero points.

Input

A set of initial cells will be given using the interactive way using a keyboard.

The followings are the prompt for the input:

Please Enter the number of initial cells: 4

The position of cell is 10 10

The position of cell is 10 11

The position of cell is 10 12

The position of cell is 11 11

How many generations do you want to display? 3

Output

The program then display each generation of organisms at a time.

The 1-generation

                                            @@@

                                                @

Press any key to continue!!

(This picture should be displayed on the new screen.)

The 2-generation

                                                @

                                            @@@

                                            @@@

Press any key to continue!!

The 3-generation

                                           @@@

                                               

                                            @    @

                                                @

Do you want to do it again?(Yes/No)    N

Solution

Note: User given template is used.

Answer:

#include<iostream>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iomanip>
using namespace std;
int orAry[30][30];
void initilaize()
{
   for(int kk=0;kk<30;kk++)
   {
       for(int bb=0;bb<30;bb++)
       {
           orAry[kk][bb]=0;
       }
   }
}
void disply()
{
   cout<<endl;
   for(int kk=0;kk<30;kk++)
   {
       for(int bb=0;bb<30;bb++)
       {
           if(orAry[kk][bb]==1)
               cout<<\"@\";
           else
           cout<<\" \";
       }
       cout<<endl;
   }
}

int ChechNeighBourCount(int kk,int aa)
{
   int cc=0;
   if(orAry[kk][aa-1]==1 && aa-1>=0 )
{
cc++;
}
   if( orAry[kk+1][aa-1]==1 && kk+1<30 && aa-1>=0)
{
cc++;
}
   if( orAry[kk+1][aa]==1 && kk+1<30)
{
cc++;
}
   if( orAry[kk][aa+1]==1 && aa+1<30)
{
cc++;
}
   if( orAry[kk-1][aa-1]==1 && kk-1>=0 && aa-1>=0)
{
cc++;
}
   if(orAry[kk+1][aa+1]==1 && kk+1<30 && aa+1<30)
{
cc++;
}
   if(orAry[kk-1][aa]==1 && kk-1>=0)
{
cc++;
}
   if( orAry[kk-1][aa+1]==1 && kk-1>=0 && aa+1<30)
{
cc++;
}
   return cc;
}
  
void runLife()
{
   int bCnt=0;
   int dCnt=0;
   for(int kk=0;kk<30;kk++)
   {
       for(int bb=0;bb<30;bb++)
       {
      
           if(orAry[kk][bb]==0)
           {
               bCnt=ChechNeighBourCount(kk,bb);
              
               if(bCnt==3)
               {
                   orAry[kk][bb]=1;
               }
           }
           else if(orAry[kk][bb]==1)
           {
               dCnt=ChechNeighBourCount(kk,bb);              
               if(dCnt>3)
               {
                   orAry[kk][bb]=0;
               }
               if(dCnt<2)
               {
                   orAry[kk][bb]=0;
               }
           }
       }
   }
}
          
int main()
{
   int rr,cc;
   int npos=0;
   int ngen=0;
   int xv,yv;
   char re;
   srand(time(NULL));
   do
   {
   initilaize();
   cout<<\"Please Enter the number of initial cells:\";
   cin>>npos;
   for(int kk=0;kk<npos;kk++)
   {
       cout<<\"The position of cell is \"<<endl;
       cin>>xv>>yv;
       if(orAry[xv][yv]==0)
       {
         
           orAry[xv][yv]=1;
       }
   }
   cout<<\"How many generations do you want to display?\";
   cin>>ngen;
   for(int kk=0;kk<ngen;kk++)
   {
       cout<<(kk+1)<<\" - generation \"<<endl;
       disply();      
       runLife();
       cout<<\"Press any key to continue\"<<endl;
       cin.get();
   }
   cout<<\"Do you want to do it again?(Yes/No) \";
   cin>>re;
   }while(re!=\'N\');
   return 0;
}
  

Sample Output:
Please Enter the number of initial cells:4
The position of cell is
10 10
The position of cell is
10 11
The position of cell is
10 12
The position of cell is
11 11
How many generations do you want to display?3
1 - generation

  
  
  
  
  
  
  
  
  
  
@@@   
@
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
Press any key to continue
2 - generation

  
  
  
  
  
  
  
  
  
@@   
@ @   
@
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
Press any key to continue

3 - generation

  
  
  
  
  
  
  
  
  
@@   
@ @   
@
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
Press any key to continue

Do you want to do it again?(Yes/No) N

Exit code: 0 (normal program termination)

Need help writing Conway\'s Game of Life. These are the instructions: The game of Life is intended to model life in a society of organisms. Consider a rectangul
Need help writing Conway\'s Game of Life. These are the instructions: The game of Life is intended to model life in a society of organisms. Consider a rectangul
Need help writing Conway\'s Game of Life. These are the instructions: The game of Life is intended to model life in a society of organisms. Consider a rectangul
Need help writing Conway\'s Game of Life. These are the instructions: The game of Life is intended to model life in a society of organisms. Consider a rectangul
Need help writing Conway\'s Game of Life. These are the instructions: The game of Life is intended to model life in a society of organisms. Consider a rectangul
Need help writing Conway\'s Game of Life. These are the instructions: The game of Life is intended to model life in a society of organisms. Consider a rectangul

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site