summaryrefslogtreecommitdiff
path: root/main.c
blob: a81abe6b6f818698ac91e842b827dee385dc82e3 (plain)
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
#include <stdio.h>

#define WIDTH 213
#define HEIGHT 56

#define F_ARGS _Bool board[HEIGHT][WIDTH], int neighbours[HEIGHT][WIDTH], int y, int x

int get_neighbours(_Bool board[HEIGHT][WIDTH], int y, int x)
{
    int neighbours = 0;

    if(y > 0)
    {
        if(board[y - 1][x]) ++neighbours; // n

    	if((x + 1) < WIDTH && board[y - 1][x + 1]) ++neighbours; // ne
        if( x > 0          && board[y - 1][x - 1]) ++neighbours; // nw
    }

    if((x + 1) < WIDTH && board[y][x + 1]) ++neighbours; // e
    if( x > 0         && board[y][x - 1]) ++neighbours; // w

    if((y + 1) < HEIGHT)
    {
        if(board[y + 1][x]) ++neighbours; // s

        if((x + 1) < WIDTH && board[y + 1][x + 1]) ++neighbours; // se
        if( x > 0          && board[y + 1][x - 1]) ++neighbours; // sw
    }

    return neighbours;
}

void iter(void (*f)(F_ARGS), _Bool board[HEIGHT][WIDTH], int neighbours[HEIGHT][WIDTH])
{
    for(int y = 0; y < HEIGHT; ++y)
    {
        for(int x = 0; x < WIDTH; ++x)
        {
            (*f)(board, neighbours, y, x);
        }
    }
}

void print_board_pos(F_ARGS)
{
    board[y][x] ? putc('*', stdout) : putc('.', stdout);
    if(x + 1 == WIDTH)
        putc('\n', stdout);
}

void calculate_neighbours(F_ARGS)
{
    neighbours[y][x] = get_neighbours(board, 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;
    }
}

int handle_input(int input)
{
    switch(input)
    {
        case 'q':
            return 0;
        default:
            return 1;
    }
}

int main(void)
{
    _Bool board[HEIGHT][WIDTH] = { 0 };
    int neighbours[HEIGHT][WIDTH] = { 0 };

    board[0][1] = 1;
    board[1][2] = 1;
    board[2][0] = 1;
    board[2][1] = 1;
    board[2][2] = 1;

    iter(&print_board_pos, board, neighbours);
    
    int input = getchar();
    _Bool run = handle_input(input);

    while(run)
    {
        iter(&calculate_neighbours, board, neighbours);

        iter(&apply_rules, board, neighbours);

        iter(&print_board_pos, board, neighbours);

        input = getchar();
        run = handle_input(input);
    }

    return 0;
}