From 78f6cd22a1581c4b455adaae6231e7d7c3146ab8 Mon Sep 17 00:00:00 2001 From: vSKAH Date: Sat, 14 Oct 2023 21:11:03 +0200 Subject: [PATCH] add:(vSKAH): Add maps tests fix:(vSKAH): Fix norm errors fix:(vSKAH): Fix parsing leaks / errors --- errors_utils.c | 25 +++++++++++++++---------- graphics_utils.c | 17 +++++++++++++++++ main.c | 18 +++++++----------- maps/1.ber | 8 -------- maps/double_exit.ber | 8 ++++++++ maps/double_player.ber | 8 ++++++++ maps/empty.ber | 0 maps/invalid_1.ber | 1 + maps/invalid_2.ber | 1 + maps/invalid_3.ber | 4 ++++ maps/invalid_char.ber | 8 ++++++++ maps/invalid_extension.be | 3 +++ maps/invalid_shape_1.ber | 8 ++++++++ maps/invalid_shape_2.ber | 9 +++++++++ maps/no_collectible.ber | 8 ++++++++ maps/no_exit.ber | 8 ++++++++ maps/no_player.ber | 8 ++++++++ maps/not_solvable_1.ber | 3 +++ maps/not_solvable_2.ber | 8 ++++++++ maps/not_solvable_3.ber | 8 ++++++++ maps/nothing.ber | 8 ++++++++ maps/valid.ber | 8 ++++++++ maps_utils.c | 20 ++++++++++---------- rectangle_check.c | 2 ++ so_long.h | 5 +++-- 25 files changed, 163 insertions(+), 41 deletions(-) delete mode 100644 maps/1.ber create mode 100644 maps/double_exit.ber create mode 100644 maps/double_player.ber create mode 100644 maps/empty.ber create mode 100644 maps/invalid_1.ber create mode 100644 maps/invalid_2.ber create mode 100644 maps/invalid_3.ber create mode 100644 maps/invalid_char.ber create mode 100644 maps/invalid_extension.be create mode 100644 maps/invalid_shape_1.ber create mode 100644 maps/invalid_shape_2.ber create mode 100644 maps/no_collectible.ber create mode 100644 maps/no_exit.ber create mode 100644 maps/no_player.ber create mode 100644 maps/not_solvable_1.ber create mode 100644 maps/not_solvable_2.ber create mode 100644 maps/not_solvable_3.ber create mode 100644 maps/nothing.ber create mode 100644 maps/valid.ber diff --git a/errors_utils.c b/errors_utils.c index 5b103a0..5f44864 100644 --- a/errors_utils.c +++ b/errors_utils.c @@ -48,27 +48,32 @@ t_boolean handle_map_solve(char *path, t_game game) return (0); } -int handle_map_error(char *path, t_game game) +int handle_elements_error(char *path, t_game game) { int elements_code; + elements_code = valid_elements(game.world); + if (elements_code < 0) + { + ft_printf("Error\n -> Map must contains 1 E, 1 P, minimum 1 C"); + free_collectibles(game.world.player.collectibles); + free_map(&game.world); + return (-1); + } + return (handle_map_solve(path, game)); +} + +int handle_map_error(t_game game) +{ if (has_illegal_character(game.world)) { free_map(&game.world); - free_collectibles(game.world.player.collectibles); return (ft_printf("Error\n -> Invalid Map / illegal character"), -1); } if (!is_valid_shape(game.world)) { free_map(&game.world); - free_collectibles(game.world.player.collectibles); return (ft_printf("Error\n -> Invalid shape / not closed"), -1); } - elements_code = valid_elements(game.world); - if (elements_code < 0) - { - ft_printf("Error\n -> Map must contains 1 E, 1 P, minimum 1 C"); - return (-1); - } - return (handle_map_solve(path, game)); + return (0); } diff --git a/graphics_utils.c b/graphics_utils.c index 97d6ca7..97d93db 100644 --- a/graphics_utils.c +++ b/graphics_utils.c @@ -12,6 +12,23 @@ #include "so_long.h" +int init_graphics_part(t_game *game) +{ + game->mlx = mlx_init(); + if (!game->mlx) + return (-1); + game->textures = load_textures(game->mlx); + if (free_unavailable_texture(*game)) + return (-1); + game->window = mlx_new_window(game->mlx, \ + 128 * ft_strlen(game->world.map[0]), \ + 128 * game->world.length_y, "Xe'Burger"); + draws(*game); + mlx_hook(game->window, 2, (1L << 0), on_player_move, game); + mlx_loop(game->mlx); + return (0); +} + void draw_type(t_game game, t_texture texture) { int index_y; diff --git a/main.c b/main.c index 1f3a22a..6386bbe 100644 --- a/main.c +++ b/main.c @@ -17,26 +17,22 @@ int main(int argc, char *argv[]) int fd; t_game game; int value; + int graphics; fd = handle_file_error(argv); if (argc < 1 || fd < 0) return (0); load_map(fd, argv[1], &game.world); + value = handle_map_error(game); + if (value < 0) + return (0); game.world.player = init_player(find_element(game.world, 'P'), \ load_collectibles(&game.world)); - value = handle_map_error(argv[1], game); + value = handle_elements_error(argv[1], game); if (value < 0) return (0); - game.mlx = mlx_init(); - if (!game.mlx) - return (0); - game.textures = load_textures(game.mlx); - if (free_unavailable_texture(game)) + graphics = init_graphics_part(&game); + if (graphics < 0) return (0); - game.window = mlx_new_window(game.mlx, 128 * ft_strlen(game.world.map[0]), \ - 128 * game.world.length_y, "Xe'Burger"); - draws(game); - mlx_hook(game.window, 2, (1L << 0), on_player_move, &game); - mlx_loop(game.mlx); return (destroy(&game), 0); } diff --git a/maps/1.ber b/maps/1.ber deleted file mode 100644 index c7c87c0..0000000 --- a/maps/1.ber +++ /dev/null @@ -1,8 +0,0 @@ -1111111111111 -10000000000C1 -10P0000000001 -1000000000001 -1000111110001 -1100000000001 -1C0000C0001E1 -1111111111111 \ No newline at end of file diff --git a/maps/double_exit.ber b/maps/double_exit.ber new file mode 100644 index 0000000..ff812bc --- /dev/null +++ b/maps/double_exit.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +10P000000000000001 +1000000C0000000001 +100000000000010001 +100E000000000010E1 +100000000000010001 +111111111111111111 \ No newline at end of file diff --git a/maps/double_player.ber b/maps/double_player.ber new file mode 100644 index 0000000..36fa2a2 --- /dev/null +++ b/maps/double_player.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +10P0P0000000000001 +1000000C0000000001 +100000000000010001 +100E00000000001001 +100000000000010001 +111111111111111111 \ No newline at end of file diff --git a/maps/empty.ber b/maps/empty.ber new file mode 100644 index 0000000..e69de29 diff --git a/maps/invalid_1.ber b/maps/invalid_1.ber new file mode 100644 index 0000000..17851a4 --- /dev/null +++ b/maps/invalid_1.ber @@ -0,0 +1 @@ +\0 \ No newline at end of file diff --git a/maps/invalid_2.ber b/maps/invalid_2.ber new file mode 100644 index 0000000..5424ade --- /dev/null +++ b/maps/invalid_2.ber @@ -0,0 +1 @@ +\n \ No newline at end of file diff --git a/maps/invalid_3.ber b/maps/invalid_3.ber new file mode 100644 index 0000000..9823571 --- /dev/null +++ b/maps/invalid_3.ber @@ -0,0 +1,4 @@ +\n\n\n\n\n +\n\n\n +\n\n\n +\n\n \ No newline at end of file diff --git a/maps/invalid_char.ber b/maps/invalid_char.ber new file mode 100644 index 0000000..e5b92ef --- /dev/null +++ b/maps/invalid_char.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +10P00000000F000001 +1000000C0000000001 +100000000000010001 +100E00000000001001 +100000000000010001 +111111111111111111 \ No newline at end of file diff --git a/maps/invalid_extension.be b/maps/invalid_extension.be new file mode 100644 index 0000000..51798ca --- /dev/null +++ b/maps/invalid_extension.be @@ -0,0 +1,3 @@ +111111111111111111 +100C00E0000P000001 +111111111111111111 \ No newline at end of file diff --git a/maps/invalid_shape_1.ber b/maps/invalid_shape_1.ber new file mode 100644 index 0000000..a7169d5 --- /dev/null +++ b/maps/invalid_shape_1.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +10P000000000000001 +1000000C0000000001 +100000000000010001 +100E00000000001001 +1000000000000100011 +111111111111111111 \ No newline at end of file diff --git a/maps/invalid_shape_2.ber b/maps/invalid_shape_2.ber new file mode 100644 index 0000000..12d4b6e --- /dev/null +++ b/maps/invalid_shape_2.ber @@ -0,0 +1,9 @@ +111111111111111111 +100000000000000001 +10P000000000000001 +1000000C0000000001 +100000000000010001 +100E00000000001001 +100000000000010001 +111111111111111111 +1 \ No newline at end of file diff --git a/maps/no_collectible.ber b/maps/no_collectible.ber new file mode 100644 index 0000000..6a3d751 --- /dev/null +++ b/maps/no_collectible.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +100P00000000000001 +100000000000000001 +100000000000010001 +1000000000001E1001 +100000000000010001 +111111111111111111 \ No newline at end of file diff --git a/maps/no_exit.ber b/maps/no_exit.ber new file mode 100644 index 0000000..419f5ff --- /dev/null +++ b/maps/no_exit.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +100P00000000000001 +1000000C0000000001 +100000000000010001 +100000000000101001 +100000000000010001 +111111111111111111 \ No newline at end of file diff --git a/maps/no_player.ber b/maps/no_player.ber new file mode 100644 index 0000000..a923922 --- /dev/null +++ b/maps/no_player.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +100000000000000001 +1000000C0000000001 +100000000000010001 +100E00000000001001 +100000000000010001 +111111111111111111 \ No newline at end of file diff --git a/maps/not_solvable_1.ber b/maps/not_solvable_1.ber new file mode 100644 index 0000000..51798ca --- /dev/null +++ b/maps/not_solvable_1.ber @@ -0,0 +1,3 @@ +111111111111111111 +100C00E0000P000001 +111111111111111111 \ No newline at end of file diff --git a/maps/not_solvable_2.ber b/maps/not_solvable_2.ber new file mode 100644 index 0000000..e6e2e28 --- /dev/null +++ b/maps/not_solvable_2.ber @@ -0,0 +1,8 @@ +111111111111111111 +100100000000000001 +101P10000000000001 +100100000000000001 +1000000000000C0001 +1000000000000E0001 +100000000000000001 +111111111111111111 \ No newline at end of file diff --git a/maps/not_solvable_3.ber b/maps/not_solvable_3.ber new file mode 100644 index 0000000..bf31b23 --- /dev/null +++ b/maps/not_solvable_3.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +100P00000000000001 +10000000C000000001 +100000000000010001 +1000000000001E1001 +100000000000010001 +111111111111111111 \ No newline at end of file diff --git a/maps/nothing.ber b/maps/nothing.ber new file mode 100644 index 0000000..3fd27af --- /dev/null +++ b/maps/nothing.ber @@ -0,0 +1,8 @@ +111111111111111111 +100000000000000001 +100000000000000001 +100000000000000001 +100000000000010001 +100000000000101001 +100000000000010001 +111111111111111111 \ No newline at end of file diff --git a/maps/valid.ber b/maps/valid.ber new file mode 100644 index 0000000..6c4ea28 --- /dev/null +++ b/maps/valid.ber @@ -0,0 +1,8 @@ +111111111111111111 +10000000000C000001 +100P00000000000C01 +1000000C0000000001 +1000C0000000010001 +100000000000101001 +1000000C0000010E01 +111111111111111111 \ No newline at end of file diff --git a/maps_utils.c b/maps_utils.c index 7e9ea06..37fdbfe 100644 --- a/maps_utils.c +++ b/maps_utils.c @@ -86,22 +86,22 @@ void is_solvable(char **map, int x, int y, int length_y) character = map[y][x]; if (character == 'O') return ; - if (character == '0' || character == 'C' || character == 'P') + if (character == '0' || character == 'C' || \ + character == 'P' || character == 'E') { - map[y][x] = 'O'; - is_solvable(map, x - 1, y, length_y); - is_solvable(map, x + 1, y, length_y); - is_solvable(map, x, y + 1, length_y); - is_solvable(map, x, y - 1, length_y); - } - if (character == 'E') - { - map[y][x] = 'L'; + if (character == 'E') + { + map[y][x] = 'L'; + return ; + } + else + map[y][x] = 'O'; is_solvable(map, x - 1, y, length_y); is_solvable(map, x + 1, y, length_y); is_solvable(map, x, y + 1, length_y); is_solvable(map, x, y - 1, length_y); } + return ; } t_boolean is_inside_world(int y, int x, t_world world) diff --git a/rectangle_check.c b/rectangle_check.c index 7dc612c..429f19a 100644 --- a/rectangle_check.c +++ b/rectangle_check.c @@ -45,6 +45,8 @@ t_boolean has_good_shape(t_world world) int x_length; int y_index; + if (world.map == NULL || world.map[0] == NULL) + return (_false); x_length = (int) ft_strlen(world.map[0]); y_index = 0; while (y_index < world.length_y && world.map[y_index]) diff --git a/so_long.h b/so_long.h index df855e9..66f9629 100644 --- a/so_long.h +++ b/so_long.h @@ -71,10 +71,11 @@ typedef struct s_game t_boolean loc_equals(t_location loc_1, t_location loc_2); t_boolean is_inside_world(int y, int x, t_world world); t_boolean ft_endwith(char *src, char *pattern); - +int init_graphics_part(t_game *game); // ** ERRORS **// int handle_file_error(char *argv[]); -int handle_map_error(char *path, t_game game); +int handle_map_error(t_game game); +int handle_elements_error(char *path, t_game game); // ** MAP ** // t_world *load_map(int fd, char *path, t_world *world);