aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoriceyrazor <iceyrazor@mailfence.com>2026-07-23 17:58:50 -0500
committericeyrazor <iceyrazor@mailfence.com>2026-07-23 17:58:50 -0500
commit54d07a5ac4d7f08197ce4ce622e99dcb047e6e30 (patch)
tree14986a7e6adc9c73ace8d9c7d1a31473ec620064 /src
parent66b8519627d3ec3f43c041d7d7a9a86331593765 (diff)
Diffstat (limited to 'src')
-rw-r--r--src/4-1-maze.c255
1 files changed, 255 insertions, 0 deletions
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 <algolib.c>
+#include <math.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <vectorlib.c>
+
+#include <SDL2/SDL.h>
+#include <SDL2/SDL2_gfxPrimitives.h>
+#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_ttf.h>
+#include <SDL2/SDL_video.h>
+
+#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 <init.h>
+
+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; i<count; i++){
+ if(!arr[i]){
+ arr[i]=cell;
+ return;
+ }
+ }
+}
+
+CELL* pop_cell(CELL* arr[], int count){
+ CELL* ret_cell;
+ for (int i=0; i<count; i++){
+ if(!arr[i]){
+ ret_cell = arr[i-1];
+ arr[i-1]=NULL;
+ return ret_cell;
+ } else if (i == count){
+ ret_cell = arr[i];
+ arr[i]=NULL;
+ return ret_cell;
+ }
+ }
+ return NULL;
+}
+
+CELL* cell_checkNeighbors(CELL* cell, STUFFS* stuff){
+ CELL* neighbors[4];
+ for (int i=0; i<4; i++) { neighbors[i] = NULL; }
+
+ CELL *top = NULL;
+ CELL *right = NULL;
+ CELL *bottom = NULL;
+ CELL *left = NULL;
+
+ int index=get_index(cell->i,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; i<obj_n; i++){
+ draw_cell(&stuff->obj.items[i],stuff);
+ }
+
+ int stack_len=0;
+ for (int i=0; i<obj_n; i++){
+ if(stuff->obj.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 <init.c>
+
+/* Challanges
+ *
+ * highlighting the cells in the stack a different color
+ *
+ */