-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.h
132 lines (109 loc) · 2.73 KB
/
life.h
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
//Life library: contains basic life/stabilife tools
#include <stdlib.h>
void drawFrame(void);
int liveNeighbors(int x, int y, int gridSize); //Uses global temp grid
int **tempGrid;
long int simStableLife(int gridSize, int setMemory[], int setNum, int chunkSize)
{
int i, j;
tempGrid=malloc(gridSize*sizeof(int*));
//There should be null-pointer checking here, but I'll let the program crash instead-I don't feel like writing tons of for loops right now.
for(i=0; i<gridSize; i++)
{
tempGrid[i]=malloc(gridSize*sizeof(int));
//More error checking, again, skipped for the time being.
}
for(i=0; i<gridSize; i++) for(j=0; j<gridSize; j++) tempGrid[i][j]=setMemory[chunkSize*chunkSize*4*setNum+chunkSize*2*i+j]; //Copy grid into buffer (setMemory weird addressing copied from main.c)
int action=0;
int gen=1;
for(;;) //Mainloop
{
//drawFrame();
if(action>0) action=0;
for(i=0; i<gridSize; i++) for(j=0; j<gridSize; j++)
{
switch(liveNeighbors(i, j, gridSize))
{
case 0:
case 1:
case 4:
case 5:
case 6:
case 7:
case 8:
if(tempGrid[i][j]==1)
{
tempGrid[i][j]=0;
action++;
}
break;
case 3:
if(tempGrid[i][j]==0)
{
tempGrid[i][j]=1;
action++;
}
break;
}
}
gen++;
if(gen>=999999) return 999999;
//Since 999999 generations have passed and there is no stable pattern,
//we cap the simulation at 999999 generations to save CPU time and prevent
//patterns like a Gosper glider gun from hanging the simulation.
if (action==0) return gen;
}
}
int liveNeighbors(int x, int y, int gridSize)
{
int count=0;
int a, b;
a=x-1;
b=y-1;
if(a<0 || b<0 || a> gridSize|| b>gridSize) ;
else if(tempGrid[a][b]==1) count++;
a=x;
b=y-1;
if(a<0 || b<0 || a>gridSize || b>gridSize) ;
else if(tempGrid[a][b]==1) count++;
a=x+1;
b=y-1;
if(a<0 || b<0 || a>gridSize || b>gridSize) ;
else if(tempGrid[a][b]==1) count++;
a=x-1;
b=y;
if(a<0 || b<0 || a>gridSize || b>gridSize) ;
else if(tempGrid[a][b]==1) count++;
a=x+1;
b=y;
if(a<0 || b<0 || a>gridSize || b>gridSize) ;
else if(tempGrid[a][b]==1) count++;
a=x-1;
b=y+1;
if(a<0 || b<0 || a>gridSize || b>gridSize) ;
else if(tempGrid[a][b]==1) count++;
a=x;
b=y+1;
if(a<0 || b<0 || a>gridSize || b>gridSize) ;
else if(tempGrid[a][b]==1) count++;
a=x+1;
b=y+1;
if(a<0 || b<0 || a>gridSize || b>gridSize) ;
else if(tempGrid[a][b]==1) count++;
return count;
}
/*void drawFrame(void) //Never called, here for reference
{
system("cls");
int i, j;
for(i=0; i<GRIDSIZE; i++)
{
for(j=0; j<GRIDSIZE; j++)
{
if(grid[i][j]==1) printf("*");
else printf(" ");
}
printf("\n");
}
printf("\nBirths: %d\nDeaths: %d\nGeneration: %d", births, deaths, gen);
}*/