Need help. This will be uploaded to hypergrade. I use dev-c++ 5.1 assignment: Co
ID: 3639092 • Letter: N
Question
Need help. This will be uploaded to hypergrade. I use dev-c++ 5.1
assignment:
Conway's Game of Life
For this assignment your are to write a program, that plays Conway's game of Life. See the Wikipedia definition, if
you have never played the game: http://en.wikipedia.org/wiki/Conway's_Game_of_Life.
Here is how our implementation will work:
(1) The program will ask the user to enter a filename from the console. (Just like the last assignment).
(2) The initial configuration will be read in from a file, which will be a 12 by 30 two-dimensional array of characters. The game board will be surrounded by all O's.
(3) The game will be played on 10 by 28 two-dimensional array. (Can you guess why?). A period ('.') will represent a dead cell and an 'X' will represent a live cell.
You will be severely penalized if your program does not have at least three functions.
Explanation / Answer
please rate - thanks
try this
#include <iostream>
#include <fstream>
using namespace std;
void generation(char[][28]);
void display(char[][28]);
int initialize(char[][28]);
int check(int,int,char[][28]);
int main()
{int good;
char world[22][28];
good=initialize(world);
if(good==1)
while(true)
{display(world);
getchar();
generation(world);
}
return 0;
}
int check(int i,int j,char world[][28])
{if(world[i][j]=='.')
return 0;
return 1;
}
void generation(char world[][28])
{int i,j,count;
for(i=1;i<9;i++)
for(j=0;j<27;j++)
{count=0;
count+=check(i-1,j-1,world);
count+=check(i-1,j,world);
count+=check(i-1,j+1,world);
count+=check(i,j-1,world);
count+=check(i-1,j+1,world);
count+=check(i+1,j-1,world);
count+=check(i+1,j,world);
count+=check(i+1,j+1,world);
}
if(world[i][j]=='X'&&(count<2||count>3))
world[i][j]='.';
if(world[i][j]=='.'&&count==3)
world[i][j]='X';
}
void display(char world[][28])
{int i,j;
for(i=0;i<22;i++)
{for(j=0;j<28;j++)
cout<<world[i][j];
cout<<endl;
}
}
int initialize(char world[][28])
{int i,j;
char filename[30];
cout<<"what is the name of the file you are using? ";
cin>>filename;
ifstream input;
input.open(filename); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
return 0;
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.