From 54d07a5ac4d7f08197ce4ce622e99dcb047e6e30 Mon Sep 17 00:00:00 2001 From: iceyrazor Date: Thu, 23 Jul 2026 17:58:50 -0500 Subject: maze --- src/4-1-maze.c | 255 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 src/4-1-maze.c (limited to 'src') diff --git a/src/4-1-maze.c b/src/4-1-maze.c new file mode 100644 index 0000000..c48c31e --- /dev/null +++ b/src/4-1-maze.c @@ -0,0 +1,255 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#define PROJECT_NAME "algorithms-maze 1" + +#define grid_cols 10 +#define grid_rows grid_cols + +#define obj_n grid_cols* grid_rows + +#define frame_delay 10 + +typedef struct { + int i; + int j; + //top, right, bottom, left + bool walls[4]; + bool visited; +} CELL; + +typedef struct OBJECT { + CELL items[obj_n]; + CELL* current; + CELL* stack[obj_n]; + int w; + int h; +} OBJECT; + +#include + +int get_index(int i, int j){ + if (i < 0 || j < 0 || i > grid_cols-1 || j > grid_rows-1) + return -1; + return (j * grid_cols + i); +} + +void cell_constructor(CELL* cell,int i,int j){ + cell->i = i; + cell->j = j; + for (int i=0; i<4; i++){ + cell->walls[i]=true; + } + cell->visited=false; +} + +void push_cell(CELL* arr[], CELL* cell, int count){ + for (int i=0; ii,cell->j-1); + if(index != -1) + top = &stuff->obj.items[index]; + index = get_index(cell->i+1,cell->j); + if(index != -1) + right = &stuff->obj.items[index]; + index = get_index(cell->i,cell->j+1); + if(index != -1) + bottom = &stuff->obj.items[index]; + index = get_index(cell->i-1,cell->j); + if(index != -1) + left = &stuff->obj.items[index]; + + if (top && !top->visited) { + push_cell(neighbors,top,4); + } + if (right && !right->visited) { + push_cell(neighbors,right,4); + } + if (bottom && !bottom->visited) { + push_cell(neighbors,bottom,4); + } + if (left && !left->visited) { + push_cell(neighbors,left,4); + } + + int neighbors_len=0; + for (int i=0; i<4; i++){ + if(neighbors[neighbors_len]) { + neighbors_len++; + } else { + break; + } + } + + if(neighbors_len > 0) { + int r = (int)RandomFloat(0,neighbors_len); + return neighbors[r]; + } else { + return NULL; + } +} + +void cell_removeWalls(CELL* a, CELL* b){ + int x = a->i - b->i; + if (x == 1){ + a->walls[3] = false; + b->walls[1] = false; + } else if (x == -1){ + a->walls[1] = false; + b->walls[3] = false; + } + int y = a->j - b->j; + if (y == 1){ + a->walls[0] = false; + b->walls[2] = false; + } else if (y == -1){ + a->walls[2] = false; + b->walls[0] = false; + } +} + +void init_stuffs(int w, int h, STUFFS* stuff) +{ + stuff->obj.w = stuff->width / grid_cols; + stuff->obj.h = stuff->height / grid_rows; + + for (int j = 0; j < grid_rows; j++) { + for (int i = 0; i < grid_cols; i++) { + int index = j * grid_cols + i; + cell_constructor(&stuff->obj.items[index],i,j); + stuff->obj.stack[index] = NULL; + } + } + + stuff->obj.current = &stuff->obj.items[0]; +} + +void draw_cell(CELL* cell, STUFFS* stuff) +{ + int w = stuff->obj.w; + int x = cell->i * w; + int y = cell->j * w; + //SDL_RenderDrawRect(stuff->renderer, &(SDL_Rect) { x, y, w, w }); + SDL_SetRenderDrawColor(stuff->renderer, 255, 255, 255, 255); + if (cell->walls[0]==true) + SDL_RenderDrawLine(stuff->renderer,x,y,x+w,y); + if (cell->walls[1]==true) + SDL_RenderDrawLine(stuff->renderer,x+w,y,x+w,y+w); + if (cell->walls[2]==true) + SDL_RenderDrawLine(stuff->renderer,x+w,y+w,x,y+w); + if (cell->walls[3]==true) + SDL_RenderDrawLine(stuff->renderer,x,y+w,x,y); + + if (cell->visited==true){ + SDL_SetRenderDrawColor(stuff->renderer, 255, 0, 255, 100); + SDL_RenderFillRect(stuff->renderer, &(SDL_Rect) { x, y, w, w }); + } +} + +void draw_highlight(CELL* cell, STUFFS* stuff){ + int w = stuff->obj.w; + int x = cell->i * w; + int y = cell->j * w; + SDL_SetRenderDrawColor(stuff->renderer, 0, 0, 255, 100); + SDL_RenderFillRect(stuff->renderer, &(SDL_Rect) { x, y, w, w }); +} + +float dist(float x, float y, float x2, float y2) +{ + return sqrt((pow(x, 2) + pow(y, 2) + pow(x2, 2) + pow(y2, 2))); +} + +void draw(SDL_Window* window, STUFFS* stuff) +{ + static bool done = false; + if (done == false) { + SDL_SetRenderDrawColor(stuff->renderer, 20, 20, 20, 255); + // SDL_RenderClear(renderer); + SDL_RenderFillRect(stuff->renderer, &(SDL_Rect) { 0, 0, stuff->width, stuff->height }); + + for (int i=0; iobj.items[i],stuff); + } + + int stack_len=0; + for (int i=0; iobj.stack[stack_len]) { + stack_len++; + } else { + break; + } + } + + // STEP 1 + stuff->obj.current->visited=true; + draw_highlight(stuff->obj.current,stuff); + CELL* next = cell_checkNeighbors(stuff->obj.current, stuff); + if (next != NULL){ + next->visited=true; + // STEP 2 + push_cell(stuff->obj.stack,stuff->obj.current,obj_n); + + // STEP 3 + cell_removeWalls(stuff->obj.current, next); + + // STEP 4 + stuff->obj.current = next; + } else if (stack_len > 0) { + stuff->obj.current = pop_cell(stuff->obj.stack,obj_n); + } + + SDL_RenderPresent(stuff->renderer); + } +} + +void on_end(STUFFS* stuff) +{ +} + +#include + +/* Challanges + * + * highlighting the cells in the stack a different color + * + */ -- cgit v1.3