This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.c
902 lines (784 loc) · 22.8 KB
/
maze.c
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/**
* @file maze.c
*
* @author Spac Petr <[email protected]>
* @date 2023-11
*
*/
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CELL_NEIGHBORS_COUNT 3 ///< Number of neighbours each cell has.
#define EMPTY_CELL 10 ///< This represents non existing cell
#define RIGHT_HAND 6
#define LEFT_HAND 7
#define LEFT_BORDER 0
#define RIGHT_BORDER 1
#define VERTICAL_BORDER 2
#define NDIR 3 ///< Number of possible directions from cell
/**
* @brief Map represents matrix
*
*/
typedef struct {
int rows;
int cols;
unsigned char *cells;
} Map;
/**
* @brief Coordinates for cell
*
*/
typedef struct {
int row;
int col;
} coordinates_t;
/**
* @brief Node represents an item in Queue
*
*/
struct node {
coordinates_t cell;
struct node *parent;
struct node *next;
};
/// Front of the Queue
struct node *front = NULL;
/// tail of the Queue
struct node *tail = NULL;
/**
* Array used in BFS
*
*/
typedef struct {
struct node *array;
size_t used; /// index of next free position
size_t size;
} Array;
void enqueue(coordinates_t cell, struct node *parent);
void dequeue(struct node *new_cell);
int new_border(int old_border);
int start_border(Map *map, int r, int c, int leftright);
int get_side_border(Map *maze, coordinates_t curr_cell, int border,
int leftright);
bool isborder(Map *map, int r, int c, int border);
bool is_valid_start(Map *maze, int r, int c);
bool valid_borders(unsigned char cell, unsigned char *neighbors);
bool is_lebeled(Map *maze, coordinates_t cell);
void set_label(Map *maze, coordinates_t cell);
bool is_goal(Map *maze, coordinates_t start, coordinates_t new_cell,
coordinates_t base_cell);
bool is_exit(Map *maze, coordinates_t cell);
bool is_in_maze(Map *maze, coordinates_t point);
int get_cell_index(Map *maze, int r, int c);
void next_cell(Map *maze, coordinates_t *curr_cell, int border);
void print_help();
unsigned char *maze_test(char *file_name);
int path_by_rule(coordinates_t start_cell, char *file_name, int leftright);
int bfs(coordinates_t start_cell, char *file_name);
Map *init_maze(FILE **fptr, Map *maze, char *file_name);
void free_maze(FILE **fptr, Map *maze);
Map *map_load(FILE **fptr, Map *maze);
// Array index represents current border, value for index represents right
// border from current border
const int rpath_even_dir[NDIR] = {2, 0, 1};
const int rpath_odd_dir[NDIR] = {1, 2, 0};
const int lpath_even_dir[NDIR] = {1, 2, 0};
const int lpath_odd_dir[NDIR] = {2, 0, 1};
/**
* Allocates memory for an array and initializes its metadata.
* @param arr Pointer to the Array structure to be initialized.
* @param init_size The initial number of elements.
* @return 0 on success, non-zero on allocation failure.
*/
int init_array(Array *arr, size_t init_size) {
arr->array = malloc(init_size * sizeof(struct node));
if (arr->array == NULL) {
return -1;
}
arr->used = 0;
arr->size = init_size;
return 0;
}
/**
* Insert item into array.
* @param arr Pointer to array
* @param init_size Initial size of the array
*/
int array_insert(Array *arr, struct node item) {
if (arr->used == arr->size) {
arr->size *= 2;
arr->array = realloc(arr->array, arr->size * sizeof(struct node));
if (arr->array == NULL) {
return -1;
}
}
// use arr->used, then increment
arr->array[arr->used++] = item;
return 0;
}
/**
* Free an array.
* @param arr Pointer to array
*/
void free_array(Array *arr) {
free(arr->array);
arr->array = NULL;
arr->used = arr->size = 0;
}
/**
* Add cell to queue.
* @param cell coordinations of cell
* @param parent parent cell
*/
void enqueue(coordinates_t cell, struct node *parent) {
struct node *nptr = malloc(sizeof(struct node));
assert(nptr != NULL);
if (nptr == NULL) {
fprintf(stderr, "Failed to malloc\n");
return;
}
nptr->cell = cell;
nptr->next = NULL;
nptr->parent = parent;
if (tail == NULL) {
front = nptr;
tail = nptr;
} else {
tail->next = nptr;
tail = tail->next;
}
}
/**
* Free queue
*/
void freeQueue() {
while (front != NULL) {
struct node *temp = front;
front = front->next;
free(temp);
}
tail = NULL; // Set tail to NULL as the queue is now empty
}
/**
* Dequeue node from queue.
* @param node pointer to a node, which will be dequeued
*/
void dequeue(struct node *node) {
if (front == NULL) {
front = NULL;
tail = NULL;
} else {
struct node *temp;
temp = front;
*node = *temp;
if (front == tail) {
front = front->next;
tail = tail->next;
} else {
front = front->next;
}
free(temp);
}
}
int main(int argc, char **argv) {
// TODO: Maybe move to function?
if (argc == 2 && !strcmp(argv[1], "--help")) {
print_help();
return 0;
} else if (argc == 3 && !strcmp(argv[1], "--test")) {
unsigned char *cells = maze_test(argv[2]);
if (cells == NULL) {
fprintf(stdout, "Invalid\n");
return 0;
} else {
free(cells);
fprintf(stdout, "Valid\n");
return 0;
}
} else if (argc == 5 && !strcmp(argv[1], "--rpath")) {
coordinates_t start = {.row = atoi(argv[2]), .col = atoi(argv[3])};
int result = path_by_rule(start, argv[4], RIGHT_HAND);
if (result == -1) {
return 1;
}
} else if (argc == 5 && !strcmp(argv[1], "--lpath")) {
coordinates_t start = {.row = atoi(argv[2]), .col = atoi(argv[3])};
int result = path_by_rule(start, argv[4], LEFT_HAND);
if (result == -1) {
return 1;
}
} else if (argc == 5 && !strcmp(argv[1], "--shortest")) {
coordinates_t start = {.row = atoi(argv[2]), .col = atoi(argv[3])};
int result = bfs(start, argv[4]);
if (result == -1) {
return 1;
}
} else {
fprintf(stderr, "Invalid argument!\n");
}
return 0;
}
/**
* Validate starting cell
* @param maze Pointer to loaded maze
* @param r cell row
* @param c cell column
* @returns True as valid, false as invalid
*/
bool is_valid_start(Map *maze, int r, int c) {
if (r == 0 || c == 0)
return false;
bool is_maze_col_even = (maze->cols % 2) == 0;
bool is_cell_row_even = (r % 2) == 0;
bool is_cell_index_even = get_cell_index(maze, r, c) % 2 == 0;
// From left
if (c == 1 && !isborder(maze, r, c, 0)) {
return true;
}
// From up
if (r == 1) {
if ((c % 2 != 0) && !isborder(maze, r, c, 2)) {
return true;
}
}
// From down
if (r == maze->rows) {
if (is_maze_col_even && is_cell_row_even) {
if (!is_cell_index_even && !isborder(maze, r, c, 2)) {
return true;
}
} else {
if (is_cell_index_even && !isborder(maze, r, c, 2)) {
return true;
}
}
}
// From right
if (c == maze->cols && !isborder(maze, r, c, 1)) {
return true;
}
return false;
}
/**
* Check if cell is in maze
* @param maze pointer to loaded maze
* @param start coordinates of start cell
* @return boolean
*/
bool is_in_maze(Map *maze, coordinates_t cell) {
if (cell.row < 1 || cell.row > maze->rows || cell.col < 1 ||
cell.col > maze->cols) {
return false;
}
return true;
}
/**
* Returns 1-based index for given cell.
* @param maze pointer to loaded maze
* @param r Row index of the cell (1-based).
* @param c Column index of the cell (1-based).
* @return cell index in maze
*/
int get_cell_index(Map *maze, int r, int c) {
// +1 to remove zero indexing
return ((r - 1) * maze->cols + (c - 1)) + 1;
}
/**
* Checks if is cell labeled
* @param maze pointer to loaded maze
* @param cell coordinate of given cell
* @return true as labeled, false as not labeled
*/
bool is_lebeled(Map *maze, coordinates_t cell) {
// -1 because we need straight access to cell
int cell_index = get_cell_index(maze, cell.row, cell.col) - 1;
if ((maze->cells[cell_index] >> 7) & 1) {
return true;
}
return false;
}
/**
* Label given cell
* @param maze pointer to loaded maze
* @param cell coordinate of given cell
*/
void set_label(Map *maze, coordinates_t cell) {
int cell_index = get_cell_index(maze, cell.row, cell.col) - 1;
maze->cells[cell_index] = maze->cells[cell_index] | 128;
}
/**
* Check if cell is exit (used in BFS)
* @param maze pointer to loaded maze
* @param start coordinates of start cell
* @param base_cell coordinates of current_cell
* @param new_cell current_cells neighbours coordinates
* @return true for exit, otherwise false
*/
bool is_goal(Map *maze, coordinates_t start, coordinates_t new_cell,
coordinates_t base_cell) {
// Return false immediately if the current position is the start position,
// or if the current position is outside the bounds of the maze.
if (!is_in_maze(maze, new_cell) &&
(base_cell.row != start.row || base_cell.col != start.col)) {
return true;
}
return false;
}
/**
* Reconstructs path from visited cells in BFS
* @param s coordinates of start
* @param e end node
*/
void reconstruct_path(coordinates_t s, struct node e) {
struct node *current = &e;
Array path;
if (init_array(&path, 20) == -1) {
fprintf(stderr, "Array did not init!\n");
return;
}
// Trace back from the end node to the start node
while (current != NULL) {
array_insert(&path, *current);
current = current->parent;
}
if (path.array[path.used - 1].cell.row == s.row &&
path.array[path.used - 1].cell.col == s.col) {
// Display the path in reverse (from start to end)
for (int i = path.used - 1; i >= 0; i--) {
fprintf(stdout, "%d,%d\n", path.array[i].cell.row,
path.array[i].cell.col);
}
} else {
fprintf(stdout, "%d,%d\n", s.row, s.col);
}
free_array(&path);
}
/**
* BFS neighbour checking
* @param maze pointer to loaded maze
* @param start coordinates of start
* @param duqueued_node
* @param visited pointer to array of visited nodes
*/
bool check_neighbours(Map *maze, coordinates_t start,
struct node *dequeued_node, Array *visited) {
// Search visited for node and its index
struct node *lastVisited = &visited->array[visited->used - 1];
for (int k = 0; k < (int)visited->used; k++) {
if (visited->array[k].cell.row == dequeued_node->cell.row &&
visited->array[k].cell.col == dequeued_node->cell.col) {
lastVisited = &visited->array[k];
}
}
for (int i = 0; i < CELL_NEIGHBORS_COUNT; i++) {
if (!isborder(maze, dequeued_node->cell.row, dequeued_node->cell.col, i)) {
struct node tmp = *dequeued_node;
next_cell(maze, &tmp.cell, i);
if (is_goal(maze, start, tmp.cell, dequeued_node->cell)) {
return false;
}
if (is_in_maze(maze, tmp.cell)) {
if (!is_lebeled(maze, tmp.cell)) {
set_label(maze, tmp.cell);
tmp.parent = lastVisited;
array_insert(visited, tmp);
enqueue(tmp.cell, lastVisited);
}
}
}
}
return true;
}
/**
* BFS algorithm (https://en.wikipedia.org/wiki/Breadth-first_search)
* @param start coordinates for start cell
* @param file_name name of the maze file
* @return -1 for failure, 0 for success
*/
int bfs(coordinates_t start, char *file_name) {
// Load file
FILE *fptr;
Map maze = {0};
if (init_maze(&fptr, &maze, file_name) == NULL) {
fprintf(stderr, "Map did not initialize!\n");
return -1;
}
if (map_load(&fptr, &maze) == NULL) {
fprintf(stderr, "Map did not load!\n");
fclose(fptr);
return -1;
}
if (!is_valid_start(&maze, start.row, start.col)) {
fprintf(stderr, "Invalid entry cell!\n");
free_maze(&fptr, &maze);
return -1;
}
Array visited;
if (init_array(&visited, maze.cols * maze.rows) == -1) {
fprintf(stderr, "Array did not init!\n");
return -1;
}
struct node end = {0};
struct node root = {.cell = start, .parent = NULL, .next = NULL};
enqueue(start, NULL);
set_label(&maze, start);
array_insert(&visited, root);
while (front != NULL) {
struct node dequeued_node;
dequeue(&dequeued_node);
if (!check_neighbours(&maze, root.cell, &dequeued_node, &visited)) {
end = dequeued_node;
break;
};
}
reconstruct_path(start, end);
freeQueue();
free_maze(&fptr, &maze);
free_array(&visited);
return 0;
}
/**
* @param map pointer to a map
* @param r Row index of the cell (1-based).
* @param c Column index of the cell (1-based).
* @param leftright Hand rule (7 left, 6 right)
* @return 0 as left, 1 as right, 2 as up/down
*/
int start_border(Map *map, int r, int c, int leftright) {
// From left
if (c == 1 && !isborder(map, r, c, 0)) {
// Even row
if (r % 2 != 0) {
return leftright == RIGHT_HAND ? RIGHT_BORDER : VERTICAL_BORDER;
} else {
return leftright == RIGHT_HAND ? VERTICAL_BORDER : RIGHT_BORDER;
}
}
// From up
if (r == 1 && !isborder(map, r, c, 2)) {
return leftright == RIGHT_HAND ? LEFT_BORDER : RIGHT_BORDER;
}
// From down
if (r == map->rows && !isborder(map, r, c, 2)) {
return leftright == RIGHT_HAND ? RIGHT_BORDER : LEFT_BORDER;
}
// From right
if (c == map->cols && !isborder(map, r, c, 1)) {
if (r % 2 != 0) {
return leftright == RIGHT_HAND ? LEFT_BORDER : VERTICAL_BORDER;
} else {
return leftright == RIGHT_HAND ? VERTICAL_BORDER : LEFT_BORDER;
}
}
return -1;
}
/**
* Path finding algorithm
* @param start_cell coordinates of start
* @param file_name name of the file with maze
* @param leftright specifies path finding rule (7 left, 6 right)
* @return 0 for left, 1 for right, 2 for up/down
*/
int path_by_rule(coordinates_t start_cell, char *file_name, int leftright) {
// Load file
FILE *fptr;
Map maze = {0};
if (init_maze(&fptr, &maze, file_name) == NULL) {
fprintf(stderr, "Map did not initialize!\n");
return -1;
}
if (map_load(&fptr, &maze) == NULL) {
fprintf(stderr, "Map did not load!\n");
fclose(fptr);
return -1;
}
if (!is_valid_start(&maze, start_cell.row, start_cell.col)) {
fprintf(stderr, "Invalid entry cell!\n");
free_maze(&fptr, &maze);
return -1;
}
coordinates_t curr_cell = start_cell;
int curr_border =
start_border(&maze, start_cell.row, start_cell.col, leftright);
if (curr_border == -1) {
fprintf(stderr, "Invalid entry cell!\n");
free_maze(&fptr, &maze);
return -1;
}
// Search algorithm
while (1) {
fprintf(stdout, "%d,%d\n", curr_cell.row, curr_cell.col);
while (isborder(&maze, curr_cell.row, curr_cell.col, curr_border)) {
curr_border = get_side_border(&maze, curr_cell, curr_border, leftright);
}
next_cell(&maze, &curr_cell, curr_border);
if (is_exit(&maze, curr_cell)) {
break;
}
curr_border =
get_side_border(&maze, curr_cell, new_border(curr_border), leftright);
if (curr_border == -1) {
return -1;
}
}
free_maze(&fptr, &maze);
return 0;
}
/**
* Used in path_by_rule
* @param fptr pointer on file pointer
* @param maze pointer to map
*/
bool is_exit(Map *maze, coordinates_t cell) {
if ((cell.row < 1 || cell.row > maze->rows || cell.col < 1 ||
cell.col > maze->cols)) {
return true;
}
return false;
}
/**
* @param fptr pointer on file pointer
* @param maze pointer to map
*/
void free_maze(FILE **fptr, Map *maze) {
fclose(*fptr);
free(maze->cells);
}
/**
* Returns border in newly visited cell
* @param old_boreder number representing border
* @return new border
*/
int new_border(int old_border) {
if (old_border == 1) {
return 0;
} else if (old_border == 0) {
return 1;
} else if (old_border == 2) {
return 2;
}
return -1;
}
/**
* Returns border in new cell
* @param maze pointer to map
* @param curr_cell pointer coordinates of currently searched cell
* @param border currently searched border
*/
void next_cell(Map *maze, coordinates_t *curr_cell, int border) {
bool isMazeColEven = (maze->cols % 2) == 0;
bool isRowEven = (curr_cell->row % 2) == 0;
int cellIndex = get_cell_index(maze, curr_cell->row, curr_cell->col);
bool isCellIndexEven = ((cellIndex) % 2 == 0);
if (border == 2) {
if (isMazeColEven && isRowEven) {
curr_cell->row += isCellIndexEven ? -1 : 1;
} else {
curr_cell->row += isCellIndexEven ? 1 : -1;
}
} else if (border == 1) {
curr_cell->col += 1;
} else if (border == 0) {
curr_cell->col -= 1;
}
}
/**
* Returns the next border(which should be checked)
* @param maze pointer to map
* @param curr_cell pointer coordinates of currently searched cell
* @param border currently searched border
* @return number representing a border
*/
int get_side_border(Map *map, coordinates_t curr_cell, int border,
int leftright) {
bool isMazeColEven = (map->cols % 2) == 0;
bool isRowEven = (curr_cell.row % 2) == 0;
int cellIndex = get_cell_index(map, curr_cell.row, curr_cell.col);
bool isCellIndexEven = ((cellIndex) % 2 == 0);
// When maze has even cols, triangles are changing direction on every even row
if (isMazeColEven && isRowEven) {
if (isCellIndexEven) {
return leftright == LEFT_HAND ? lpath_odd_dir[border]
: rpath_odd_dir[border];
} else {
return leftright == LEFT_HAND ? lpath_even_dir[border]
: rpath_even_dir[border];
}
} else {
if (isCellIndexEven) {
return leftright == LEFT_HAND ? lpath_even_dir[border]
: rpath_even_dir[border];
} else {
return leftright == LEFT_HAND ? lpath_odd_dir[border]
: rpath_odd_dir[border];
}
}
return -1;
}
/**
* Determines if a specified side of a cell is a border.
* @param map Pointer to the map structure containing the maze.
* @param r Row index of the cell (1-based).
* @param c Column index of the cell (1-based).
* @param border The specific border side to check (0 left, 1 right, 2 up/down).
* @return true if the specified side is a border, false otherwise.
*/
bool isborder(Map *map, int r, int c, int border) {
// Do not forget on 0 indexing
return (map->cells[(r - 1) * map->cols + (c - 1)] >> border) & 1 ? true
: false;
}
/**
* Loads maze into map struct
* @param fptr Pointer to the file pointer for reading the maze.
* @param map Pointer to the maze structure to be loaded.
* @return pointer to map struct, NULL as failure
*/
Map *map_load(FILE **fptr, Map *map) {
int count = 1;
int tmp = 0;
while (fscanf(*fptr, "%d", &tmp) != EOF) {
if (tmp < 0 || tmp > 255) {
free(map->cells);
return NULL;
}
if (count > (map->rows * map->cols)) {
free(map->cells);
return NULL;
}
// Save value
map->cells[count - 1] = (unsigned char)tmp;
count++;
}
if (count != (map->rows * map->cols + 1)) {
free(map->cells);
return NULL;
}
return map;
}
/**
* Sets up the maze structure from a file.
* @param fptr Pointer to the file pointer for reading the maze.
* @param map Pointer to the maze structure to be initialized.
* @param file_name Name of the file containing the maze.
* @return Initialized maze structure on success, or NULL on error.
*/
Map *init_maze(FILE **fptr, Map *map, char *file_name) {
// Open file
*fptr = fopen(file_name, "r");
if (*fptr == NULL) {
return NULL;
}
// Get size from first row
int size[2] = {0};
for (int i = 0; i < 2; i++) {
fscanf(*fptr, "%d", &size[i]);
}
unsigned char *ptr = malloc(sizeof(unsigned char) * size[0] * size[1]);
if (ptr == NULL) {
fclose(*fptr);
return NULL;
}
map->rows = size[0];
map->cols = size[1];
map->cells = ptr;
for (int i = 0; i < size[0] * size[1]; i++) {
map->cells[i] = 0;
}
return map;
}
/**
* Validates a maze's structure from a file, checking its size and border rules.
* @param file_name Path to the file containing the maze data.
* @return Pointer to maze data if successful, NULL if any validation fails.
*/
unsigned char *maze_test(char *file_name) {
// Load file
FILE *fptr;
Map maze = {0};
if (init_maze(&fptr, &maze, file_name) == NULL) {
return NULL;
}
if (map_load(&fptr, &maze) == NULL) {
fclose(fptr);
return NULL;
}
// Check border validity
// get neigbourts around one cell
for (int row = 1; row <= maze.rows; row++) {
for (int col = 1; col <= maze.cols; col++) {
unsigned char neighbors[CELL_NEIGHBORS_COUNT];
// For accessing maze.cells we need 0 indexing
int cellIndex = (row - 1) * maze.cols + (col - 1);
bool isCellIndexEven = (cellIndex % 2) == 0;
bool isMazeColEven = (maze.cols % 2) == 0;
bool isCurrRowEven = (row % 2) == 0;
// Left
neighbors[0] = (col > 1) ? maze.cells[cellIndex - 1] : EMPTY_CELL;
// Right
neighbors[1] = (col < maze.cols) ? maze.cells[cellIndex + 1] : EMPTY_CELL;
if (isMazeColEven && isCurrRowEven) {
if (isCellIndexEven) {
neighbors[2] = (row < maze.rows) ? maze.cells[cellIndex + maze.cols]
: EMPTY_CELL;
} else {
neighbors[2] =
(row > 1) ? maze.cells[cellIndex - maze.cols] : EMPTY_CELL;
}
} else {
if (isCellIndexEven) {
neighbors[2] =
(row > 1) ? maze.cells[cellIndex - maze.cols] : EMPTY_CELL;
} else {
neighbors[2] = (row < maze.rows) ? maze.cells[cellIndex + maze.cols]
: EMPTY_CELL;
}
}
if (!valid_borders(maze.cells[cellIndex], neighbors)) {
free_maze(&fptr, &maze);
return NULL;
}
}
}
fclose(fptr);
return maze.cells;
}
/**
* Checks if the current cell's borders are compatible with its neighbors.
* @param cell Value of the current cell
* @param neighbors Array of neighbor cells' values in the order: [left, right,
* up/down].
* @return True if all neighboring sides are compatible with the current
* cell, false otherwise.
*/
bool valid_borders(unsigned char cell, unsigned char *neighbors) {
// Left neighbor
if (neighbors[0] != EMPTY_CELL &&
(((cell >> 0) & 1) != ((neighbors[0] >> 1) & 1))) {
return false;
}
// Right neighbor
if (neighbors[1] != EMPTY_CELL &&
(((cell >> 1) & 1) != ((neighbors[1] >> 0) & 1))) {
return false;
}
// Up/Down neighbor
if (neighbors[2] != EMPTY_CELL &&
(((cell >> 2) & 1) != ((neighbors[2] >> 2) & 1))) {
return false;
}
return true;
}
void print_help() {
char *help = "maze [command] <options>\n\
\n\
--test <file> check, if given file has needed maze definition. If the maze file is valid program pritns Valid. Otherwise prints Invalid\n\
--rpath <R> <C> tries to find path in maze on entry on column R and row C. Path is searched by rule of right hand\n\
--lpath <R> <C> trues to find path in maze on entry on column R and row C. Path is searched by the rule of left hand\n\
--shortest <R> <C> tries to find shortest path in maze on entry on column R and row C.";
printf("%s\n", help);
}