-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
624 lines (521 loc) · 16.1 KB
/
game.c
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#include "game.h"
#include <SDL2/SDL_ttf.h>
#define terminate 0
#define still 1
//function to create the world
//return the world pointer if the world created correctly
int **create(){
int x;
int **cells;
int a=0;
while(a==0){
printf("Please choose the way of initialization\n1)input\n2)click\nChoice:");
x=optionChoice();
if(x==1){
getsize();
cells = input_create();
a=1;
}
else if(x==2){
getsize();
cells=click_create();
a=1;
}
else{
printf("Please input 1 or 2\n");
}
}
return cells;
}
//game evolve to the terminal
//return 1 if the world is not end when exit
//return 0 when the world is end
int run_terminal(int**cells){
int button=0;
int q=0;
int d=-100;
int way=0;
int **last=NULL;
bool quit=true;
int**my=NULL;
int x=0;
int swidth=30;
int sheight=30;
if(High>20||Width>20){
swidth=10;
sheight=10;
}
if(High<10&&Width<10){
swidth=50;
sheight=50;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { return -1; }
TTF_Init();
int phigh=0;
if(Width>=40&&High>=40){
phigh=50;
}
// 创建窗口
SDL_Window *sdlWindow = SDL_CreateWindow("Game of life", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width*swidth,High*sheight+phigh, SDL_WINDOW_SHOWN);
if (!sdlWindow) { return -1; }
// 创建渲染器
SDL_Renderer *sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0);
if (!sdlRenderer) { return -1; }
// 创建纹理
SDL_Texture *sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, Width*swidth, High*sheight+phigh);
if (!sdlTexture) { return -1; }
printf("press enter to start or stop\n");
TTF_Font *ttffont=TTF_OpenFont("lazy.ttf",20);
SDL_Surface * sdlsurface=NULL;
SDL_Texture * texture=NULL;
SDL_Rect dstrect;
SDL_Color color={52,203,120,255};/*字体颜色RGBA*/
char *word=NULL;
if(High>=40&&Width>=40){
way=1;
ttffont=TTF_OpenFont("lazy.ttf",50);
}
SDL_Event event;
while(quit){
while (SDL_PollEvent(&event)){
switch (event.type) {
case SDL_QUIT:{
quit=false;
break;
}
case SDL_KEYDOWN:{
switch(event.key.keysym.sym)
{
case SDLK_UP:{
if(delaytime==10){
printf("\nFastest speed already\n");
}
else{
printf("\nspeed up\n");
}
if(delaytime<=10){
delaytime = 10;
}
else{
delaytime = delaytime-10;
}
break;
}
case SDLK_DOWN:{
if(delaytime==500){
printf("\nLowest speed already\n");
}
else{
printf("\nspeed down\n");
}
if(delaytime>=500){
delaytime = 500;
}
else{
delaytime = delaytime+10;
}
break;
}
case SDLK_RETURN:{
if(button==0){
button=1;
}
else{button=0;}
break;
}
case SDLK_SPACE:{
quit=false;
break;
}
}
}
}
}
if(last!=NULL){
d=judge(cells,last);
}
if(d==0&&x==0){
x=1;
printf("It is terminate\n");
printf("Close the window to continue\n");
}
if(q==0&&button==1&&d!=0){
printf("The origin world:\n");
}
if(q!=0&&d!=0&&button==1){
printf("step %d:\n",q);
}
if(way==0){
show(sdlRenderer,sdlWindow,sdlTexture, cells);
}
else{
int number=q;
if(d==0||button==0){
number=number-1;
if(number<0){
number=0;
}
}
char str[25];
char a[20]="Generations:";
sprintf(str, "%d" ,number);
word=strcat(a,str);
sdlsurface=TTF_RenderUTF8_Blended(ttffont,word,color);
memset(word, 0, 100*sizeof(char));
texture=SDL_CreateTextureFromSurface(sdlRenderer,sdlsurface);
dstrect.x=Width*swidth/2-sdlsurface->w/2;/*显示的起始位置*/
dstrect.y=0;/*显示的起始位置*/
dstrect.w=sdlsurface->w;/*显示的宽度*/
dstrect.h=sdlsurface->h;/*显示的高度*/
showlabel(sdlRenderer,sdlWindow, sdlTexture,cells,sdlsurface,texture,dstrect);
}
SDL_Delay(delaytime);
if(d!=0&&button==1){
free(last);
last=copy(cells);
updateWithoutInput(cells);
printf("\n");
q+=1;
}
}
last=copy(cells);
my=copy(cells);
updateWithoutInput(my);
d=judge(my,last);
if(d==0){
freecell(my);
freecell(last);
close(sdlWindow,sdlTexture,sdlRenderer);
return terminate;
}
else{
freecell(my);
freecell(last);
close(sdlWindow,sdlTexture,sdlRenderer);
return still;
}
}
//input world to evolve for input steps
//return 1 if the world is not end when exit
//return 0 when the world is end
int run_step(int **cells,int y)
{
int button=0;
int mm=0;
int x=0;
int d=-100;
int **last=NULL;
int way=0;
int **my=NULL;
bool quit=true;
int swidth=30;
int sheight=30;
if(High>20||Width>20){
swidth=10;
sheight=10;
}
if(High<10&&Width<10){
swidth=50;
sheight=50;
}
if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { return -1; }
TTF_Init();
int phigh=0;
if(Width>=40&&High>=40){
phigh=50;
}
// 创建窗口
SDL_Window *sdlWindow = SDL_CreateWindow("Game of life", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Width*swidth,High* sheight+phigh, SDL_WINDOW_SHOWN);
if (!sdlWindow) { return -1; }
// 创建渲染器
SDL_Renderer *sdlRenderer=SDL_CreateRenderer(sdlWindow,-1,SDL_RENDERER_ACCELERATED);
if (!sdlRenderer) { return -1; }
// 创建纹理
SDL_Texture *sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, Width*swidth,High* sheight+phigh);
if (!sdlTexture) { return -1; }
SDL_SetRenderDrawColor(sdlRenderer, 255, 255, 255, 255);
/*清空渲染器*/
SDL_RenderClear(sdlRenderer);
TTF_Font *ttffont=TTF_OpenFont("lazy.ttf",20);
SDL_Surface * sdlsurface=NULL;
SDL_Texture * texture=NULL;
SDL_Rect dstrect;
SDL_Color color={52,203,120,255};/*字体颜色RGBA*/
char *word=NULL;
if(High>=40&&Width>=40){
way=1;
ttffont=TTF_OpenFont("lazy.ttf",50);
}
SDL_Event event;
printf("press enter to start or stop\n");
//certain step
int z=0;
while(quit){
while (SDL_PollEvent(&event)){
switch (event.type) {
case SDL_QUIT:{
quit=false;
break;
}
case SDL_KEYDOWN:{
switch(event.key.keysym.sym)
{
case SDLK_UP:{
if(delaytime==10){
printf("\nFastest speed already\n");
}
else{
printf("\nspeed up\n");
}
if(delaytime<=10){
delaytime = 10;
}
else{
delaytime = delaytime-10;
}
break;
}
case SDLK_DOWN:{
if(delaytime==500){
printf("\nLowest speed already\n");
}
else{
printf("\nspeed down\n");
}
if(delaytime>=500){
delaytime = 500;
}
else{
delaytime = delaytime+10;
}
break;
}
case SDLK_RETURN:{
if(button==0){
button=1;
}
else{button=0;}
break;
}
case SDLK_SPACE:{
quit=false;
break;
}
}
}
}
}
if(last!=NULL){
d=judge(cells,last);
}
if(d==0&&x==0){
x=1;
printf("It is terminate\n");
printf("Close the window to continue\n");
}
if(d!=0&&z<=y&&button==1){
if(z==0){
printf("The origin world:\n");
}
else{
printf("step %d:\n",z);
}
}
if(way==0){
show(sdlRenderer,sdlWindow,sdlTexture, cells);
}
else{
int number=z;
if(z>y){
number=y;
}
if(d==0||button==0){
number=number-1;
if(number<0){
number=0;
}
}
char str[25];
char a[20]="Generations:";
sprintf(str, "%d" ,number);
word=strcat(a,str);
sdlsurface=TTF_RenderUTF8_Blended(ttffont,word,color);
memset(word, 0, 100*sizeof(char));
texture=SDL_CreateTextureFromSurface(sdlRenderer,sdlsurface);
dstrect.x=Width*swidth/2-sdlsurface->w/2;/*显示的起始位置*/
dstrect.y=0;/*显示的起始位置*/
dstrect.w=sdlsurface->w;/*显示的宽度*/
dstrect.h=sdlsurface->h;/*显示的高度*/
showlabel(sdlRenderer,sdlWindow, sdlTexture,cells,sdlsurface,texture,dstrect);
}
SDL_Delay(delaytime);
if(d!=0&&z<=y&&button==1){
free(last);
last=copy(cells);
updateWithoutInput(cells);
z++;
}
if(d!=0&&z>y&&mm==0){
mm=1;
printf("The step is up\n");
printf("Please close the window to continue\n");
}
}
last=copy(cells);
my=copy(cells);
updateWithoutInput(my);
d=judge(my,last);
if(d==0){
freecell(my);
freecell(last);
close(sdlWindow,sdlTexture,sdlRenderer);
return terminate;
}
else{
freecell(my);
freecell(last);
close(sdlWindow,sdlTexture,sdlRenderer);
return still;
}
}
//function of whole process of a world's evolution
//return 1 if the world is not end when exit
//return 0 when the world is end
int game(int ** cell){
int result;
int x;
int a=0;
int b=0;
while(a==0){
printf("Please choose the mode\n");
printf("1-Set certain number of step\n");
printf("2-Keep envolving\n");
printf("Choice:");
x=optionChoice();
if(x==-1){
printf("\nYou should input a digit \n");
continue;
}
else if(x!=1&&x!=2){
printf("\nYou should input a 1 or 2 \n");
continue;
}
else{
a=1;
}
}
if(x==2){
result= run_terminal(cell);
if(result==terminate){
return terminate;
}
else{
return still;
}
}
else{
while(b==0){
printf("Please input the number of evolution:");
x=optionChoice();
if(x==-1){
printf("\nYou should input an integer larger than 0 \n\n");
continue;
}
else if(x<=0){
printf("\nThe number should over 0 \n\n");
continue;
}
else{
b=1;
}
}
result=run_step(cell,x);
if(result==terminate){
return terminate;
}
else{
return still;
}
}
return 1;
}
//The whole game system
void whole_game(char * filename){
FILE *file=fopen(filename,"r+");
if (file==NULL){
printf("File not found");
return;
}
int **cell=load(file);
fclose(file);
FILE *close=fopen("my.txt","w");
int result;
int x;
int a;
while(1){
a=0;
result=game(cell);
if(result==terminate){
while(a==0){
printf("This world is terminated\n");
printf("Do you want to play a new game?(0 for quit and 1 for yes)\n");
printf("Choice:");
x=optionChoice();
if(x==-1){
printf("\nplease input 0 or 1\n\n");
continue;
}
else if(x!=0&&x!=1){
printf("\nplease input 0 or 1\n\n");
continue;
}
else{
a=1;
}
}
if(x==0){
printf("\nGame closed,Bye Bye\n");
store(close,cell);
freecell(cell);
fclose(close);
return;
}
if(x==1){
free(cell);
cell = create();
continue;
}
}
if(result==still){
while(a==0){
printf("This world is not terminated\n");
printf("Please choose an option\n1)Quit the game\n2)Still use it for another game\n3)Create a new world\n");
printf("Choice:");
x=optionChoice();
if(x==-1){
printf("\n\nPlease input 1 or 2 or 3\n\n");
continue;
}
else if(x==1){
printf("\nGame closed,Bye Bye\n");
store(close,cell);
freecell(cell);
fclose(close);
return;
}
else if(x==2){
a=1;
}
else if(x==3){
freecell(cell);
cell=create();
a=1;
}
else{
printf("\n\nPlease input 1 or 2 or 3\n\n");
continue;
}
}
}
}
}