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

Using C++ and Code Blocks: The game of Life, invented by the mathematician John

ID: 3583024 • Letter: U

Question

Using C++ and Code Blocks:

The game of Life, invented by the mathematician John H. Conway, 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 assumed to extend indefinitely in both directions, each cell will have eight neighbors, the eight cells surrounding it.

Birth and deaths occur according to the following rules.

(a)An organism is born in an empty cell that has exactly three neighbors.

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

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

The following display shows the first five generations of a particular configuration of organisms

                                                                              O

                                   O               OOO                 O                      OOO

       OOO                 OOO                                   O O                    O O

          O                    OOO            O O                  O                      OOO

                                                        O                    O                                                                  .

Write a program that will start with the first generation above. Your program must generate the next four generations. This can be done with two functions.

Submit your source code in Blackboard.

Read the first generation from a file named LIFE.txt.

Using only inner part not outer edges of zeroes:

LIFE.txt

0 0 0 0 0 0
0 0 0 0 0 0
0 0 1 1 1 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

Here is the code I have:

#include
#include
#include

using namespace std;

const int max_size = 6;
typedef char arr[max_size][max_size];

int check_neighbor(arr organism,int r, int c)
{
int num=0;
if (organism[r-1][c-1] == '0')
num++;
if (organism[r-1][c] == '0')
num++;
if (organism[r-1][c+1] == '0')
num++;
if (organism[r][c-1] == '0')
num++;
if (organism[r][c+1] == '0')
num++;
if (organism[r+1][c-1] == '0')
num++;
if(organism[r+1][c] == '0')
num++;
if (organism[r+1][c+1] == '0')
num++;
return num;
}

void get_next_gen(arr organism, arr temp)
{
int num;
for (int r = 0; r < max_size-1; r++)
for (int c = 0; c < max_size-1; c++)
{
num = check_neighbor(organism,r,c);
if (num == 3)
temp[r][c] = '0';
else if(num < 2)
temp[r][c] = ' ';
else if(num > 3)
temp[r][c]=' ';
else
temp[r][c] = organism[r][c];
}
}

void print(int n, arr organism,arr obj)
{

  
cout << endl << "Generation " << n << ":" << endl;
for(int i = 0; i < max_size; i++)
{
for(int j = 0; j < max_size; j++)
{
cout << obj[i][j] << " ";
}
cout << endl;
}
}
  
void dublicate_arr(arr organism, arr temp)
{
for (int i=0; i for (int j=0; j organism[i][j]=temp[i][j];
}


void read(int& no_of_gen, arr organism)
{
int n, m, j, no_of_cel=0;
//string line;
ifstream f1("h:\LIFE.txt", ios::in);
if(f1.is_open())
{
while(!f1.eof())
{
for(int i = 0; i < max_size; i++)
{
f1>>organism[j][i];
}
j++;
  
f1.close();
}

}

}

int main()
{
int no_gens=5;
arr arrname = {' '};
arr current, next;
char ch;

//heading();

do {

read(no_gens,current);
print(1, current,obj);
for(int i = 2; i <= no_gens; i++)
{

get_next_gen(current, next);
dublicate_arr(current, next);
print(i, current);
}
cout<<"Continue? (Y/N)";
cin>> ch;
}while(ch=='Y' || ch =='y');
return 0;
}

Any help in getting this to work correctly would be greatly appreciated.

Explanation / Answer

The below program has dependency on the version of C++ compiler you use.

If you face any problem then it must be in the constructor of Grid class. There may be problems in opening the file... If you can correct that as per your compiler then this program has no problems.

// C++ code

#include <iostream>
#include <fstream>

using namespace std;

//The Grid class of the game of life. The grid is a 2D array.
class Grid {
   int width, height;
   bool grid [1000][1000];
public:
   Grid(int, int);
   void nextFrame();
   void printFrame();
};

Grid::Grid(int a, int b) {
   height = a;
   width = b;
  
   ifstream f1("LIFE.txt".c_str(), ios::in);
   if(f1.is_open()){
   while(!f1.eof()){
  
   for(int i = 0; i < 6; i++){
   for(int j = 0; j < 6; j++){
   char c = f1.get(j);
   grid[i][j] = c - '0';
   }
   }
   }
   }

}

/**
Checks how many surrounding cells are alive for a cell and changes it depending on the game of life rules.
The changes are stored in a temporary 2D array. Then, it is copied to the main grid array.
**/
void Grid::nextFrame() {
  
   int numSurrounding = 0;
   bool tempGrid [height][width];
  
   for (int i = 0; i < height ; i++)
   {
       for (int j = 0; j < width ; j++)
       {
           if ( (i+1) < height && grid[i + 1][j] == true )
           {
               numSurrounding++;
           }
           if ( (i-1) >= 0 && grid[i - 1][j] == true )
           {
               numSurrounding++;
           }
           if ( (j+1) < width && grid[i][j+1] == true )
           {
               numSurrounding++;
           }
           if ( (j-1) >= 0 && grid[i][j-1] == true )
           {
               numSurrounding++;
           }
           if ( (i+1) < height && (j+1) < width && grid[i+1][j+1] == true )
           {
               numSurrounding++;
           }
           if ( (i+1) < height && (j-1) >= 0 && grid[i+1][j-1] == true )
           {
               numSurrounding++;
           }
           if ( (i-1) >= 0 && (j+1) < width && grid[i-1][j+1] == true )
           {
               numSurrounding++;
           }
           if ( (i-1) >= 0 && (j-1) >= 0 && grid[i-1][j-1] == true )
           {
               numSurrounding++;
           }
          
           if (numSurrounding < 2 || numSurrounding > 3)
           {
               tempGrid[i][j] = false;
           }
           else if (numSurrounding == 2)
           {
               tempGrid[i][j] = grid[i][j];
           }
           else if (numSurrounding == 3)
           {
               tempGrid[i][j] = true;
           }
          
           numSurrounding = 0;
          
       }
   }
  
   for (int i = 0 ; i < height ; i++ )
   {
       for (int j = 0 ; j < width ; j++ )
       {
           grid[i][j] = tempGrid[i][j];
       }
   }
}

//Prints the grid into the console.
void Grid::printFrame() {
   for (int i = 0; i < height ; i++ )
   {
       for (int j = 0 ; j < width ; j++ )
       {
           if ( grid[i][j] == true )
           {
               cout << "x";
           }
           else
           {
               cout << " ";
           }
          
           if ( (j + 1) >= width )
           {
               cout << endl;
           }
       }
   }
}


int main () {  
   int inputHeight, inputWidth;
   string kbd;

   cout << "The Game of Life in C++ by James Li " ;
   cout << "How many rows? (up to 1000)" << endl;
   cin >> inputHeight;
   cout << "How many columns? (up to 1000)" << endl;
   cin >> inputWidth;
  
   Grid life(inputHeight, inputWidth);
   life.printFrame();
   cout << endl;
  
   do {
       kbd = "";
       cout << "Enter "y" for next cycle, anything else to quit... ";
       cin >> kbd;
       cout << endl;
      
       life.nextFrame();
       life.printFrame();
       cout << endl;
   } while (kbd == "y");
  
   cout << "Goodbye." << endl;
  
   return 0;
}

Output:

// As we continue the next cycle, we get all of them as below.

O

                                   O               OOO                 O                      OOO

       OOO                 OOO                                   O O                    O O

          O                    OOO            O O                  O                      OOO

                                                        O                    O

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote