summaryrefslogtreecommitdiff
path: root/sources/main.c
blob: 7d19cf19f7949d45a6501ec2b9ae77b278528900 (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
110
111
#include "raylib.h"
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main(void)
{
    const int pixSize = 5;
    const int padding = 2;
    const int dim = 80;
    const int space = pixSize + (2 * padding);
    const int winSize = space * dim;

    bool (*currState)[dim];
    bool (*nextState)[dim];
    currState = malloc(dim * dim * sizeof(bool));
    nextState = malloc(dim * dim * sizeof(bool));

    srand(time(NULL));
    int prob = 30;
    for (int i=0; i<dim; i++) {
        for (int j=0; j<dim; j++) {
            currState[i][j] = false;
            nextState[i][j] = false;
            if (rand() % 100 < prob) {
                currState[i][j] = true;
                nextState[i][j] = true;
            } else {
                currState[i][j] = false;
                nextState[i][j] = false;
            }
        }
    }

    InitWindow(winSize, winSize, "Kontol");
    SetTargetFPS(10);

    bool isRunning = true;
    while (!WindowShouldClose())
    {
        BeginDrawing();

        if (IsKeyDown(KEY_A))
            isRunning = !isRunning;
        ClearBackground(BLACK);

        int nearestI = GetMouseX() / space;
        int nearestJ = GetMouseY() / space;

        if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
            currState[nearestI][nearestJ] = true;
        if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
            currState[nearestI][nearestJ] = false;

        for (int i=0; i<dim; i++) {
            for (int j=0; j<dim; j++) {
                Color pixColor = BLACK;
                if (currState[i][j]) pixColor = WHITE;
                DrawRectangle(i * space, j * space, pixSize, pixSize, pixColor);

                if (isRunning) {
                    int n1 = 0;
                    int n2 = 0;
                    int n3 = 0;
                    int n4 = 0;
                    int n5 = 0;
                    int n6 = 0;
                    int n7 = 0;
                    int n8 = 0;
                    if (i > 0 && j > 0) {
                        n1 = currState[i - 1][j - 1];
                        n2 = currState[i - 1][j];
                        n4 = currState[i][j - 1];
                    }
                    if (i > 0 && j < dim-1) {
                        n3 = currState[i - 1][j + 1];
                    }
                    if (i < dim-1 && j < dim-1) {
                        n5 = currState[i][j + 1];
                        n7 = currState[i + 1][j];
                    }
                    if (i < dim-1 && j > 0) {
                        n6 = currState[i + 1][j - 1];
                    }
                    if (i < dim-1 && j < dim-1) {
                        n8 = currState[i + 1][j + 1];
                    }

                    int total = n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8;
                    if (total < 2 && currState[i][j]) nextState[i][j] = false;
                    if (total > 3 && currState[i][j]) nextState[i][j] = false;
                    if ((total == 2 || total == 3) && currState[i][j]) nextState[i][j] = true;
                    if (total == 3 && !currState[i][j]) nextState[i][j] = true;
                }
            }
        }

        if (isRunning) {
            memcpy(currState, nextState, dim * dim * sizeof(bool));
        }

        EndDrawing();
    }

    free(currState);
    free(nextState);

    CloseWindow();

    return 0;
}