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

Expected output of this problem : Initial world * * *** *** ====================

ID: 3878809 • Letter: E

Question



Expected output of this problem :
Initial world
*   
*
***
  
  
***   
  
  
===========================
Generation: 1
  
* *
**
*   
*
*
*
  
====================
Generation: 2
  
*
* *
**
  
***   
  
  
====================
Generation: 3
  
*   
**   
**
*
*
*
  
====================
Generation: 4
  
*
*   
***   
  
***   
  
  
====================
Generation: 5
  
  
* *   
**   
* *
*
*
  
====================
Generation: 6
  
  
*   
* *   
**   
***   
  
  
====================
Generation: 7
  
  
*
**
** *
*
*
  
====================
Generation: 8
  
  
*   
*
***
***   
  
  
====================
Generation: 9
  
  
  
* *
** *
* *
*
  
====================
Generation: 10
  
  
  
*
* *
** ***   
  
  
====================

Implement Conway's Game of life . The rules of life sav: o A living cell with two or three neighboring living cells survives into the next generation. A living cell with fewer than two living neighbors dies of loneliness and a living cell with more than three living neighbors will die from overcrowding o Each empty/dead cell that has exactly three living neighbors--no more, no fewer- comes to life in the next generation. Input: o Get the initial state from a file, named "life.txt" in the current working directory o Each row will have dashes for dead cells and asterisks for live ones o To be nice, I will make all lines the same length. Output to standard output (i.e. the screen) o Display the original state of the world, as read in from the file and 10 additional generations. o In each generation, each row will have spaces and asterisks. (spaces look nicer than dashes) - Spaces represent empty/dead cells Asterisks represent occupied/living cells Suggestion: For your internal representation, create an extra row on the top and bottom as well as an extra column to the right and the left of the actual data. Mark the extra cells as dead. This will simplify your code, as you won't need special checks for "boundary" cells Sample input and output for the program are attached Good programming style o Naming o Use of functions o Length of lines and functions o Comments are not required for labs except be sure there is a comment at the top of the file identifying the author (you, hopefully), the name of the program file, and a [very] brief description of the program Note that all of your submissions for the semester should have such a comment at the beginning of each code file

Explanation / Answer

You can define the maximum number of lines in the Conway's Game of life in MXSZ. By default, I have set it to 10^5 + 1.

Since the question did not ask for comments, I have not written the comments.

The Code is as follows:

#include <bits/stdc++.h>
using namespace std;
#define MXSZ 100001
/*
    Conway's Game Of Life
*/
string str[MXSZ],newstr[MXSZ];
int len,counter=0;
int will_live(int x,int y,int required)
{
   int arr[]={1,1,0,-1,-1,-1,0,1};
   int brr[]={0,1,1,1,0,-1,-1,-1};
   int cnt=0;
   for(int i=0;i<8;i++)
   {
       if(str[x+arr[i]][y+brr[i]]=='*')
           cnt++;
   }
   if(cnt==required)
       return 1;
   else
       return 0;

}
void PrintGen()
{
   for(int i=1;i<counter+1;i++)
   {
       for(int j=1;j<len;j++)
       {
           if(str[i][j]=='-' && will_live(i,j,3))
           {
               newstr[i][j]='*';              
           }
           else if(str[i][j]=='*' && ( !will_live(i,j,2) && !will_live(i,j,3) ) )
           {
               newstr[i][j]='-';      
           }
           else
               newstr[i][j]=str[i][j];
          
           if(newstr[i][j]=='-')
               cout<<" ";
           else
               cout<<"*";
       }
       cout<<endl;
   }
   for(int i=1;i<=counter;i++)
       str[i]=newstr[i];
}
void PrintInitialLine(string s)
{
   for(int i=1;i<s.length()-1;i++)
   {
       if(s[i]=='-')
           cout<<" ";
       else
           cout<<"*";
   }
   cout<<endl;
}
int main()
{
   fstream myfile;
   myfile.open("life.txt",ios::in);
   string line;
   cout<<"Initial world"<<endl;
   while(getline(myfile,line))
   {  
       counter++;
       line='-'+line+'-';
       str[counter]=line;
       newstr[counter]=line;
       PrintInitialLine(line);  
       len=str[counter].length()-1;
   }
   for(int i=1;i<=10;i++)
   {
       cout<<"================="<<endl;
       cout<<"Generation "<<i<<endl;
       PrintGen();
   }
   myfile.close();  
   return 0;
}

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