aboutsummaryrefslogtreecommitdiff
path: root/src/4-1-maze.c
blob: c48c31ee0d83663a2eec7205d0f84d00135eb4b3 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
 * 
 */