#include <helianthus.h>
#define ROWS 12
#define COLS 18
int map[ROWS][COLS][3];
int visit(int r, int c) {
if (r < 0 || c < 0 || r >= ROWS || c >= COLS) return FALSE;
if (map[r][c][2]) return randomFloat()*100 < 2;
map[r][c][2] = TRUE;
int ways[4] = {0, 1, 2, 3};
for(int i = 0; i < 4; ++i) {
int a = randomNumber(0, 3);
int b = randomNumber(0, 3);
int n = ways[a];
ways[a] = ways[b];
ways[b] = n;
}
for(int i = 0; i < 4; ++i) {
switch(ways[i]) {
case 0:
if (visit(r, c-1)) map[r ][c ][0] = TRUE;
break;
case 1:
if (visit(r, c+1)) map[r ][c+1][0] = TRUE;
break;
case 2:
if (visit(r-1, c)) map[r ][c ][1] = TRUE;
break;
case 3:
if (visit(r+1, c)) map[r+1][c ][1] = TRUE;
break;
}
}
return TRUE;
}
void generate() {
for(int r = 0; r < ROWS; ++r)
for(int c = 0; c < COLS; ++c)
for(int i = 0; i < 3; ++i)
map[r][c][i] = 0;
visit(0, 0);
}
void init() {
generate();
}
void draw() {
double w = windowGetWidth();
double h = windowGetHeight();
if (keyWentDown("space")) generate();
saveState();
translate(w/2, h/2);
zoom(w/(COLS + 1));
translate(-COLS/2.0, -ROWS/2.0);
strokeWidth(0.1);
for(int r = 0; r < ROWS; ++r) {
for(int c = 0; c < COLS; ++c) {
if (!map[r][c][0]) line(c, r, c, r+1);
if (!map[r][c][1]) line(c, r, c+1, r);
}
}
line(COLS, 0, COLS, ROWS);
line(0, ROWS, COLS, ROWS);
restoreState();
}
int main() {
windowSetVariableFrameRate();
windowSetResizable(TRUE);
windowSetInit(&init);
windowSetDraw(&draw);
windowRun();
}