Thursday, 19 September 2013

c++ : the game of life?

c++ : the game of life?

i really need your help and really fast. i've been working on this project
where i am supposed to create a simulation of conway's game of life.
A cell can only be in one of two states: alive or dead. There are two
kinds of cells: ConwayCells and FredkinCells.
Live ConwayCells are denoted with an asterisk, "*", and dead cells are
denoted with a period, "." A ConwayCell has 8 neighbours, if it's an
interior cell, 5 neighbours, if it's an edge cell, and 3 neighbours, if
it's a corner cell
ConwayCells do not have the notion of age, FredkinCells do. A
FredkinCells' age is initially zero and only increments by one if the cell
is alive and stays alive. Live FredkinCells are denoted with their age, if
their age is less than 10, otherwise denoted with a plus, "+", and dead
cells are denoted with a minus, "-". A FredkinCell has 4 neighbours, if
it's an interior cell, 3 neighbours, if it's an edge cell, and 2
neighbours, if it's a corner cell.
The rules for going from one generation to the next for ConwayCells are:
a dead cell becomes a live cell, if exactly 3 neighbours are alive
a live cell becomes a dead cell, if less than 2 or more than 3 neighbours
are alive
The rules for going from one generation to the next for FredkinCells are:
a dead cell becomes a live cell, if 1 or 3 neighbours are alive
a live cell becomes a dead cell, if 0, 2, or 4 neighbours are alive
I have to create 5 classes: *Cell class(abstract) from which two are
derived ConwayCell and FredkinCell where each of these cells knows how to
print itself, whether it is alive or not and how many neighbours are
alive. *Life class(abstract) from which two others are derived :
fredkinLife and conwayLife.the subclasses should have functions that keeps
track of the grid (2d dynamic array) and generation , that plays life ,
that evolves , and that prints itself.
And a LifeFactory class that would shoose between creating a fredkinLife
or a ConwayLife.
To push Life forward one generation (evolve), traverse the grid of cells,
only notice the live cells, and for each of them, visit the neighbours and
increment their respective neighbour counts. Remember that the two kinds
of cells have different definitions of neighbour. Traverse the grid a
second time, set the next state, and zero out the neighbour count.
Remember that the two kinds of cells have different rules for the next
state.
In a main program, you will read in initial board states from an input
file and iterate through several generations of the game of life.
#include <iostream>
#include <fstream>
using namespace std;
const int _rows = 109;
const int _columns = 69;
const int maxRows = 20;
const int maxColumns = 20;
class Cell{
protected:
char cell;
int count; // nb of neighbors alive
public:
Cell( char unit = NULL , int n = 0 ){
cell = unit;
count = n;
}
int get_count(){
return count;
}
virtual bool is_live()=0;
virtual int _neighbors()=0;
virtual void print();
};
class ConwayCell : public Cell {
public:
ConwayCell( char unit = NULL , int n = 0 ){
count = n;
cell = unit;;
}
bool is_live(){
if( cell = '*' )
return true;
else if( cell = '.' )
return false;
}
int _neighbors(){
char c[_rows][_columns] = {0};
for( int i=0; i<_rows ; ++i){
for( int j=0; j<_columns ; ++j){
//if( (i == 0 && j == 0) || ( i == _rows - 1 && j ==
_columns - 1 ) || (i == 0 && j == _columns - 1 ) || (i ==
_rows && j == 0) ){
if( c[ i + 1][j] == '*')
count++;
if( c[i][j+1] == '*')
count++;
if( c[i+1][j+1] == '*')
count++;
if( c[i-1][j] == '*')
count++;
if( c[i-1][j-1] == '*')
count++;
if( c[i][j-1] == '*')
count++;
if( c[i-1][j+1] == '*')
count++;
if( c[i+1][j-1] == '*')
count++;
}
}
return count;
}
void print(){
if( ConwayCell::is_live())
cout << '*' ;
else
cout << '.' ;
}
};
class FredkinCell : public Cell {
int cellAge;
public:
FredkinCell( char unit = NULL , int n = 0 , int age = 0 ){
count = n;
cell = unit;
cellAge = age;
if(cell == '+')
cellAge = 10;
else if (cell != '-')
age = (cell - '0');
}
int get_age(){
return cellAge;
}
bool is_live(){
switch (cell){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '+':
return true;
break;
default:
return false;
}
}
char age(){
if(!FredkinCell::is_live())
return '-';
else if(cellAge>9)
return '+';
return cellAge + '0';
}
int _neighbors(){
char c[maxRows][maxColumns];
for( int i=0; i<maxRows ; ++i){
for( int j = 0; j<maxColumns ; ++j){
if( (c[i+1][j] >= '0' && c[i+1][j] < '10') || c[i+1][j]
== '+' )
count++;
if( (c[i][j+1] >= '0' && c[i][j+1] < '10') || c[i][j+1]
== '+')
count++;
if( (c[i+1][j+1] >= '0' && c[i+1][j+1] < '10') ||
c[i+1][j+1] == '+')
count++;
if( (c[i-1][j] >= '0' && c[i-1][j] < '10') || c[i-1][j]
== '+')
count++;
if( (c[i-1][j-1] >= '0' && c[i-1][j-1] < '10') ||
c[i-1][j-1] == '+')
count++;
if( (c[i][j-1] >= '0' && c[i][j-1] < '10') || c[i][j-1]
== '+')
count++;
if( (c[i-1][j+1] >= '0' && c[i-1][j+1] < '10') ||
c[i-1][j+1] == '+')
count++;
if( (c[i+1][j-1] >= '0' && c[i+1][j-1] < '10') ||
c[i+1][j-1] == '+')
count++;
}
}
return count;
}
void print(){
for( int i=0; i < maxRows ; ++i){
for( int j=0; j< maxColumns; ++j){
if(!FredkinCell::is_live())
cout << "-" ;
else
cout << cell ;
}
}
}
};
class Life {
Cell ** c;
int generation;
int population;
public:
Life(Cell **cell = NULL, int g=0, int p=0){
cell = c;
generation = g;
population = p;
}
virtual void grid() = 0;
virtual void evolve() = 0;
virtual void play() =0;
virtual void print() =0;
};
class ConwayLife {
public:
ConwayLife(){
c = new char * [_rows];
for( int i=0; i< _rows ; ++i)
c[i] = new char[_columns];
}
void grid();
void play();
void evolve();
void print (){
for( int i=0; i< _rows; ++i){
for( int j=0; j< _columns ; ++j){
cout << c[i][j] << " ";
}
cout << endl;
}
}
~ConwayLife(){
for( int i=0; i< _rows;++i)
delete [] c[i];
delete [] c;
}
};
class FredkinLife {
public:
FredkinLife(){
c = new char * [_rows];
for( int i=0; i< _rows ; ++i)
c[i] = new char[_columns];
}
void grid();
void play();
void evolve();
void print (){
for( int i=0; i< maxRows; ++i){
for( int j=0; j< maxColumns ; ++j){
cout << c[i][j] << " ";
}
cout << endl;
}
}
~FredkinLife(){
for( int i=0; i< maxRows;++i)
delete [] c[i];
delete [] c;
}
};
basically what i am asking for is someone to check if my code is correct
and to help me implement gthe other functions in class Life (no code is
required) just detailed explanation

No comments:

Post a Comment