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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#define F_ARGS _Size* size, _Bool (*board)[size->width], [[maybe_unused]] char (*neighbours)[size->width], int y, int x
typedef struct {
int height;
int width;
} _Size;
void iter(void (*f)(F_ARGS), _Size *size, _Bool (*board)[size->width], char (*neighbours)[size->width])
{
for(int y = 0; y < size->height; ++y)
{
for(int x = 0; x < size->width; ++x)
{
(*f)(size, board, neighbours, y, x);
}
}
}
void get_neighbours(F_ARGS)
{
if(y > 0)
{
if(board[y - 1][x]) ++neighbours[y][x]; // n
if((x + 1) < size->width && board[y - 1][x + 1]) ++neighbours[y][x]; // ne
if( x > 0 && board[y - 1][x - 1]) ++neighbours[y][x]; // nw
}
if((x + 1) < size->width && board[y][x + 1]) ++neighbours[y][x]; // e
if( x > 0 && board[y][x - 1]) ++neighbours[y][x]; // w
if((y + 1) < size->height)
{
if(board[y + 1][x]) ++neighbours[y][x]; // s
if((x + 1) < size->width && board[y + 1][x + 1]) ++neighbours[y][x]; // se
if( x > 0 && board[y + 1][x - 1]) ++neighbours[y][x]; // sw
}
}
void print_board_pos(F_ARGS)
{
if(board[y][x])
mvaddch(y, x, '*');
}
void apply_rules(F_ARGS)
{
if(neighbours[y][x] < 2 || neighbours[y][x] > 3)
{
board[y][x] = 0;
}
else if(neighbours[y][x] == 3)
{
board[y][x] = 1;
}
}
_Bool handle_input(int input)
{
switch(input)
{
case 'q':
return 0;
case KEY_RESIZE:
return 0;
default:
return 1;
}
}
int main(void)
{
initscr();
cbreak();
noecho();
_Size size;
getmaxyx(stdscr, size.height, size.width);
_Bool (*board )[size.width] = malloc((size_t)size.height * (size_t)size.width * sizeof(_Bool));
char (*neighbours)[size.width] = malloc((size_t)size.height * (size_t)size.width * sizeof(char ));
memset(board , 0, (size_t)size.height * (size_t)size.width * sizeof(_Bool));
memset(neighbours, 0, (size_t)size.height * (size_t)size.width * sizeof(char ));
board[0][1] = 1;
board[1][2] = 1;
board[2][0] = 1;
board[2][1] = 1;
board[2][2] = 1;
iter(&print_board_pos, &size, board, neighbours);
int input = getch();
_Bool run = handle_input(input);
while(run)
{
iter(&get_neighbours, &size, board, neighbours);
iter(&apply_rules, &size, board, neighbours);
clear();
iter(&print_board_pos, &size, board, neighbours);
refresh();
input = getch();
run = handle_input(input);
memset(neighbours, 0, (size_t)size.height * (size_t)size.width * sizeof(char ));
}
free(board);
free(neighbours);
endwin();
return run;
}
|