need help to complete my program the game is called game of
need help to complete my program
the game is called \"game of life\";
Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
here is my code but havent finish yet just no ideal how to continue please edit it and make it run, much appreaciated,
gameoflife.cpp:
#include \"gameoflife.h\"
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
namespace csci2312{
Cell::Cell()
{
state=0;
}
Cell::Cell(bool state)
{
face=state;
}
Cell::~Cell()
{
}
bool Cell::getState() const
{
return state;
}
void Cell::setState(bool newState)
{
state=newState;
if(state==0)
face=dead;
else
face=alive;
}
char Cell::getFace() const
{
return face;
}
GameOfLife::GameOfLife()
{
}
GameOfLife::GameOfLife(size_t boardSize)
{
}
GameOfLife::~GameOfLife()
{
}
int GameOfLife::seedBoard(string fileName)
{
int input;
ifstream infile;
infile.open(fileName);
if(infile.fail())
{
cout<<\"The file fail to open.\"<<endl;
return 0;
}
for(int i=0;i<30;i++)
{
for( int j=0;i<30;j++)
{
infile>>input;
currentLife[i][j].setState(input);
}
}
return 0;
}
void GameOfLife::seedBoard(size_t seeds)
{
srand(time(NULL));
size_t count=0;
while(count<seeds)
{
int randNumber1=rand()%(26)+2;
int randNumber2=rand()%(26)+2;
count++;
currentLife[randNumber1][randNumber2].setState(true);
}
}
ostream& operator << (ostream& out, const GameOfLife& board)
{
for(int i=1;i<28;i++)
{
if(i>1)
for(int j=1;j<28;j++)
{
out<<board.currentLife[i][j].getFace();
}
}
}
void GameOfLife::run(unsigned int numberOfIterations)
{
for(int i=2;i<28;i++)
{
for(int j=2;j<28;j++)
{
nextLife[i][j].setState(currentLife[i][j].getState());
}
}
for(int i=2;i<28;i++)
{
for(int j=2;j<28;j++)
{
int neighboarsAlive=0;
if(currentLife[i][j].getState()==false)
{
if(currentLife[i-1][j-1].getState()==1)
neighboarsAlive++;
if(currentLife[i-1][j].getState()==1)
neighboarsAlive++;
if(currentLife[i-1][j+1].getState()==1)
neighboarsAlive++;
if(currentLife[i][j-1].getState()==1)
neighboarsAlive++;
if(currentLife[i-1][j+1].getState()==1)
neighboarsAlive++;
if(currentLife[i+1][j-1].getState()==1)
neighboarsAlive++;
if(currentLife[i+1][j].getState()==1)
neighboarsAlive++;
if(currentLife[i+1][j+1].getState()==1)
neighboarsAlive++;
}
if(currentLife[i][j].getState()==true)
{
if(currentLife[i-1][j-1].getState()==1)
neighboarsAlive++;
if(currentLife[i-1][j].getState()==1)
neighboarsAlive++;
if(currentLife[i-1][j+1].getState()==1)
neighboarsAlive++;
if(currentLife[i][j-1].getState()==1)
neighboarsAlive++;
if(currentLife[i-1][j+1].getState()==1)
neighboarsAlive++;
if(currentLife[i+1][j-1].getState()==1)
neighboarsAlive++;
if(currentLife[i+1][j].getState()==1)
neighboarsAlive++;
if(currentLife[i+1][j+1].getState()==1)
neighboarsAlive++;
}
if(numberOfIterations==true)
{
if(currentLife[i][j].getState()==false)
{
if(neighboarsAlive==3)
{
nextLife[i][j].setState(true);
}
else
nextLife[i][j].setState(false);
}
if(currentLife[i][j].getState()==true)
{
if(neighboarsAlive<2||neighboarsAlive>3)
{
nextLife[i][j].setState(false);
}
else
nextLife[i][j].setState(true);
}
}
}
for(int i=2;i<28;i++)
{
for(int j=2;j<28;j++)
{
currentLife[i][j].setState(nextLife[i][j].getState());
}
}
cin.get();
}
}
}
///////////////
gameoflife.h:
#pragma once
#include <iostream> // Provides ostream
#include <string> // String operations
#include <cstdlib> // Randomizer
namespace csci2312
{
using std::string;
using std::ostream;
class Cell
{
public:
static const char alive =\'o\'; // alive image
static const char dead = \'-\'; // dead image
// Default constructor sets the cell\'s state to false
Cell();
// Custom constructor sets the cell\'s state as per argument
Cell(bool state);
// Empty destructor
~Cell();
// Accessors have no intention to modify the object, so it is a good practice to make them \'const\' functions
bool getState() const;
char getFace() const;
// Mutator to change cell\'s state
void setState(bool newState);
private:
bool state;
char face;
};
class GameOfLife
{
public:
static const unsigned int MAX_BOARD = 30;
GameOfLife();
GameOfLife(size_t boardSize);
~GameOfLife();
int seedBoard(string fileName);
void seedBoard(size_t seeds);
void run();
void run(unsigned int numberOfIterations);
// ADVANCED
// A const(!) accessor method that returns a handle to the private currentLife array.
// The return type must also be \'const\' because we return a pointer to a static array, and these are fixed
// It is just an example. It is not needed if we have a friend operator.
const Cell(*getCurrentLife() const )[MAX_BOARD] { return currentLife; };
/////////////////////////////////////////////////////////////////////////
// friend operator can access private members of GameOfLife
friend ostream& operator << (ostream& out, const GameOfLife& board);
private:
Cell currentLife[MAX_BOARD][MAX_BOARD];
Cell nextLife[MAX_BOARD][MAX_BOARD];
// ADVANCED
// Example how to declare variable c as a pointer/handle to our array of Cells of size MAX_BOARD
// The accessor method getCurrentLife() above uses the same syntax for the return type
const Cell (*c)[MAX_BOARD] = currentLife;
/////////////////////////////////////////////////////////////////////////
size_t boardSize; // Board size requested in the constructor
};
// NON-MEMBER OUTPUT FUNCTIONS
// Display cell\'s state with alive/dead face
ostream& operator << (ostream& out, const Cell& cell);
// Display current board. Implementation needs to be moved to the .cpp file, only here temporarily to demo
ostream& operator << (ostream& out, const GameOfLife& board) {
// ADVANCED
// Example of how to invoke the accessor method getCurrentLife() and access an element of the board
// If we have an accessor, then the operator does not have to be a friend
out << board.getCurrentLife()[0][0];
//////////////////////////////////////////////////////////////
// If the operator is a friend, then any private data member (like currentLife) can be accessed directly.
out << board.currentLife[0][0];
// Example how to access a cell\'s method.
// The board comes in to this operator function with a \'const\' qualifier in front of the board, which means that
// the function cannot alter the board. Therefore only \'const\' functions are allowed to operate on the baord.
// So getState() accessor must be a \'const\' function - please see its declaration above
bool a = board.currentLife[1][2].getState() ;
}
}
#endif /* gameoflife_h */
////////////////////
main.cpp:
#include \"gameoflife.h\"
#include <iostream>
using namespace std;
using namespace csci2312;
int main()
{
int input = 0;
GameOfLife board;
int choice;
unsigned int seeds = 0;
const int File=1, quit=2;
do{
cout<<\"Enter 1 to seed the borad from file\"<<endl;
cout<<\"Enter 2 to quit\"<<endl;
cin>>choice;
if(choice!=3)
{
cout<<\"How many literations of the game of life do you want?\";
cin>>input;
}
if(choice==1)
{
cout<<\"Eneter the number of living seeds to start with: \";
cin>>seeds;
}
while(choice<File||choice>quit)
{
cout<<\"Enter a valid selections: \";
cin>>choice;
}
switch(choice)
{
case File: board.seedBoard(\"gameseed.txt\");
while(input>0)
{
board.run(File);
cout<<board;
--input;
cout<<\"\ \ \";
}
break;
}
}while(choice !=2);
}
please make it run, the sameple output is:
O OO ----oo--o-o--o-o O-OO Press any key to continue... O O O O -O-O-O-O-OOOO- Preas any key to continue...Solution
Here is my code :











