From 95d32b2c6193d73651a000d0b4ee1aef6a74ce0c Mon Sep 17 00:00:00 2001 From: david Date: Wed, 6 May 2026 05:25:06 -0300 Subject: i'll take my laptop to school i'll probably keep working on it there ignore the maze game it sucks --- games/gamemaze.c | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 4 deletions(-) (limited to 'games/gamemaze.c') diff --git a/games/gamemaze.c b/games/gamemaze.c index c47d2d5..621c8ff 100644 --- a/games/gamemaze.c +++ b/games/gamemaze.c @@ -3,7 +3,8 @@ #include #include #include -#include "../rat.h" +#include +#include "../rat.h" /* // i'm probably gonna use a loop erased random walk algorithm @@ -31,6 +32,7 @@ typedef struct{ }cursor; + static void stop() { clear(); refresh(); @@ -42,9 +44,87 @@ static void stop() { } //yes i stole the code from gamecatch shut up it works +static char **grid; //big ahh grid + +void lerw() { + int W = getmaxx(stdscr); + int H = getmaxy(stdscr); + + for (int y = 0; y < H; y++) + for (int x = 0; x < W; x++) + grid[y][x] = '#'; + + int cw = W / 2; + int ch = H / 2; + if (cw < 1 || ch < 1) return; + + int total = cw * ch; + char *vis = calloc(total, 1); + int *px = malloc(total * sizeof(int)); + int *py = malloc(total * sizeof(int)); + int *ip = malloc(total * sizeof(int)); + vis[0] = 1; + grid[1][1] = ' '; + int vc = 1; + + int ddx[] = { 1, -1, 0, 0 }; + int ddy[] = { 0, 0, 1, -1 }; + + while (vc < total) { + int sx, sy; + do { + sx = rand() % cw; + sy = rand() % ch; + } while (vis[sy * cw + sx]); + + memset(ip, -1, total * sizeof(int)); + px[0] = sx; py[0] = sy; + ip[sy * cw + sx] = 0; + int pl = 1; + int cx = sx, cy = sy; + + while (!vis[cy * cw + cx]) { + int dir = rand() % 4; + int nx = cx + ddx[dir]; + int ny = cy + ddy[dir]; + if (nx < 0 || nx >= cw || ny < 0 || ny >= ch) continue; + + int ni = ny * cw + nx; + if (ip[ni] >= 0) { + //loop: erase back to ip[ni] heh ni hehjehehe + int idx = ip[ni]; + for (int i = idx + 1; i < pl; i++) + ip[py[i] * cw + px[i]] = -1; + pl = idx + 1; + cx = nx; cy = ny; + } else { + px[pl] = nx; py[pl] = ny; + ip[ni] = pl++; + cx = nx; cy = ny; + } + } + + for (int i = 0; i < pl; i++) { + grid[py[i]*2+1][px[i]*2+1] = ' '; + vis[py[i]*cw+px[i]] = 1; + } + vc += pl; + + for (int i = 0; i + 1 < pl; i++) + grid[py[i]+py[i+1]+1][px[i]+px[i+1]+1] = ' '; + + if (pl > 0) + grid[py[pl-1]+cy+1][px[pl-1]+cx+1] = ' '; + } + + //entrance top, exit bottom + if (H > 0) grid[0][1] = ' '; + if (H > 1 && W > 2) grid[H-1][W-2] = ' '; + + free(vis); free(px); free(py); free(ip); +} - - +//i have no fucking idea what i did here. it was revealed to me in a dream unironically void gamemaze(){ @@ -53,6 +133,15 @@ void gamemaze(){ if(initialized == 0){ initscr();}else{refresh();} ++initialized; + int W = getmaxx(stdscr); + int H = getmaxy(stdscr); + + grid = malloc(H * sizeof(char *)); //those who allocate memory + for (int y = 0; y < H; y++) + grid[y] = malloc(W * sizeof(char)); + + lerw(); + found = 0; noecho(); @@ -61,7 +150,7 @@ void gamemaze(){ keypad(stdscr, TRUE); curs_set(0); - cursor curs = {0, 0, {"@"}}; + cursor curs = {1, 1, {"@"}}; running = 1; @@ -83,11 +172,20 @@ void gamemaze(){ if (!running) break; clear(); + + for (int y = 0; y < H; y++) + for (int x = 0; x < W; x++) + if (grid[y][x] == '#') mvaddch(y, x, '#'); + mvaddstr(curs.posy, curs.posx, curs.face[0]); refresh(); usleep(50000); } + for (int y = 0; y < H; y++) + free(grid[y]); + free(grid); + stop(); } -- cgit v1.3-2-g0d8e