Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HW#5 Game of Life The game of life belongs to a class called “Cellular Automata”

ID: 3690164 • Letter: H

Question

HW#5 Game of Life

The game of life belongs to a class called “Cellular Automata”. It is a simulation of biological survival and growth.
The rules are simple
1.   The game is played on a rectangular grid of any size.
2.   The game consists of cycles
3.   At each cycle of the game, every cell on the grid is either alive, “1”, or dead, “0”.
4.   In the 0 cycle a starting pattern is generated.
5.   At each subsequent cycle the pattern is evolved according to the rules.
6.   The state of a cell may change from cycle to cycle depending on the population of the 8 adjacent cells.
a.   If a cell is dead, “0” and exactly three of its closest 8-neighbors are alive, “1”, in the current cycle, then the cell will be born, “1”, in the next cycle.
b.   If a cell is alive, “1” in the current cycle, and either 2 or 3 of its neighbors are alive it will survive, “1”, into the next cycle.
c.   In all other cases, the cell dies “0” in the next cycle.
7.   The game continues for as long as the user chooses to run the cycling.
8.   Some patterns grow, some are stable, and some die off over time.
9.   You will display the current state at each cycle by printing spaces for dead cells and a character for live cells in the corresponding positions on your display.

Solving the problem requires 2 arrays, one to calculate the next state and one to hold and display the current state. During recalculation, the calculation array accepts the next state information. Once the calculation of the entire array is complete, the calculation array is copied to the display array and the new state is printed to the screen along with a fill percentage, a cycle count, and a signature.

Part 1
1.   Write a program to generate and display a random starting array
2.   Calculate and display fill percentage. (2 decimal places please)
3.   Save the output screen.
4.   Write a function to update “cycle” the array.
5.   Write a function to display the array
a.   Cycle through the array
b.   Print a ‘Q’ in every live cell.
c.   Print a space ‘ ‘ in every dead cell.
6.   Calculate and display fill percentage and cycle number
7.   Run the program for 50 update cycles and save the output screen.

Output: Required – Initial random pattern with fill percentage

The example generated here used a threshold of .5 for the random number generator.
The actual fill percentage is slightly different due to the random nature of the process.
See page 2

Output: Required – Evolved Pattern after 50 Cycles

Note: Print fill percentage, cycle numbers, and signature at the bottom of the screen
The console window used had 25 lines of 80 characters
A single line is reserved at the bottom of the screen for fill percentage, cycle count, and signature.
A width of 80 was used for this array to fill the width of the screen
A height of 24 was used to allow a text line at the bottom of the screen.

Explanation / Answer

//program

#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int getAliveCount(char array[][80],int x,int y)
{
   int count=0;
   int startPosX = (x - 1 < 0) ? x : x-1;
int startPosY = (y - 1 < 0) ? y : y-1;
int endPosX = (x + 1 > 23) ? x : x+1;
int endPosY = (y + 1 > 79) ? y : y+1;


// See how many are alive
for (int rowNum=startPosX; rowNum<=endPosX; rowNum++) {
for (int colNum=startPosY; colNum<=endPosY; colNum++) {
if(array[rowNum][colNum]=='1')
{
   count++;
       }
}
}
   return count;
}
int main()
{
  
   char current[24][80];
   char next[24][80];
   int count;
   srand (time(NULL));
   for(int cycle=0;cycle<=50;cycle++)
   {
       if(cycle==0)
       {
           //generate starting pattern
           cout<<endl<<"Generating starting pattern";
           int range =10 -1+1;
           int n=(range *rand()/RAND_MAX)+1;
           char c='1';
           for(int k=0;k<n;k++){
           range =79 -0+1;
           int p1=(range *rand()/RAND_MAX)+0;
           int p2=(range *rand()/RAND_MAX)+0;
           current[p1][p2]=c;
           if(c=='1')
           c='0';
           else if(c=='0')
           c='1';
       }
       }
       else
       {
           for(int i=0;i<24;i++)
           {
               for(int j=0;j<80;j++)
               {
                   if(current[i][j]=='0')
                   {
                   count=getAliveCount(current,i,j);
                   if(count==3)
                   {
                       next[i][j]='1';
                   }
                   else
                   {
                       next[i][j]='0';
                   }
                   }
                   else if(current[i][j]=='1')
                   {
                   count=getAliveCount(current,i,j);
                   if(count==2 ||count==3)  
                   {
                       next[i][j]='1';
                   }
                   else
                   {
                       next[i][j]='0';
                   }
                   }
               }
           }
           //display in screen at each cycle
           int fill=0;
           for(int i=0;i<24;i++)
           {
               for(int j=0;j<80;j++)
               {
                   if(current[i][j]=='1')
                   {
                       cout<<"Q";
                       fill++;
                   }
                   else if( current[i][j]=='0')
                   {
                       cout<<" ";
                   }
               }
           }
           cout<<endl<<"Cycle Number: "<<cycle<<" Fill Percentage: "<<(fill/(80*24))*100<<" %"<<endl;
           //copy next to current
           for(int i=0;i<24;i++)
           {
               for(int j=0;j<80;j++)
               {
                   current[i][j]=next[i][j];
               }
           }
          
       }
   }
   return 0;
}