1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
//Declaring Function Varibles
const int MAX_ARRAY_SIZE=50;
//FUNCTION//forprinting initiail board and title
void PrintGen(char lifeBoard[][MAX_ARRAY_SIZE], ostream& outStream, int numRowsInBoard, int numColsInBoard, int generationNum)
{
if(generationNum==0)
{
outStream<<"Initial board"<<endl;
}
else
{
outStream << "LIFE game board generation "<<generationNum << endl;
}
for(int countrow=0;countrow<numRowsInBoard; countrow++)
{
for(int countcol=0;countcol<numColsInBoard; countcol++)
{
outStream<< lifeBoard[countrow][countcol];
outStream<<" ";
}
outStream<< endl;
}
outStream<<endl<<endl<<endl;
}
//FUNCTION// for printing NextGen
void NextGen(char lifeBoard[][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard)
{
char nextboard[50][50]={0};
for(int countcol=0; countcol<numColsInBoard; countcol++)
{
for(int countcol2=0;countcol2<numColsInBoard; countcol2++)
{
nextboard[countcol][countcol2]='.';
}
}
//Checking neighbours
for (int countrows=1; countrows<numRowsInBoard-1;countrows++)
{
for(int countcols=1; countcols<numColsInBoard-1; countcols++)
{
//Declaring Varibles
int beside;
//initializing neighbour
beside=0;
if(lifeBoard[countrows+1][countcols+1]=='X')
{
beside++;
}
if(lifeBoard[countrows+1][countcols-1]=='X')
{
beside++;
}
if(lifeBoard[countrows+1][countcols]=='X')
{
beside++;
}
if(lifeBoard[countrows][countcols-1]=='X')
{
beside++;
}
if(lifeBoard[countrows][countcols+1]=='X')
{
beside++;
}
if(lifeBoard[countrows-1][countcols-1]=='X')
{
beside++;
}
if(lifeBoard[countrows-1][countcols]=='X')
{
beside++;
}
if(lifeBoard[countrows-1][countcols+1]);
{
beside++;
}
if(beside==2 || beside==3)
{
if(beside==3 && lifeBoard[countrows][countcols]=='X')
{
nextboard[countrows][countcols]='X';
}
if(beside==2 && lifeBoard[countrows][countcols]=='X')
{
nextboard[countrows][countcols]='X';
}
if(beside==3 && lifeBoard[countrows][countcols]=='.')
{
nextboard[countrows][countcols]=='X';
}
if(beside<2 || beside>3)
{
nextboard[countrows][countcols]='.';
}
}
}
for(int colinboard=0; colinboard<numColsInBoard;colinboard++)
{
for(int colinboard2=0;colinboard2<numColsInBoard;colinboard2++)
{
lifeBoard[colinboard][colinboard2]=nextboard[colinboard][colinboard2];
}
}
}
}
int main()
{
//Declaring Varibles
int row;
int column;
int gens;
int generationNum;
int minarray;
int maxarray;
ifstream inputStream;
ofstream outputStream;
char inf[99];
char outf[99];
char Lifeboard[50][50]={0};
//Initializing Varibles
row=0;
column=0;
gens=0;
generationNum=1;
minarray=0;
maxarray=50;
//Prompting for file name
cout<<"Enter input file name: ";
cin>>inf;
inputStream.open(inf);
//Check if file was opened
if(inputStream.fail())
{
cerr<<"ERROR : input file not opened correctly."<<endl;
return 1;
}
//Prompting for output
cout << "Enter the name of the output file: ";
cin >> outf;
outputStream.open(outf);
outputStream << "File Open" << endl; // Test
if (outputStream.fail())
{
cerr << "Error, output file not opened correctly." << endl;
return 1;
}
//Checking for rows
if(!(inputStream>>row))
{
cerr<<"ERROR: Cannot read number of rows."<<endl;
return 1;
}
//Checking valid number of rows
if (row<=0 || row >49)
{
cerr<<"ERROR: Read an illegal number of rows for the board"<<endl;
return 1;
}
//Checking for columns
if(!(inputStream>>column))
{
cerr<<"ERROR: Cannot read number of columns."<< endl;
return 1;
}
//Checking for valid number of columns
if(column<=0|| column>49)
{
cerr<<"ERROR: Read an illegal number of columns for the board."<<endl;
return 1;
}
//Checking for generations
if(!(inputStream>>gens))
{
cerr<<"ERROR: Cannot read the number of generations."<<endl;
return 1;
}
//Checking for valid number of generations
if (gens<0)
{
cerr<<"ERROR: Read an illegal number of generations."<<endl;
return 1;
}
//Readng for valid board
for( int countrow=0;countrow<row;countrow++)
{
for(int countcol=0;countcol<column;countcol++)
{
inputStream>> Lifeboard[countrow][countcol];
//no data in file
if(inputStream.eof())
{
cerr<<"ERROR; Not enough data in the input file"<< endl;
return 1;
}
if(Lifeboard[countrow][countcol]!='.'&& Lifeboard[countrow][countcol] !='X')
{
cerr<<"Error : Input data for initial board is incorrect"<< endl;
}
return 1;
}
}
//Checking boarders
for(int rowboarder=0;rowboarder<row;rowboarder++)
{
if(Lifeboard[0][rowboarder] =='X' || Lifeboard[row-1][rowboarder]=='X')
{
cerr<<"ERROR: Organisims are present in the boarder of the board, please correct your iput file."<< endl;
return 1;
}
}
for(int colboarder=0; colboarder<column;colboarder++)
{
if(Lifeboard[colboarder][0]=='X' || Lifeboard[colboarder][column-1] == 'X')
{
cerr<<"ERROR: Organisms are present in the boarder of the board, please correct your input file."<<endl;
return 1;
}
}
//Printing initial board
PrintGen(Lifeboard, cout, row, column, 0);
for(int currentgen=0;currentgen<=gens;currentgen++)
{
PrintGen(Lifeboard,outputStream, row, column, currentgen);
NextGen(Lifeboard,row,column);
}
PrintGen(Lifeboard,cout,row,column,gens);
outputStream << "Done" << endl; // Test
outputStream.close();
return 0;
}
|