-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.c
executable file
·147 lines (132 loc) · 2.95 KB
/
life.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
#include <GLUT/glut.h>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define RETURN 13
#define ESCAPE 27
#define SPACE 32
#define P 112
int window;
int size = 1000;
int current[1000][1000];
int next[1000][1000];
void initBoard(int *board, int n) {
int i, j;
for(i=0; i < n; i++)
for(j = 0; j < n; j++)
board[i*n + j] = 0;
}
void randomize(int *board, int n) {
int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
board[n*i + j] = random() % 2;
}
void getNext(int *board, int *next, int n) {
int i, j;
int count;
for(i = 1; i < n-1; i++) {
for(j = 1; j < n-1; j++) {
count = 0;
if(board[n*(i-1)+j])
count++;
if(board[n*(i-1)+(j-1)])
count++;
if(board[n*(i-1)+(j+1)])
count++;
if(board[n*i + (j-1)])
count++;
if(board[n*i + (j+1)])
count++;
if(board[n*(i+1) + (j-1)])
count++;
if(board[n*(i+1) + j])
count++;
if(board[n*(i+1) + (j+1)])
count++;
if(board[n*i + j]) {
if(count < 2 || count > 3)
next[n*i + j] = 0;
else
next[n*i + j] = 1;
} else {
if(count == 3)
next[n*i + j] = 1;
else
next[n*i + j] = 0;
}
}
}
memcpy(board, next, sizeof(int) * n * n);
}
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
}
void resizeGL(int w, int h) {
if(h == 0) { h=1; }
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)w/h, 0.1f, 200.0f);
glMatrixMode(GL_MODELVIEW);
}
void drawBoard() {
int i, j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-40.0, -40.0, -50.0);
glPushMatrix();
glPointSize(3.0f);
glBegin(GL_POINTS);
for(i=0; i < size; i++) {
for(j=0; j < size; j++) {
if(current[i][j]) {
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
else {
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
}
glVertex3f(i*(0.5f), j*(0.5f), -6.0f);
}
}
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void update() {
getNext(*current, *next, size);
drawBoard();
}
void keyPressed(unsigned char key, int x, int y) {
usleep(100);
if(key == ESCAPE) {
glutDestroyWindow(window);
exit(0);
}
if(key == SPACE)
randomize(*current, size);
if(key == RETURN)
getNext(*current, *next, size);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(800, 600);
window = glutCreateWindow("life");
initBoard(*current, size);
randomize(*current, size);
glutDisplayFunc(&drawBoard);
glutIdleFunc(&update);
glutReshapeFunc(&resizeGL);
glutKeyboardFunc(&keyPressed);
initGL();
glutMainLoop();
return 1;
}