-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D_GoL.c
executable file
·105 lines (78 loc) · 1.44 KB
/
2D_GoL.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#define PLUS1 2 // Tuning Parameters
#define PLUS2 4
#define OUTPUT 219
void sleepx (int milliseconds) {
clock_t start, act;
int dif;
start = clock ();
while (1) {
act = clock ();
dif = act - start;
if (dif > milliseconds - 9 && dif < milliseconds + 9){
break;
}
else if (dif > 1000){
break;
}
}
}
int main () {
_textcolor (12);
srand (time (0));
int cell [2] [80];
int switcher = 0;
int line = 1;
int sum;
int i;
char input;
start:
_clrscr ();
switcher = 0;
line = 1;
for (i = 0; i < 80; ++i) {
cell [0] [i] = 0;
cell [1] [i] = 0;
}
_gotoxy (1, line);
for (i = 0; i < 80; ++i) {
cell [switcher] [i] = rand () % 2;
if (cell [switcher] [i] == 1) {
printf ("%c", OUTPUT);
}
else {
printf (" ");
}
}
while (1) {
sleepx (500);
switcher = !switcher;
++line;
_gotoxy (1, line);
for (i = 0; i < 80; ++i) {
sum = cell [!switcher] [(i-2) % 80] + cell [!switcher] [(i-1) % 80] + cell [!switcher] [(i+1) % 80] + cell [!switcher] [(i+2) % 80];
if (sum == PLUS1 || sum == PLUS2) {
cell [switcher] [i] = 1;
printf ("%c", OUTPUT);
}
else {
cell [switcher] [i] = 0;
printf (" ");
}
}
if (_kbhit()) {
input = _getch ();
if (input == ' ')
_getch ();
else if (input == 27)
break;
else if (input == '\r')
goto start;
}
}
_getch ();
return 0;
}