From 0ebea4f1b15aeba4e92ab8c2e1a75eba1a44efd6 Mon Sep 17 00:00:00 2001 From: Yesim Zaim Date: Thu, 14 Dec 2023 13:37:32 +0100 Subject: [PATCH] added rectangle parser --- Makefile | 3 +- include/test_utils.h | 1 + src/.render.c.swp | Bin 12288 -> 0 bytes src/game.c | 6 +-- src/parser/check_map.c | 34 +++++++++++++++- src/parser/parser.c | 2 + src/test_utils.c | 19 +++++++++ src/utils/free.c | 1 + test.c | 90 ----------------------------------------- 9 files changed, 61 insertions(+), 95 deletions(-) create mode 100644 include/test_utils.h delete mode 100644 src/.render.c.swp delete mode 100644 test.c diff --git a/Makefile b/Makefile index a57c282..5c1d3a0 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,8 @@ RUN_CMD := ./$(NAME) test_maps/simple.cub # CFLAGS += -Wall -Wextra -Werror CFLAGS += -Wall -Wextra -# CFLAGS += -g -fsanitize=address +CFLAGS += -g -fsanitize=address +# CFLAGS += -g # CFLAGS += -Ofast -flto -march=native LIBFT := libft/build/libft.a diff --git a/include/test_utils.h b/include/test_utils.h new file mode 100644 index 0000000..09027e1 --- /dev/null +++ b/include/test_utils.h @@ -0,0 +1 @@ +void print_map_cell(t_cell_type *level, uint32_t w, uint32_t h); \ No newline at end of file diff --git a/src/.render.c.swp b/src/.render.c.swp deleted file mode 100644 index 7236d4c63294969c69a8c7ee5b3aea2398f2380e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmeI2OKclO9LA@-7YIclA<@$`7Q(R|du=CuIBi6lSV>f;iPD6siqh5E9w!_8>Uy2n zjRJ@_oRAP4IfFn{+(5vABNvWb0C4~zaY5W@B@TS^*tH#!HX%7sG?V-?&;4fRoBym- zb@S%4(=+r`JVlTmCFH%MGwe$Gb@KjG25DcjoeFQiO)4eb6OH6hyihCJm3Xzh+wkXw zV508U)>y%7Bnr)Zx|nFVg@nthMdro}yFIDfwFBCLgFDbi(y64$bwd5e>En+r9$Zo@ zXa}?d+5zo=c0fCz9ncPF2ebosj00Z%2zddky-Th5g36C<%T-lp+5zo=c0fCz9ncPF z2ebp)0quZxKs%rv&<@;z4%kIP-oBfVU+%%;@&Et+zyGfvCFBa|17AKu$aCNr_@(eGu7Y_m44wpkJw(V&a0C1az6WoCA@JjaguDS#;0J?{*T67Hfg~`& zPY+-mcoU?+@Anh(A@~4Hf-IN-A#fagaUUU{gU`Ub;2rQXXaE;5Fa@&UBsd110N>tA z$k*U2@G1BNd<;GUFM$`qc`ypjf?*H>6#N0-egnS(?%~fsC+&cC;J@R*7BTQ^IML_` zvP!vS)tjDGcUr6*b}IG?i%};=%@`e@&CSkR=f=j*PtMOSUYHob_)TJLtU6^z!wx+| z8_qR0Kn#bTrRE4B2CrM50dWj1)T#}SHl3=MPN9+aVVsy@aCE~ddaE?zS67*{vg(bH zO|o8did238WfjWIcCGwMm@iU1Xn8au3Il|weH2)^Y_;WP_`VF$(2@;C(X}@$-V&}b z&yLZVvALzNd<3(jW?h<|SeP21Q7WHXj;Vsm+kDr2+y1`gFl@f_FgjJM@S)2jHIp1X zm7SiPT7X}wwZ}9yD<+dB%6fadB^A4Ft+-Ax>{)z6G{XN%GsIRKSDUuW#AXLa)oJn} zgQNAb-Nr{{tr+d8w_|iJH+DHYFO3LlarowMs)iS8JRQBT|x%WKz}2)UcHzDHP>uRAqjv$bG?{#@kn-q3AAye8F?=j9SH+wS0}Ovu7F}&aUbO zAA67XDD0}}3-*lOOC_4wd5ylH|LOQ%wA7ePG$Tu@ATwWZA40*8 M!9%+!xO55fHx9`YL;wH) diff --git a/src/game.c b/src/game.c index e35f28a..20be8af 100644 --- a/src/game.c +++ b/src/game.c @@ -39,9 +39,9 @@ t_cell_type MAP[] = { static void init_map(t_map *m) { - m->level = MAP; - m->width = MAP_WIDTH; - m->height = MAP_HEIGHT; + // m->level = MAP; + // m->width = MAP_WIDTH; + // m->height = MAP_HEIGHT; } diff --git a/src/parser/check_map.c b/src/parser/check_map.c index 394d2a3..80e975a 100644 --- a/src/parser/check_map.c +++ b/src/parser/check_map.c @@ -101,6 +101,37 @@ bool is_floor_exposed(t_meta *meta, char *map) return (false); } +t_cell_type find_enum_value(char c) +{ + if (c == ' ') + { + return (MAP_EMPTY); + } + else if (c == '1') + { + return (MAP_WALL); + } + else + { + return (MAP_SPACE); + } +} + +bool save_map(t_meta *meta, char *rect) +{ + uint32_t i; + + meta->map.level = malloc(sizeof(t_cell_type) * meta->map.width * meta->map.height); + if (!meta->map.level) + return (false); + i = 0; + while (rect[i]) + { + meta->map.level[i] = find_enum_value(rect[i]); + i++; + } + return (true); +} int check_map(t_meta *meta, char *rect) { if (!is_map_chars_valid(rect)) @@ -108,10 +139,11 @@ int check_map(t_meta *meta, char *rect) if (!save_start_pos(meta, rect)) return (pr_err(NO_PLAYER)); rect[find_index(meta, meta->player.position[VEC_X], meta->player.position[VEC_Y])] = '0'; - if (flood_fill(meta, rect, meta->player.position[VEC_X], meta->player.position[VEC_Y])) return (pr_err(INV_WALLS)); if (is_floor_exposed(meta, rect)) // maybe change to a warning? return (pr_err(OUT_OF_BOUNDS)); + if (!save_map(meta, rect)) + return(pr_err(MALL_ERR)); return (EXIT_SUCCESS); } diff --git a/src/parser/parser.c b/src/parser/parser.c index 895a368..1b0f5bb 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "meta.h" +# include "test_utils.h" char *read_file(int fd) { @@ -77,6 +78,7 @@ int parser(t_meta *meta, char *map_file) return(pr_err(MALL_ERR), EXIT_FAILURE); if (check_map(meta, rect)) return (free(rect), EXIT_FAILURE); + print_map_cell(meta->map.level, meta->map.width, meta->map.height); free(rect); return (EXIT_SUCCESS); } diff --git a/src/test_utils.c b/src/test_utils.c index b8471c1..ca73fbb 100644 --- a/src/test_utils.c +++ b/src/test_utils.c @@ -48,3 +48,22 @@ void print_map(char *map, uint32_t w, uint32_t h) } } + +void print_map_cell(t_cell_type *level, uint32_t w, uint32_t h) +{ + size_t i = 0; + size_t k = 0; + size_t j; + while (i < h) + { + j = 0; + while (j < w) + { + printf("%d", level[k]); + k++; + j++; + } + printf("\n"); + i++; + } +} \ No newline at end of file diff --git a/src/utils/free.c b/src/utils/free.c index 8b4265b..b3643ee 100644 --- a/src/utils/free.c +++ b/src/utils/free.c @@ -24,4 +24,5 @@ void free_t_tex(t_tex *tex) void meta_free(t_meta *meta) { free_t_tex(&(meta->tex)); + free(meta->map.level); } \ No newline at end of file diff --git a/test.c b/test.c deleted file mode 100644 index 18d4f52..0000000 --- a/test.c +++ /dev/null @@ -1,90 +0,0 @@ -#include -#include -#include - -/* -Foreground colors: - - Black: \033[30m - Red: \033[31m - Green: \033[32m - Yellow: \033[33m - Blue: \033[34m - Magenta: \033[35m - Cyan: \033[36m - White: \033[37m - -Background colors (use 40 to 47 instead of 30 to 37): - - Black: \033[40m - Red: \033[41m - Green: \033[42m - Yellow: \033[43m - Blue: \033[44m - Magenta: \033[45m - Cyan: \033[46m - White: \033[47m - -Text styles: - - Reset: \033[0m (resets colors and styles to default) - Bold: \033[1m - Underline: \033[4m - Blink: \033[5m - Reverse: \033[7m -*/ - -int main(void) -{ - // parser tests - printf(" \033[37m \033[5mPARSER TESTS \033[0m \n\n"); - // bad extension - printf("\033[1;33m TEST FOR BAD EXTENSION: \033[0m\n"); - system("./app test_maps/bad_ext.u"); - printf("\n"); - // duplicate elements - printf("\033[1;33m TEST FOR DUPLICATE ELEMENTS: \033[0m\n"); - system("./app test_maps/duplicate_elements.cub"); - printf("\n"); - // no player - printf("\033[1;33m TEST FOR NO PLAYER: \033[0m\n"); - system("./app test_maps/no_player.cub"); - printf("\n"); - // not enough elements - printf("\033[1;33m TEST FOR MISSING ELEMENTS: \033[0m\n"); - system("./app test_maps/not_enough_elements.cub"); - printf("\n"); - // too many players - printf("\033[1;33m TEST FOR TOO MANY PLAYERS: \033[0m\n"); - system("./app test_maps/too_many_players.cub"); - printf("\n"); - // unclosed walls - printf("\033[1;33m TEST FOR UNCLOSED WALLS: \033[0m\n"); - system("./app test_maps/unclosed_walls.cub"); - printf("\n"); - // map with invalid chars - printf("\033[1;33m TEST FOR MAP WITH INVALID CHARS: \033[0m\n"); - system("./app test_maps/wrong_chars.cub"); - printf("\n"); - // wrong order - printf("\033[1;33m TEST FOR FILE WITH WRONG ORDER: \033[0m\n"); - system("./app test_maps/wrong_order.cub"); - printf("\n"); - // file failed to open - printf("\033[1;33m TEST FOR FILE FAILED TO OPEN: \033[0m\n"); - system("./app test_maps/filedoesntexist.cub"); - printf("\n"); - // file failed to open - printf("\033[1;33m TEST FOR FILE WITH NO MAP: \033[0m\n"); - system("./app test_maps/file_with_no_map.cub"); - printf("\n"); - // random chars between elements - printf("\033[1;33m TEST FOR FILE WITH RANDOM CHARS BETWEEN ELEMENTS: \033[0m\n"); - system("./app test_maps/random_chars_between_elements.cub"); - printf("\n"); - // colour code wrong - printf("\033[1;33m TEST FOR FILE WITH WRONG COLOR CODES: \033[0m\n"); - system("./app test_maps/wrong_color_code.cub"); - printf("\n"); - return (0); -} \ No newline at end of file