Previous page//  Conway's Game of Life

// Programmer:

#include <iostream.h>

#include "apstring.h"

#include "apvector.h"

#include "apmatrix.h"

#include <time.h> //gets system time

 

//function to print vector

void printoutarray (apvector<int> & onerow) {

for (int x=0; x< onerow.length(); x++) {

{if (onerow[x]==0)

cout<<"-"<<"\t";

else if(onerow[x]==1)

cout<<"*"<<"\t";}

//cout<<onerow[x]<<"\t";

}

cout<<endl;

}

//function below sends one row ata time of matrix to print vector

void printmatrix (apmatrix<int> e){

cout<<endl;

for (int row=0; row< e.numrows(); row++) {

printoutarray (e[row]);

}

}

 

int sumneighbors(apmatrix<int> matrixcopy2,int roworg,intcolorg) {

int total=0;

 

int rowstart=0; // if row is 0th row - special case, changes ifroworg>0

int colstart=0; //if col is 0th col - special case, changes belowwhen colorg>0

int rowend = roworg +1;

int colend = colorg +1 ;

 

if (roworg > 0) // if row is 0th row - special case, changes ifroworg>0

rowstart=roworg-1;

 

if (colorg >0) //if col is 0th col - special case, changesbelow when colorg>0

colstart=colorg-1;

if (roworg == matrixcopy2.numrows()-1) //if org at last row -special case

rowend = roworg;

if (colorg == matrixcopy2.numcols()-1) //if org at last column -special case

colend = colorg;

 

for (int rows=rowstart;rows<=rowend;rows++)

{

for (int cols=colstart;cols<=colend;cols++)

{

total=total + matrixcopy2[rows][cols];

}

}

total=total-matrixcopy2[roworg][colorg];

return(total);

}

 

void checkmatrix(apmatrix<int>&nextcopy,apmatrix<int>const &originalm, int numtolive){

int total = 0;int checksum = 0;

for (int rows = 0;rows < originalm.numrows();rows++) {

for (int cols = 0;cols < originalm.numcols(); cols ++) {

checksum=sumneighbors(originalm,rows,cols);

 

if (checksum== numtolive)

nextcopy[rows][cols]=1;

else

if ((checksum < (numtolive-1)) || (checksum > numtolive) )

nextcopy[rows][cols]=0;

}

}

}

 

void populate(apmatrix<int> &mat )

{

srand(time(0));

int numberofRows,numberofCols;

 

cout<<endl<<"How many rows and columns ? (row# col#)";

cin>>numberofRows>>numberofCols;

mat.resize(numberofRows,numberofCols);

 

for (int x=0;x<numberofRows;x++)

{

for (int y=0;y<numberofCols;y++)

{

mat[x][y]=rand()%2;;

}

}

}

 

void main(void) {

cout<<"WELCOME TO CONWAY'S GAME OF LIFE"<<endl;

cout<<"Standard rules of the game:"<<endl;

cout<<"1. An organism is born in an empty cell that hasexactly 3 neighbors."<<endl;

cout<<"2. An organism dies from loneliness if it has fewerthan 2 neighbors."<<endl;

cout<<"3. An organism's state remains unchanged if it has 2neighbors."<<endl;

cout<<"4. An organism dies from overcrowding if it has morethan 3 neighbors."<<endl;

char answer; int numtolive;

cout<<"Do you want to change the rules of the Game of Life?(y or n)"<<endl;

cin>>answer;

if(answer=='y')

{

cout<<"How many neighbors does the cell need in order tosurvive?"<<endl;

cin>>numtolive;

}

else

{

numtolive=3;

 

}

int desiredCount,count;

apmatrix<int>originalmatrix(0,0,0);

populate(originalmatrix);

cout<<endl<<"This is original matrix : "<<endl;

printmatrix(originalmatrix);

cout<<endl<<"How many new generations do you want tosee ? ";

cin>>desiredCount;

for (count = 0;count<desiredCount;count ++)

{

apmatrix<int>nextgeneration(originalmatrix); //copy function

checkmatrix(nextgeneration,originalmatrix, numtolive);

cout<<endl<<"This is next generation matrix :"<<endl;

printmatrix(nextgeneration);

originalmatrix=nextgeneration;

}

 

}