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: 3679001 • 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 ‘0’ 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.

Part 2

1.Load a pattern from a supplied file and write it into your display array

2.Display and save the output screen

3.Run the update function for 120 cycles and save the resulting output screen.

Bonus – For 2 bonus points, write a screen wrap algorithm that links the top of the array to the bottom and the left side to the right side. The effectiveness of your screen wrap will be tested in part 2.

Parameters

1.The instructor used a 24 x 79 grid array

2.In order for the intermediate screens to be persistent long enough to view you must build a time delay between cycles. The following code will generate a 1 second delay between cycles. The first cycle duration will be random and less than 1 second.

   startTime = time( 0 );

   elapsedTime = startTime;

   while (elapsedTime == startTime){ elapsedTime = time( 0 );

3.Students may earn a bonus point for finding and using a standard library sleep function

4.The supplied file contains a count of the number of live cells and a listing of the coordinate pairs with values separated by spaces and pairs separated by newlines.

5.The data for part 2 was output to the supplied file by the following code

#include <fstream>;

#include <iostrean>;

void savePattern( int array1[][width], int h, int w)

{

    ofstream outFile("JefPattern.txt", ios::out);

    int total = 0;

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

    {

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

        {

            total += array1[i][j]; // count the number of live cells

        }

    }

    outFile << total << endl; // output the count of point coordinates

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

    {

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

        {

            if (array1[i][j]) outFile << i << ' ' << j << endl; // output the coordinates in pairs

        }

    }

}

Pseudocode – part 1

                  Generate random array.

                  Paint array onto screen.

                  Capture starting screen.

                  While (continue)

                                    Generate new array from display array.

                                    Copy new array to display array.

                                    Paint updated display array.

                  Hold to capture output screen.

Pseudocode – part 2

                  Load array instead of generating random array.

                  Repeat

Explanation / Answer

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <unistd.h>
#define M 24
#define N 79

using namespace std;

void Part1(){
int display[M][N], compute[M][N]={0};
int i,j,filled=0,unfilled=0,total;
double fill_percent;

//randomly fill array with 0s and 1s
cout<<"Starting array is:"<<endl;
for(i=0;i<M;i++){
for(j=0;j<N;j++){
display[i][j]=rand() % 2; //generates random 0 or 1 to fill array spot
cout<<display[i][j]<<" ";
}
cout<<endl;
}

//count the number of filled and unfilled sites
for(i=0;i<M;++i){
for(j=0;j<N;++j){
if(display[i][j]==0){
unfilled++;
}
else if(display[i][j]==1){
filled++;
}
}
}

//simplification variables
total=M*N;
fill_percent=((double)filled/total);
printf("Filled percent %.2f ",((float)filled/total));

cout<<"Updating it for 50 times..."<<endl;

int loop=50;

while(loop--){
int i,j;
for(i=0;i<M;++i){
for(j=0;j<N;++j){
int cnt=0,flag=0;
  
//counting number of 1's in all 8 posible directions
if(j-1>=0 && display[i][j-1]==1)
cnt++;
if(j-1>=0 && i-1>=0 && display[i-1][j-1]==1)
cnt++;
if(i-1>=0 && display[i-1][j]==1)
cnt++;
if(j+1<M && i-1>=0 && display[i-1][j+1]==1)
cnt++;
if(j+1<M && display[i][j+1]==1)
cnt++;
if(j+1<M && i+1<N && display[i+1][j+1]==1)
cnt++;
if(i+1<N && display[i+1][j]==1)
cnt++;
if(j-1>=0 && i+1<N && display[i+1][j-1]==1)
cnt++;

//Computing next iteration values
if(display[i][j]==0){
if(cnt==3){
flag=1;
compute[i][j]=1;
}
}
else if(display[i][j]==1 && (cnt==2 || cnt==3)){
compute[i][j]=1;
}
else
compute[i][j]=0;
}
}

filled=0,unfilled=0;

cout<<"Updated array in cycle "<<50-loop<<":"<<endl;

//printing the updated array and also copying it to display array
for(i=0;i<M;++i){
for(j=0;j<N;++j){
if(compute[i][j]==0){
cout<<" ";
display[i][j]=0;
unfilled++;
}
else if(display[i][j]==1){
cout<<"0 ";display[i][j]=1;
filled++;
}
}
cout<<endl;
}

printf("Cycle nember %d: Filled percent is %.2f ",50-loop,((float)filled/total));

sleep(2); //time gap between each turn, it can be varied as one's convenience(right now it is 2 seconds)

}
}

int main(){

Part1();
//Part2 file is not clearly mentioned in question

return 0;
}