-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_line.c
90 lines (84 loc) · 2.61 KB
/
cmd_line.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
#include <stdio.h>
#include <stdbool.h>
#define ALIVE 1
#define DEAD 0
#define RESOLUTION_X 40
#define RESOLUTION_Y 40
bool game_board[RESOLUTION_X][RESOLUTION_Y];
void pulsar(int centre_x, int centre_y){
// if (!check_bounds(centre_x+6, centre_y+6) ||
// !check_bounds(centre_x-6, centre_y-6)) return;
for (int d = 2; d <=4; d++)
{
game_board[centre_x+d][centre_y+6] = ALIVE;
game_board[centre_x+d][centre_y+1] = ALIVE;
game_board[centre_x+d][centre_y-6] = ALIVE;
game_board[centre_x+d][centre_y-1] = ALIVE;
game_board[centre_x-d][centre_y+6] = ALIVE;
game_board[centre_x-d][centre_y+1] = ALIVE;
game_board[centre_x-d][centre_y-6] = ALIVE;
game_board[centre_x-d][centre_y-1] = ALIVE;
game_board[centre_x+6][centre_y+d] = ALIVE;
game_board[centre_x+1][centre_y+d] = ALIVE;
game_board[centre_x-6][centre_y+d] = ALIVE;
game_board[centre_x-1][centre_y+d] = ALIVE;
game_board[centre_x+6][centre_y-d] = ALIVE;
game_board[centre_x+1][centre_y-d] = ALIVE;
game_board[centre_x-6][centre_y-d] = ALIVE;
game_board[centre_x-1][centre_y-d] = ALIVE;
}
}
void update_board_state(){
bool new_board[RESOLUTION_X][RESOLUTION_Y];
int total = 0,n = RESOLUTION_X,m = RESOLUTION_Y;
for(int i=0;i < RESOLUTION_X;i++){
for(int j=0;j < RESOLUTION_Y;j++){
total = (game_board[(i-1)%n][(j-1)%m]+game_board[(i-1)%n][j]
+game_board[(i-1)%n][(j+1)%m]+game_board[i][(j-1)%m]
+game_board[i][(j+1)%m]+game_board[(i+1)%n][(j-1)%m]
+game_board[(i+1)%n][j]+game_board[(i+1)%n][(j+1)%m])/ALIVE;
//printf("%d %d:%d\n",i,j,total);
if(game_board[i][j]==ALIVE){
// cell dies if it has insufficient neighbours
if(total < 2 || total > 3){
new_board[i][j] = DEAD;
}
else{
new_board[i][j] = ALIVE;
}
}else{
// cell lives if it has 3 neighbours
if(total==3){
new_board[i][j] = ALIVE;
}
else{
new_board[i][j] = DEAD;
}
}
}
}
for(int i=0;i < RESOLUTION_X;i++){
for(int j=0;j < RESOLUTION_Y;j++){
game_board[i][j] = new_board[i][j];
}
}
}
int main(){
pulsar(RESOLUTION_X/2,RESOLUTION_Y/2);
game_board[12][12] = ALIVE;
game_board[12][13] = ALIVE;
game_board[12][14] = ALIVE;
//write(STDOUT_FILENO, CLEAR_SCREEN_ANSI, 12);
while(true){
for (int i = 0; i < RESOLUTION_X; i++)
{
for (int j = 0; j < RESOLUTION_Y; j++){
printf((game_board[i][j]==ALIVE)?"#":".");
}
printf("\n");
}
getchar();
update_board_state();
}
return 0;
}