For this assignment you will be impelementing the classic cellular automotan Con
ID: 642991 • Letter: F
Question
For this assignment you will be impelementing the classic cellular automotan Conway's Game of Life.
Rules
A new cell is born on an empty square if is surrounded by exactly three neighboring cells
A cell dies of overcrowding if it has four or more neighbors
A cell dies of loneliness if it has zero or one neighbor
Any live cell with two or three live neighbors lives, unchanged, to the next generation.
The initial state of the game will be held in a text file provided by the driver program (you must use this driver program). The format will be sequences of ones and zeros. A one means that a cell is living in that square. You will determine the size of the grid display by reading the first line of the text file and seeing how many columns you will need. You will determine the number of rows in the grid by counting the lines of data.
00000000100000001010
00000000010000001001
11100000010100000010
10100100101010101010
00101010010010101000
This grid may be of arbitrary size.
Jeremy Allen's Glider Gun fight grid (set the iterations to 500).
When printing out the status of the grid, print one character to standard output per cell in the automaton. You may use whatever characters you wish to show the status of the cell. The driver program will clear the screen between generations.
You must create a .cpp file and a header file that work with the following driver program:
Explanation / Answer
Complete Program:
// File: life.h
#ifndef LIFE_H
#define LIFE_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void populateWorld(const string filename);
void showWorld();
void iterateGeneration();
void findNeighbours(int r, int c);
#endif
---------------------------------------------------------------------------------------------------------------
// File: life.CPP
#include "life.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int ROWS;
int COLUMNS;
string *text;
int neighbours = 0;
const int RESIZE = 3;
void populateWorld (const string filename)
{
char line[80];
ROWS = 0;
COLUMNS = 0;
ifstream inFile;
inFile.open(filename.data());
text = new string[RESIZE];
while(inFile.getline(line, 80) )
{
text[ROWS] = line;
COLUMNS = text[ROWS].length();
ROWS++;
if(ROWS % RESIZE == 0)
{
string *temp = new string[ROWS + RESIZE];
for (int i = 0; i < ROWS; i++)
temp[i] = text[i];
delete[] text;
text = temp;
}
}
}
void showWorld()
{
for(int r = 0; r < ROWS; r++)
{
for(int c = 0; c < COLUMNS; d++)
{
cout << text[r][d];
}
cout << endl;
}
cout << endl;
}
void iterateGeneration ()
{
string *temptext = new string[ROWS];
for(int r = 0; r < ROWS; r++)
{
string tempstr = "";
for (int c = 0; c < COLUMNS; c++)
{
findNeighbours(r, c);
if(text[r][c] == 1)
{
if (neighbours >= 4)
{
tempstr += (char)(text[r][c] -1);
}
if (neighbours <= 1)
{
tempstr += (char)(text[r][c] -1);
}
}
else if (neighbours == 3)
{
tempstr += (char)(text[r][c] +1);
}
else
tempstr += (char)(text[r][c]);
}
temptext[r] = tempstr;
}
delete [] text;
text = temptext;
return;
}
void findNeighbours(int r, int c)
{
if(r+1 < ROWS && text[r+1][c] == true)
{
neighbours += 1;
}
else if(r-1 >= 0 && text[r-1][c] == true)
{
neighbours += 1;
}
else if(c+1 < COLUMNS && text[r][c+1] == true)
{
neighbours += 1;
}
else if (c-1 >=0 && text[r][c-1] == true)
{
neighbours += 1;
}
else
neighbours = 0;
}
---------------------------------------------------------------------------------------------------------------
// File: driver.cpp
#include <iostream>
#include <fstream>
#include "life.h"
const string FILE_NAME = "starting_grid.txt";
using namespace std;
const int NUM_GENERATIONS = 2;
int main()
{
populateWorld(FILE_NAME);
showWorld();
for (int iteration = 0; iteration < NUM_GENERATIONS; iteration++)
{
system("cls");
iterateGeneration();
showWorld();
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.