From 5e57f4407137a2ab4b89fd7235ced2a6079f331a Mon Sep 17 00:00:00 2001 From: Etienne Rey-bethbeder Date: Sun, 1 Jan 2023 19:50:24 +0100 Subject: [PATCH] Avancement --- Makefile | 2 +- games/enemy.c | 22 ++++++++++------------ games/init.c | 29 ++++++++++++++++++++++++++--- games/map.c | 19 ++++++++++++------- games/player.c | 15 +++++++++++++++ games/position.c | 12 ++++++++++++ games/quit.c | 12 ++++++++++++ games/render.c | 16 +++++++++++++--- libft/ft_strdups.c | 4 ++-- libft/libft.h | 6 +++--- so_long.c | 14 -------------- 11 files changed, 106 insertions(+), 45 deletions(-) delete mode 100755 so_long.c diff --git a/Makefile b/Makefile index b680e48..c489bd3 100644 --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ SRCS = games/init.c games/map.c games/player.c games/position.c games/quit.c gam OBJS = ${SRCS:.c=.o} LIBS = libft/libft.a minilibx-linux/libmlx.a CC = clang -CFLAGS = -gdwarf-4 -g -Wall -Wextra -Werror +CFLAGS = -g -Wall -Wextra -Werror NAME = so_long # PENSER ENLEVER -G diff --git a/games/enemy.c b/games/enemy.c index a71aee5..56869c7 100644 --- a/games/enemy.c +++ b/games/enemy.c @@ -1,22 +1,20 @@ -int pathfinding(char **map, char **map_cpy, int x, int y, int *check) +int path_finding(char **map_cpy, t_xy cur, t_xy ply, int nbr) { - if (map[x][y] == 'E') - return (1); + if (cur.x == ply.x && cur.y == ply.y) + return (nbr); map_cpy[x][y] = 'x'; if (map_cpy[x][y + 1] != '1' && map_cpy[x][y + 1] != 'x') - if (is_possible(map, map_cpy, x, y + 1, check)) - *check = 1; + if (is_possible(map, map_cpy, x, y + 1, nbr++)) + if (map_cpy[x + 1][y] != '1' && map_cpy[x + 1][y] != 'x') - if (is_possible(map, map_cpy, x + 1, y, check)) - *check = 1; + if (is_possible(map, map_cpy, x + 1, y, nbr)) + if (map_cpy[x - 1][y] != '1' && map_cpy[x - 1][y] != 'x') - if (is_possible(map, map_cpy, x - 1, y, check)) - *check = 1; + if (is_possible(map, map_cpy, x - 1, y, nbr)) + *check = if (map_cpy[x][y - 1] != '1' && map_cpy[x][y - 1] != 'x') - if (is_possible(map, map_cpy, x, y - 1, check)) + if (is_possible(map, map_cpy, x, y - 1, nbr)) *check = 1; - if (!has_element(map_cpy, 'C') && *check) - return (1); return (0); } diff --git a/games/init.c b/games/init.c index 6d01a5d..89c1f98 100644 --- a/games/init.c +++ b/games/init.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/29 18:06:18 by erey-bet #+# #+# */ +/* Updated: 2023/01/01 19:49:56 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../so_long.h" int key_hook(int key, t_data *data) @@ -61,18 +73,28 @@ int trgb(int t, int r, int g, int b) int render(t_data *data) { + char *str_move; + t_xy pos; + /*if (data->tick >= 5 && data->x_ene != -1) { data->tick = 0; move_enemy(data); } - else + else if (data->x_ene != -1) data->tick++;*/ if (data->update == 1) { + if (has_element(data->map, '3')) + { + pos = get_position(data->map, '3'); + + } draw(data); mlx_put_image_to_window(data->mlx, data->mlx_win, data->imgs.bg, 0, 0); - mlx_string_put(data->mlx, data->mlx_win, 100, 100, trgb(0, 150, 0, 0), ft_itoa(data->move)); + str_move = ft_itoa(data->move); + mlx_string_put(data->mlx, data->mlx_win, 100, 100, trgb(0, 150, 0, 0), str_move); + free(str_move); data->update = 0; } return (0); @@ -117,7 +139,8 @@ int main(int argc, char *argv[]) { t_data data; - (void)argc; + if (argc != 2) + return (0); (void)argv; init_data(&data); if (get_map(argv, &data) == NULL) diff --git a/games/map.c b/games/map.c index 3e93fa8..9d65ead 100644 --- a/games/map.c +++ b/games/map.c @@ -7,6 +7,11 @@ void *read_map(char *argv[], t_data *data) int i; int fd; + if (argv[1][ft_strlen(argv[1]) - 1] != 'r' || + argv[1][ft_strlen(argv[1]) - 2] != 'e' || + argv[1][ft_strlen(argv[1]) - 3] != 'b' || + argv[1][ft_strlen(argv[1]) - 4] != '.') + return (NULL); fd = open(argv[1], O_RDONLY); if (fd == -1) return NULL; @@ -83,22 +88,22 @@ int is_surrounded_by_wall(char **map, int h_map) return (1); } -int is_possible(char **map, char **map_cpy, int x, int y, int *check) +int is_possible(char **map_cpy, int x, int y, int *check) { - if (map[x][y] == 'E') + if (map_cpy[x][y] == 'E') return (1); map_cpy[x][y] = 'x'; if (map_cpy[x][y + 1] != '1' && map_cpy[x][y + 1] != 'x') - if (is_possible(map, map_cpy, x, y + 1, check)) + if (is_possible(map_cpy, x, y + 1, check)) *check = 1; if (map_cpy[x + 1][y] != '1' && map_cpy[x + 1][y] != 'x') - if (is_possible(map, map_cpy, x + 1, y, check)) + if (is_possible(map_cpy, x + 1, y, check)) *check = 1; if (map_cpy[x - 1][y] != '1' && map_cpy[x - 1][y] != 'x') - if (is_possible(map, map_cpy, x - 1, y, check)) + if (is_possible(map_cpy, x - 1, y, check)) *check = 1; if (map_cpy[x][y - 1] != '1' && map_cpy[x][y - 1] != 'x') - if (is_possible(map, map_cpy, x, y - 1, check)) + if (is_possible(map_cpy, x, y - 1, check)) *check = 1; if (!has_element(map_cpy, 'C') && *check) return (1); @@ -156,7 +161,7 @@ void *get_map(char *argv[], t_data *data) data = set_position_player(data, get_position(data->map, 'P')); i = 0; map_cpy = ft_strdups(data->map); - if (!is_possible(data->map, map_cpy, data->y_player, data->x_player, &i)) + if (!is_possible(map_cpy, data->y_player, data->x_player, &i)) { write(1, "Error\nLa carte est impossible a faire", 37); return (NULL); diff --git a/games/player.c b/games/player.c index 7f575ce..7f26039 100644 --- a/games/player.c +++ b/games/player.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/29 18:06:32 by erey-bet #+# #+# */ +/* Updated: 2023/01/01 19:39:28 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../so_long.h" static int get_new_element(t_data *data, int x, int y) @@ -8,6 +20,9 @@ static int get_new_element(t_data *data, int x, int y) static void set_new_element(t_data *data, int x, int y) { data->map[data->y_player + y][data->x_player + x] = '0'; + if (!has_element(data->map, 'C')) + data->map[data->y_player + y][data->x_player + x] = '3'; + } void *set_position_player(t_data *data, t_xy *xy) diff --git a/games/position.c b/games/position.c index 9616c2a..ba25158 100644 --- a/games/position.c +++ b/games/position.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* position.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/29 18:06:59 by erey-bet #+# #+# */ +/* Updated: 2022/12/29 18:07:01 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../so_long.h" t_xy *get_position(char **map, char c) diff --git a/games/quit.c b/games/quit.c index 16d9d14..ee065bc 100644 --- a/games/quit.c +++ b/games/quit.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* quit.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/29 18:06:50 by erey-bet #+# #+# */ +/* Updated: 2022/12/29 18:06:53 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../so_long.h" int end(t_data *data) diff --git a/games/render.c b/games/render.c index 6b46305..57f37a2 100644 --- a/games/render.c +++ b/games/render.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/12/29 18:06:42 by erey-bet #+# #+# */ +/* Updated: 2023/01/01 19:39:55 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "../so_long.h" static void *assets(t_data *data, int x, int y) @@ -9,15 +21,13 @@ static void *assets(t_data *data, int x, int y) dist = sqrt(pow(x - data->x_player, 2) + pow(y - data->y_player, 2)) - 1; if (dist > 2) dist = 2; - if (c == '0' || c == 'P') - return (data->imgs.bg); else if (c == '1') return (data->imgs.wall[dist]); else if (c == 'C') return (data->imgs.col[dist]); else if (c == 'E') return (data->imgs.exit[dist]); - return (NULL); + return (data->imgs.bg); } void draw(t_data *data) diff --git a/libft/ft_strdups.c b/libft/ft_strdups.c index 37d3e48..56d22ed 100644 --- a/libft/ft_strdups.c +++ b/libft/ft_strdups.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_strdups.c :+: :+: :+: */ +/* ft_strdups.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/07/26 14:25:30 by erey-bet #+# #+# */ -/* Updated: 2022/09/28 16:32:55 by erey-bet ### ########.fr */ +/* Updated: 2022/12/29 18:05:24 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/libft/libft.h b/libft/libft.h index d4ff729..651b5a2 100644 --- a/libft/libft.h +++ b/libft/libft.h @@ -43,7 +43,7 @@ int ft_strncmp(const char *s1, const char *s2, size_t n); size_t ft_strlcat(char *dest, const char *src, size_t size); char *ft_strdup(const char *src); char *ft_strdup_free(char *src); -char **ft_strdups(char **src); +char **ft_strdups(char **src); int ft_atoi(const char *nptr); char **ft_split(char const *s, char c); void *ft_calloc(size_t nitems, size_t size); @@ -69,6 +69,6 @@ void ft_lstdelone(t_list *lst, void (*del)(void*)); void ft_lstclear(t_list **lst, void (*del)(void*)); void ft_lstiter(t_list *lst, void (*f)(void *)); t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); -int ft_atoi_check(const char *nptr); -int ft_strlen_double(char **strs); +int ft_atoi_check(const char *nptr); +int ft_strlen_double(char **strs); #endif diff --git a/so_long.c b/so_long.c deleted file mode 100755 index 6f4d456..0000000 --- a/so_long.c +++ /dev/null @@ -1,14 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* so_long.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: erey-bet +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/12/09 14:18:18 by erey-bet #+# #+# */ -/* Updated: 2022/12/14 17:15:45 by erey-bet ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "so_long.h" -#include