Skip to content

Commit

Permalink
add:(vSKAH): Add maps tests
Browse files Browse the repository at this point in the history
fix:(vSKAH): Fix norm errors
fix:(vSKAH): Fix parsing leaks / errors
  • Loading branch information
vSKAH committed Oct 14, 2023
1 parent 4a5d757 commit 78f6cd2
Show file tree
Hide file tree
Showing 25 changed files with 163 additions and 41 deletions.
25 changes: 15 additions & 10 deletions errors_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
17 changes: 17 additions & 0 deletions graphics_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 7 additions & 11 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 0 additions & 8 deletions maps/1.ber

This file was deleted.

8 changes: 8 additions & 0 deletions maps/double_exit.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
10P000000000000001
1000000C0000000001
100000000000010001
100E000000000010E1
100000000000010001
111111111111111111
8 changes: 8 additions & 0 deletions maps/double_player.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
10P0P0000000000001
1000000C0000000001
100000000000010001
100E00000000001001
100000000000010001
111111111111111111
Empty file added maps/empty.ber
Empty file.
1 change: 1 addition & 0 deletions maps/invalid_1.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\0
1 change: 1 addition & 0 deletions maps/invalid_2.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\n
4 changes: 4 additions & 0 deletions maps/invalid_3.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\n\n\n\n\n
\n\n\n
\n\n\n
\n\n
8 changes: 8 additions & 0 deletions maps/invalid_char.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
10P00000000F000001
1000000C0000000001
100000000000010001
100E00000000001001
100000000000010001
111111111111111111
3 changes: 3 additions & 0 deletions maps/invalid_extension.be
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
111111111111111111
100C00E0000P000001
111111111111111111
8 changes: 8 additions & 0 deletions maps/invalid_shape_1.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
10P000000000000001
1000000C0000000001
100000000000010001
100E00000000001001
1000000000000100011
111111111111111111
9 changes: 9 additions & 0 deletions maps/invalid_shape_2.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
111111111111111111
100000000000000001
10P000000000000001
1000000C0000000001
100000000000010001
100E00000000001001
100000000000010001
111111111111111111
1
8 changes: 8 additions & 0 deletions maps/no_collectible.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
100P00000000000001
100000000000000001
100000000000010001
1000000000001E1001
100000000000010001
111111111111111111
8 changes: 8 additions & 0 deletions maps/no_exit.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
100P00000000000001
1000000C0000000001
100000000000010001
100000000000101001
100000000000010001
111111111111111111
8 changes: 8 additions & 0 deletions maps/no_player.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
100000000000000001
1000000C0000000001
100000000000010001
100E00000000001001
100000000000010001
111111111111111111
3 changes: 3 additions & 0 deletions maps/not_solvable_1.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
111111111111111111
100C00E0000P000001
111111111111111111
8 changes: 8 additions & 0 deletions maps/not_solvable_2.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100100000000000001
101P10000000000001
100100000000000001
1000000000000C0001
1000000000000E0001
100000000000000001
111111111111111111
8 changes: 8 additions & 0 deletions maps/not_solvable_3.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
100P00000000000001
10000000C000000001
100000000000010001
1000000000001E1001
100000000000010001
111111111111111111
8 changes: 8 additions & 0 deletions maps/nothing.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
100000000000000001
100000000000000001
100000000000000001
100000000000010001
100000000000101001
100000000000010001
111111111111111111
8 changes: 8 additions & 0 deletions maps/valid.ber
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
111111111111111111
10000000000C000001
100P00000000000C01
1000000C0000000001
1000C0000000010001
100000000000101001
1000000C0000010E01
111111111111111111
20 changes: 10 additions & 10 deletions maps_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions rectangle_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
5 changes: 3 additions & 2 deletions so_long.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 78f6cd2

Please sign in to comment.