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 <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "set.h"
#ifndef PLATFORM_WEB
#include <raylib.h>
#else
#include RAYLIB_SRC
#endif
#define W 800
#define H 600
#define NODES_X 20
#define NODES_Y 15
#define NODE_L 40
set *nodes[NODES_X][NODES_Y];
bool walls_hori[NODES_X][NODES_Y-1] = {0}; // false - exists
bool walls_vert[NODES_X-1][NODES_Y] = {0}; // true - doesnt exist
void init_nodes()
{
for(int i = 0; i < NODES_Y; i++)
for(int j = 0; j < NODES_X; j++)
nodes[j][i] = set_create();
}
void deinit_nodes()
{
for(int i = 0; i < NODES_Y; i++)
for(int j = 0; j < NODES_X; j++)
set_free(nodes[j][i]);
}
void update()
{
// optimize picking random number thingy
if(rand() % 2) {
int x = rand() % NODES_X;
int y = rand() % (NODES_Y-1);
if(set_find(nodes[x][y]) != set_find(nodes[x][y+1])) {
walls_hori[x][y] = true;
set_union(nodes[x][y], nodes[x][y+1]);
}
} else {
int x = rand() % (NODES_X-1);
int y = rand() % NODES_Y;
if(set_find(nodes[x][y]) != set_find(nodes[x+1][y])) {
walls_vert[x][y] = true;
set_union(nodes[x][y], nodes[x+1][y]);
}
}
for(int i = 0; i < NODES_Y; i++) {
for(int j = 0; j < NODES_X; j++) {
if(i < NODES_Y-1) if(!walls_hori[j][i])
DrawLine((j+0)*NODE_L, (i+1)*NODE_L, (j+1)*NODE_L, (i+1)*NODE_L, RED);
if(j < NODES_X-1) if(!walls_vert[j][i])
DrawLine((j+1)*NODE_L, (i+0)*NODE_L, (j+1)*NODE_L, (i+1)*NODE_L, RED);
}
}
}
int main(void)
{
srand(time(NULL));
InitWindow(W, H, "Kruskal");
SetTargetFPS(60);
init_nodes();
while(!WindowShouldClose())
{
BeginDrawing();
ClearBackground(RAYWHITE);
DrawFPS(10, 10);
update();
EndDrawing();
}
deinit_nodes();
return 0;
}
|