-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife.cpp
387 lines (374 loc) · 14.8 KB
/
gameoflife.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//=====================================================================================================================
// Name : gameoflife.cpp
// by : Meshal Cheema
// roll no : i191977
// section : C (lab)
//=====================================================================================================================
#include <iostream>//function to invoke cin, cout
#include <fstream>//functtion to read,writefiles
#include <string>//function to handle character strings,lines,paras
#include <ctime>//for srand
#include <cstdlib> //for randomly generation of colours
#include <unistd.h>//for clear screen,slowing.
using namespace std;
void LETS_START_THE_GAME_OF_LIFE(); //defining first function
//cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
//function for colour of stars
//function changes the colour of the stars on the grid randomly,there are 5 colurs that can be used to fill in ubuntu.They have colours range from 31 to 35
int Selecting_Colour(int colour)
{
colour=rand()%(36-31)+31;
return colour;
}
//cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
//function displays the rules of game of life.
void RULES()
{
cout<<"1) Any live cell with less than two live neighbors bites the dust, as though by underpopulation"<<endl<<"2)Any live cell with a few live neighbors lives on to the people to come"<<endl<<"3)Any live cell with in excess of three live neighbors bites the dust, as though by overpopulation"<<endl<<"4)Any dead cell with precisely three live neighbors turns into a live cell, as though by propagation"<<endl;
cout<<endl;
cout<<"1)R to Return 2)E to Exit"<<endl;
char x;
cin>>x;
if (x=='r' || x=='R')
{
LETS_START_THE_GAME_OF_LIFE();//returning to game, so that game doesnot stop after entering rules
}
else if (x=='e' || x=='E')
{
cout<<"GAME ENDED"<<endl; //game endedd
}
}
// function 8 end
//cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
//function has the description of game of life.
void DESCRIPTION()
{
char x;
cout<<"The Game of Life, additionally referred to just as Life, is a phone machine conceived by the British mathematician John Horton Conway in 1970.[1] It is a zero-player game, implying that its advancement is dictated by its underlying state, requiring no additionally input. One connects with the Game of Life by making an underlying setup and seeing how it advances. It is Turing finished and can recreate a widespread constructor or some other Turing machine"<<endl;
cout<<"1)R to Return 2)E to Exit"<<endl;
cin>>x;
if (x=='r')
{
LETS_START_THE_GAME_OF_LIFE(); //returning to game, so that game doesnot stop after entering description
}
else if (x=='e' || x=='E')
{
cout<<"GAME ENDED"<<endl;//game endedd
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//function that creates the grid Two, then equalizing it with our grid 1, then it finally implements the rules of game of life mentioned above
int CELL_ALIVE_DEAD_MODE(int GridSize,int **GAMEOFLIFEGRID)
{
/*int **gridTwo=new int*[GridSize];//declaring 2darray dynamically for making grid no 1
for(int j=0;j<GridSize;j++)
{
gridTwo[j]=new int [GridSize];
}
for (int i=0;i<GridSize;i++) //initializing each element of grid 1 to zero
{
for (int j=0; j<GridSize;j++)
{
gridTwo[i][j]=0;
}
} */
cout<<"Coordinated are stored in output.txt"<<endl;
fstream nnfile;
nnfile.open("output.txt",ios::out);
{
int gridTwo[GridSize][GridSize];
for (int i=0;i<GridSize;i++) //initializing each element of grid 2 to zero
{
for (int j=0; j<GridSize;j++)
{
gridTwo[i][j]=0;
}
}
for (int i=0;i<GridSize;i++)
{ //equalizing the both grid here
for (int j=0;j<GridSize;j++)
{
gridTwo[i][j]=GAMEOFLIFEGRID[i][j];
}
}
int neighbour_cells=0;
for (int i=0;i<GridSize;i++)//loop for cheking neighbors on x the x coordinates
{
for (int j=0;j<GridSize;j++)//loop for cheking neighbors on y coordinates
{
if(gridTwo[i-1][j-1]==1)//cheking neighbors alive or dead.
{
neighbour_cells=neighbour_cells+1;//adding all neighbors together now
}
if(gridTwo[i-1][j]==1)////cheking neighbors alive or dead.
{
neighbour_cells=neighbour_cells+1;//adding all neighbors together now
}
if(gridTwo[i][j-1]==1)////cheking neighbors alive or dead.
{
neighbour_cells=neighbour_cells+1;//adding all neighbors together now
}
if(gridTwo[i+1][j+1]==1)////cheking neighbors alive or dead.
{
neighbour_cells=neighbour_cells+1;//adding all neighbors together now
}
if(gridTwo[i][j+1]==1)//cheking neighbors alive or dead.
{
neighbour_cells=neighbour_cells+1;//adding all neighbors together now
}
if(gridTwo[i+1][j]==1)//cheking neighbors alive or dead.
{
neighbour_cells=neighbour_cells+1;//adding all neighbors together now
}
if(gridTwo[i-1][j+1]==1)//cheking neighbors alive or dead.
{
neighbour_cells=neighbour_cells+1;//adding all neighborstogether now
}
if(gridTwo[i+1][j-1]==1)//cheking neighbors alive or dead.
{
neighbour_cells=neighbour_cells+1;//adding all neighbors together now
}
if(neighbour_cells>3 || neighbour_cells<2)
{ //cheking neighbors alive or dead.
GAMEOFLIFEGRID[i][j]=0;
}//equalizing grid to 0, if condition statisfies
else if( neighbour_cells==3)//cheking neighbors alive or dead.
{
GAMEOFLIFEGRID[i][j]=1;//equlaizing it to 1
}
neighbour_cells=0;//declaring neighbors cells to zero now;
}
nnfile<<**gridTwo<<endl;//for writing end coordinated on the files
}
}
nnfile.close();//closing output.txt
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//the displaying function. recieves a pointer to the grid and grid size, where the value comes out to be true.the star is displayed, if false, then a space,moreover, int colour catches the colour we have to display for the star
int Display_GAMEOFLIFEGRID (int **GAMEOFLIFEGRID,int GridSize)
{
for (int i=0;i<GridSize;i++)
{
for (int j=0; j<GridSize;j++) //displaying grid 1
{
int colour=Selecting_Colour(colour);//the colour function implemetation
if (GAMEOFLIFEGRID[i][j]==1)//where the coordinates on the grid are
{
cout<<"\033[1;"<<colour<<"m*\033[0m"; //line for coloured text in linux.
//cout<<"*"; //when found printing a star else space
}
else
{
cout<<"\033[1;"<<colour<<"m.\033[0m";
//cout<<" ";//printing spaces on grid means nth is printed on grid
}
}
cout<<endl;
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//function for reading from the file ans toring those values to grid
//only plays when we enter the filemode
int **ReadFromFile_and_storingvalues (string filename,int **GAMEOFLIFEGRID)//use of pointer for paasing grid 1 to function
{
int movement=0,variable=0; //initializing variables to be passed in file
int GridCoordinateX=0, GridCoordinateY=0; //coordinates for grid 1
//cout<<"hello1"<<endl;
fstream file;//file declaratiom
file.open(filename.c_str());//opening file
if (!file)//incase user enters wrong file
{
cout<<"FILE OPENING FAILED"<<endl;
}
else//incase of right option
{
file>>movement;//passing variables to files
file>>variable;
for (int i=0;i<variable;i++)
{
file>>GridCoordinateX;
file>>GridCoordinateY; //filling coordinates with values of file
GAMEOFLIFEGRID[GridCoordinateX][GridCoordinateY]=1;
}
file.close();//closing file
return GAMEOFLIFEGRID;//returning grid 1 to main function
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//a function containg the functions display,working, slows our outout clears screem
void MAIN_IMPLEMENTATION(int GridSize,int **GAMEOFLIFEGRID)
{
Display_GAMEOFLIFEGRID(GAMEOFLIFEGRID,GridSize);//function for displaying grid
CELL_ALIVE_DEAD_MODE(GridSize,GAMEOFLIFEGRID);//function implementing rules of game of life
unsigned int speedforsleep=100000;
usleep(speedforsleep);
std::system("clear");
cout<<"\033[2J\033[1;1H"; //used in ubuntu for lsowing clearing
}
//cout<<">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"<<endl;
//taking file name form user
string taking_in_file_name_from_user()
{//function 2 start
string filename;//string for storing filenames
cout<<"Choose files, from the following, for the Gridsetup"<<endl;
cout<<endl; // 7 files created, having coordinates wriiten in them
cout<<"* diehard.txt"<<endl;
cout<<endl;
cout<<"* test2.txt"<<endl;
cout<<endl;
cout<<"* toad.txt"<<endl;
cout<<endl;
cout<<"* aim.txt"<<endl;
cout<<endl;
cout<<"* beehive.txt"<<endl;
cout<<endl;
cout<<"* experiment.txt"<<endl;
cout<<endl;
cout<<"* test1.txt"<<endl;
cout<<endl;
cout<<"PLEASE ENTER : ";
cout<<endl;
cin>>filename;//taking input
return filename; //returns filename to the starting game function
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//function that takes coordinaes from the user
int taking_input_from_user(int takingincells,int xcoordinate,int ycoordinate,int **GAMEOFLIFEGRID,int GridSize)
{
fstream filewrite;//for writing file
filewrite.open("myown.txt",ios::out|ios::in);//file write name
cout<<"Enter the total number of the cells you want to write on file(myown.txt)"<<endl;//Taking input
cin>>takingincells;
for(int i=0;i<takingincells;i++)
{
cout<<"Enter the x coordinate for cell "<<i+1<<" : ";
cin>>xcoordinate; //taking x coordinate on the grid
filewrite<<xcoordinate<<" "; //writing x coordinates on file
cout<<"Enter the y coordinate for cell "<<i+1<<" : ";
cin>>ycoordinate;//taking y coordinate on grid
filewrite<<ycoordinate;//writing y coordinates on file
filewrite<<endl;
while (xcoordinate>GridSize || ycoordinate>GridSize)//if our coordinated are larger in value than our size 20 then user will agin enetr the coordinates
{
cout<<"WRONG ENTERY"<<endl;//indicating the wrong entery
cout<<"Enter the x,y coordinates of the cell again "<<i+1<<" : ";
cin>>xcoordinate>>ycoordinate; //taking x y coordinates gain
filewrite<<xcoordinate<<" "<<ycoordinate; //writing coordinates on file
GAMEOFLIFEGRID[xcoordinate][ycoordinate]=1; //initilaing it on the grid
}
GAMEOFLIFEGRID[xcoordinate][ycoordinate]=1;//where the coordinated match on grid, a star will be printed in display functiom
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
char function_for_other_choices(char choice) //taking char from starting the game function
{//if b pressed
if (choice == 'b' || choice == 'B')
{
DESCRIPTION(); //description function
}//if c pressed
if (choice == 'c' || choice == 'C')
{
RULES();//rules function function
} //if d pressed
if (choice == 'd' || choice == 'D')
{
cout<<endl;
cout<<"GAME ENDED"<<endl; //ends the game
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//starting the gme function
void LETS_START_THE_GAME_OF_LIFE()
{
cout<<" WELCOME TO THE GAME OF LIFE"<<endl;
cout<<endl;//optionf for proceeding in game
cout<<"A)START"<<endl; //option A will start the game
cout<<endl;
cout<<"B)DESCRIPTION"<<endl; //option B will show u description
cout<<endl;
cout<<"C)RULES "<<endl; //option C will take u to rules
cout<<endl;
cout<<"D)EXIT"<<endl; //option D will stop your game
char choice,choice1; //declaring char variables
int choice2;int x; //declarinng int variables
cout<<endl;
cout<<"Enter : "; //taking very first input from user
cin>>choice; //cin your choice
// if A is pressed
if (choice == 'a' || choice == 'A')
{
int takingincells,xcoordinate,ycoordinate;//declarinng int variables
const int GridSize=20;//a constant size for the two grids
int **GAMEOFLIFEGRID=new int*[GridSize];//declaring 2darray dynamically for making grid no 1
for(int j=0;j<GridSize;j++)
{
GAMEOFLIFEGRID[j]=new int [GridSize];
}
for (int i=0;i<GridSize;i++) //initializing each element of grid 1 to zero
{
for (int j=0; j<GridSize;j++)
{
GAMEOFLIFEGRID[i][j]=0;
}
}
cout<<endl;//two ways for starting game
cout<<"1)START GAME BY FILE READING"<<endl; //select your file, has already wriiten coordinates
cout<<"2)START GAME BY ENTERING YOUR OWN COORDINATES"<<endl; //user can also start by entering his own coordinates
cout<<endl;
cout<<"Enter integer : ";
cin>>choice2;//taking second input
if (choice2==1) //by files
{
string filename=taking_in_file_name_from_user();//function for taking in the filename from user
ReadFromFile_and_storingvalues(filename,GAMEOFLIFEGRID);//function to store values from the file to the grid
cout<<endl;
cout<<endl;
cout<<" GRID IS SET! "<<endl;
Display_GAMEOFLIFEGRID(GAMEOFLIFEGRID,GridSize); //grid display
cout<<" PRESS S TO START "<<endl; //pressing button S will start the game
cin>>choice1;
if (choice1 == 's' || 'S' )
{
while(1)
{
MAIN_IMPLEMENTATION(GridSize,GAMEOFLIFEGRID); //function consisting of all the major functions, implemetation of game of life
}
}
else
{
cout<<"GAME ENDED"<<endl; //if s not entered game will end
}
}
else if (choice2==2) //by user
{
taking_input_from_user(takingincells,xcoordinate,ycoordinate,GAMEOFLIFEGRID,GridSize);//taking coordinates
cout<<" GRID IS SET! "<<endl;
Display_GAMEOFLIFEGRID(GAMEOFLIFEGRID,GridSize);//displaying grid
cout<<" PRESS S TO START "<<endl;
cin>>choice1;
if (choice1 == 's' || 'S' )//pressing button S will start the game
{
while(1)
{
MAIN_IMPLEMENTATION(GridSize,GAMEOFLIFEGRID); //function consisting of all the major functions, implemetation of game of life
}
}
else
{
cout<<"GAME ENDED"<<endl;//if s not entered game will end
}
}
delete []GAMEOFLIFEGRID;//deleting gameoflifegrid
} //choice a if end
//function taking us to other choices,if a not enterd
else
{
function_for_other_choices(choice);
}
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
int main ()//main function
{
LETS_START_THE_GAME_OF_LIFE(); //our main function consisting of only one function.
return 0;
}