diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d0e5d7f --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: apommier +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2022/02/13 16:27:49 by apommier #+# #+# # +# Updated: 2022/02/13 21:59:15 by apommier ### ########.fr # +# # +# **************************************************************************** # + +NAME = so_long +SRCS = main.c\ + parsing.c +OBJS = ${SRCS:.c=.o} +CC = gcc +LIB = -L ./mlx -lmlx -lXext -lX11 +INC = -I ./inc/ -I ./mlx +CFLAGS = -Wall -Wextra -Werror +RM = rm -rf +LIBFT = ./libft + +${NAME}:${OBJS} + make bonus -C ${LIBFT} + gcc -g ${OBJS} ${LIBFT}/libft.a ${LIB} -o so_long + +all: ${NAME} bonus + +clean: + ${RM} ${OBJS} + make clean -C ${LIBFT} + +fclean: clean + ${RM} ${NAME} + make fclean -C ${LIBFT} + +re: fclean all + +.PHONY: all clean fclean re bonus \ No newline at end of file diff --git a/cmd b/cmd deleted file mode 100644 index 3fa5765..0000000 --- a/cmd +++ /dev/null @@ -1,3 +0,0 @@ -gcc -I ./mlx -I ./libft -o main.o -c main.c -gcc -o main main.o -L ./mlx -lmlx -lXext -lX11 -L ./libft -lft - diff --git a/libft/ft_atoi.o b/libft/ft_atoi.o deleted file mode 100644 index bd02446..0000000 Binary files a/libft/ft_atoi.o and /dev/null differ diff --git a/libft/ft_bzero.o b/libft/ft_bzero.o deleted file mode 100644 index 07a6af3..0000000 Binary files a/libft/ft_bzero.o and /dev/null differ diff --git a/libft/ft_calloc.o b/libft/ft_calloc.o deleted file mode 100644 index e13f9d6..0000000 Binary files a/libft/ft_calloc.o and /dev/null differ diff --git a/libft/ft_isalnum.o b/libft/ft_isalnum.o deleted file mode 100644 index 3adcfbe..0000000 Binary files a/libft/ft_isalnum.o and /dev/null differ diff --git a/libft/ft_isalpha.o b/libft/ft_isalpha.o deleted file mode 100644 index 974f39c..0000000 Binary files a/libft/ft_isalpha.o and /dev/null differ diff --git a/libft/ft_isascii.o b/libft/ft_isascii.o deleted file mode 100644 index 13e6e60..0000000 Binary files a/libft/ft_isascii.o and /dev/null differ diff --git a/libft/ft_isdigit.o b/libft/ft_isdigit.o deleted file mode 100644 index a403532..0000000 Binary files a/libft/ft_isdigit.o and /dev/null differ diff --git a/libft/ft_isprint.o b/libft/ft_isprint.o deleted file mode 100644 index 625565e..0000000 Binary files a/libft/ft_isprint.o and /dev/null differ diff --git a/libft/ft_itoa.o b/libft/ft_itoa.o deleted file mode 100644 index 2ba0bdf..0000000 Binary files a/libft/ft_itoa.o and /dev/null differ diff --git a/libft/ft_memccpy.o b/libft/ft_memccpy.o deleted file mode 100644 index 02b82f6..0000000 Binary files a/libft/ft_memccpy.o and /dev/null differ diff --git a/libft/ft_memchr.o b/libft/ft_memchr.o deleted file mode 100644 index d96b5e1..0000000 Binary files a/libft/ft_memchr.o and /dev/null differ diff --git a/libft/ft_memcmp.o b/libft/ft_memcmp.o deleted file mode 100644 index a02b8ae..0000000 Binary files a/libft/ft_memcmp.o and /dev/null differ diff --git a/libft/ft_memcpy.o b/libft/ft_memcpy.o deleted file mode 100644 index 01d853b..0000000 Binary files a/libft/ft_memcpy.o and /dev/null differ diff --git a/libft/ft_memmove.o b/libft/ft_memmove.o deleted file mode 100644 index deae8a1..0000000 Binary files a/libft/ft_memmove.o and /dev/null differ diff --git a/libft/ft_memset.o b/libft/ft_memset.o deleted file mode 100644 index cc22010..0000000 Binary files a/libft/ft_memset.o and /dev/null differ diff --git a/libft/ft_putchar_fd.o b/libft/ft_putchar_fd.o deleted file mode 100644 index 7b5993d..0000000 Binary files a/libft/ft_putchar_fd.o and /dev/null differ diff --git a/libft/ft_putendl_fd.o b/libft/ft_putendl_fd.o deleted file mode 100644 index e299d4d..0000000 Binary files a/libft/ft_putendl_fd.o and /dev/null differ diff --git a/libft/ft_putnbr_fd.o b/libft/ft_putnbr_fd.o deleted file mode 100644 index a1b9ade..0000000 Binary files a/libft/ft_putnbr_fd.o and /dev/null differ diff --git a/libft/ft_putstr_fd.o b/libft/ft_putstr_fd.o deleted file mode 100644 index d95d8d6..0000000 Binary files a/libft/ft_putstr_fd.o and /dev/null differ diff --git a/libft/ft_split.o b/libft/ft_split.o deleted file mode 100644 index d99b940..0000000 Binary files a/libft/ft_split.o and /dev/null differ diff --git a/libft/ft_strchr.o b/libft/ft_strchr.o deleted file mode 100644 index 42dd88f..0000000 Binary files a/libft/ft_strchr.o and /dev/null differ diff --git a/libft/ft_strdup.o b/libft/ft_strdup.o deleted file mode 100644 index 0c96dc8..0000000 Binary files a/libft/ft_strdup.o and /dev/null differ diff --git a/libft/ft_strjoin.o b/libft/ft_strjoin.o deleted file mode 100644 index e3b3fda..0000000 Binary files a/libft/ft_strjoin.o and /dev/null differ diff --git a/libft/ft_strlcat.o b/libft/ft_strlcat.o deleted file mode 100644 index 5e01a5a..0000000 Binary files a/libft/ft_strlcat.o and /dev/null differ diff --git a/libft/ft_strlcpy.o b/libft/ft_strlcpy.o deleted file mode 100644 index b690afd..0000000 Binary files a/libft/ft_strlcpy.o and /dev/null differ diff --git a/libft/ft_strlen.o b/libft/ft_strlen.o deleted file mode 100644 index b58b4b1..0000000 Binary files a/libft/ft_strlen.o and /dev/null differ diff --git a/libft/ft_strmapi.o b/libft/ft_strmapi.o deleted file mode 100644 index 7952445..0000000 Binary files a/libft/ft_strmapi.o and /dev/null differ diff --git a/libft/ft_strncmp.o b/libft/ft_strncmp.o deleted file mode 100644 index 2bda454..0000000 Binary files a/libft/ft_strncmp.o and /dev/null differ diff --git a/libft/ft_strnstr.o b/libft/ft_strnstr.o deleted file mode 100644 index 710f7d2..0000000 Binary files a/libft/ft_strnstr.o and /dev/null differ diff --git a/libft/ft_strrchr.o b/libft/ft_strrchr.o deleted file mode 100644 index b5043c9..0000000 Binary files a/libft/ft_strrchr.o and /dev/null differ diff --git a/libft/ft_strtrim.o b/libft/ft_strtrim.o deleted file mode 100644 index 3dece19..0000000 Binary files a/libft/ft_strtrim.o and /dev/null differ diff --git a/libft/ft_substr.o b/libft/ft_substr.o deleted file mode 100644 index e1a5dab..0000000 Binary files a/libft/ft_substr.o and /dev/null differ diff --git a/libft/ft_tolower.o b/libft/ft_tolower.o deleted file mode 100644 index 73e4ed4..0000000 Binary files a/libft/ft_tolower.o and /dev/null differ diff --git a/libft/ft_toupper.o b/libft/ft_toupper.o deleted file mode 100644 index 5818e5c..0000000 Binary files a/libft/ft_toupper.o and /dev/null differ diff --git a/libft/get_next_line.o b/libft/get_next_line.o deleted file mode 100644 index 46cf0ce..0000000 Binary files a/libft/get_next_line.o and /dev/null differ diff --git a/libft/get_next_line_utils.o b/libft/get_next_line_utils.o deleted file mode 100644 index 2ca9c14..0000000 Binary files a/libft/get_next_line_utils.o and /dev/null differ diff --git a/libft/libft.a b/libft/libft.a deleted file mode 100644 index 03fce54..0000000 Binary files a/libft/libft.a and /dev/null differ diff --git a/main b/main deleted file mode 100755 index 370b65a..0000000 Binary files a/main and /dev/null differ diff --git a/main.c b/main.c index 233e006..b841084 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,16 @@ -//#include +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/13 16:42:55 by apommier #+# #+# */ +/* Updated: 2022/02/13 22:02:38 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "so_long.h" #include "./mlx/mlx.h" #include "./mlx/mlx_int.h" #include "./libft/libft.h" @@ -7,7 +19,6 @@ #include #include #include -//#include "mlx_int.h" typedef struct s_data { void *mlx; @@ -18,29 +29,30 @@ typedef struct s_data { int bear; } t_data; +size_t ft_strlen_double(char **s) +{ + size_t i; + + i = 0; + if (!s) + return (0); + while (s[i]) + i++; + return (i); +} + void quit_game(t_data *img) { int i; - int j; - int *image; i = 0; - j = 0; - printf("quit game\n"); - //image = mlx_new_image(img->mlx, 13 * 32, 10 * 32); - //mlx_put_image_to_window(img->mlx, img->mlx_win, image, 0, 0) mlx_clear_window(img->mlx, img->mlx_win); mlx_destroy_window(img->mlx, img->mlx_win); - - mlx_destroy_display(img->mlx); if (img->mlx) free(img->mlx); - //if (img->mlx_win) - // free(img->mlx_win); while (img->map_tab[i]) free(img->map_tab[i++]); - //if (img->map_tab); free(img->map_tab); exit(1); } @@ -82,9 +94,7 @@ void print_case(char type, t_data *img, int y, int x) else buffer = mlx_xpm_file_to_image(img->mlx, "./sprite/back.xpm", &img_width, &img_height); mlx_put_image_to_window(img->mlx, img->mlx_win, buffer, x * 32, y * 32); - //free(buffer); mlx_destroy_image(img->mlx, buffer); - //free(buffer); } void print_map(char **map, t_data *img) @@ -109,40 +119,30 @@ void print_map(char **map, t_data *img) } } -int change_map(t_data *img, int y, int x, char *change) +void change_map(t_data *img, int y, int x, char *change) { if (*change == '0' || *change == 'C') { if (*change == 'C') img->item--; img->move++; - printf("%d\n", img->move); + ft_putnbr_fd(img->move, 1); + ft_putchar_fd('\n', 1); img->map_tab[y][x] = '0'; *change = 'P'; } else if (*change == 'E') { - img->move++; - printf("%d\n", img->move); if (img->item <= 0) + { + img->move++; + ft_putnbr_fd(img->move, 1); + ft_putchar_fd('\n', 1); quit_game(img); + } } - } -/*void choose_bear(t_data *img, int type) -{ - img->bear = 0; - if (type == 119) - ; - else if (type == 97) - change_map(img, j, i, &map[j][i - 1]); - else if (type == 115) - change_map(img, j, i, &map[j + 1][i]); - else if (type == 100) - change_map(img, j, i, &map[j][i + 1]); -}*/ - int is_good(char **map, int type, t_data *img) { int j; @@ -169,31 +169,29 @@ int is_good(char **map, int type, t_data *img) change_map(img, j, i, &map[j][i + 1]); else return (0); - return 1; + return (1); } -int test(int code, t_data *img) +int key_press(int code, t_data *img) { - printf("code = %d\n", code); if (code == 65307) quit_game(img); is_good(img->map_tab, code, img); print_map(img->map_tab, img); + return (1); } int main(int argc, char **argv) { t_data img; char *map = 0; - char **map_tab; int fd = 0; char *swap = 0; char *del; + (void)argc; img.bear = 0; img.move = 0; - img.mlx = mlx_init(); - img.mlx_win = mlx_new_window(img.mlx, 13 * 32, 10 * 32, "Hungry Bear"); fd = open(argv[1], O_RDONLY); swap = get_next_line(fd); while (swap) @@ -205,29 +203,12 @@ int main(int argc, char **argv) free(del); } img.map_tab = ft_split(map, '\n'); + check_map(img.map_tab); + img.mlx = mlx_init(); + img.mlx_win = mlx_new_window(img.mlx, ft_strlen(img.map_tab[0]) * 32, + ft_strlen_double(img.map_tab) * 32, "Hungry Bear"); print_map(img.map_tab, &img); free(map); - mlx_key_hook(img.mlx_win, &test, &img); + mlx_hook(img.mlx_win, 2, 1L<<0, &key_press, &img); mlx_loop(img.mlx); - printf("heeeeeeeeeeeeeeerreeeee\n\n\n\n"); } - - - - - /*img.img = mlx_xpm_file_to_image(mlx, "./sprite/back.xpm", &img_width, &img_height); - if (!img.img) - return 0; - //img.addr = mlx_get_data_addr(img.img, &img.bits_per_pixel, &img.line_length, &img.endian); - //my_mlx_pixel_put(&img, 5, 5, 0x00FF0000); - /*while (i != 320) - { - j = 0; - while (j != 320) - { - mlx_put_image_to_window(mlx, mlx_win, img.img, i, j); - j += 32; - } - i += 32; - }*/ - //offset = (1920 * line_length + 1080 * (bits_per_pixel / 8)); \ No newline at end of file diff --git a/parsing.c b/parsing.c new file mode 100644 index 0000000..a33e3bf --- /dev/null +++ b/parsing.c @@ -0,0 +1,169 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: map0map/0map/13 16:map1:43 by apommier #+# #+# */ +/* Updated: map0map/0map/13 map1:map4:44 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + + +#include "so_long.h" +#include "./libft/libft.h" +#include + + +int is_player(char **map) +{ + int i; + int j; + int player; + + player = 0; + i = 0; + j = 0; + while (map[j]) + { + i = 0; + while (map[j][i++]) + { + if (map[j][i] == 'P') + player++; + } + j++; + } + if (player == 1) + return (1); + return (0); +} + +int is_exit(char **map) +{ + int i; + int j; + int exit; + + exit = 0; + i = 0; + j = 0; + while (map[j]) + { + i = 1; + while (map[j][i]) + { + if (map[j][i++] == 'E') + exit++; + } + j++; + } + if (exit > 0) + return (1); + return (0); + +} + +int is_item(char **map) +{ + int i; + int j; + int item; + + item = 0; + i = 0; + j = 0; + while (map[j]) + { + i = 1; + while (map[j][i]) + { + if (map[j][i++] == 'E') + item++; + } + j++; + } + return (item); +} + +int is_rectangle(char **map) +{ + int i; + int j; + + i = 0; + j = 0; + while (map[j]) + { + i = 1; + while (map[j][i]) + { + if (map[j][i] != '0' && map[j][i] != '1' && map[j][i] != 'P' && map[j][i] != 'E' && map[j][i] != 'C') + return (0); + i++; + } + j++; + } + j = 0; + while (map[j + 1]) + { + if (ft_strlen(map[j]) != ft_strlen(map[j + 1])) + return (0); + j++; + } + return (1); +} + +int is_close(char **map) +{ + int i; + int j; + + i = 0; + j = 0; + while (map[j][i]) + { + if (map[j][i++] != '1') + return (0); + } + while (map[j]) + { + if (map[j][0] != '1' || map[j][ft_strlen(map[j]) - 1] != '1') + return (0); + j++; + } + i = 0; + while (map[j - 1][i]) + { + if (map[j - 1][i++] != '1') + return (0); + } + return (1); +} + +void ft_error(char *error_msg, char **map) +{ + int i; + + i = 0; + while (map[i]) + free(map[i++]); + ft_putstr_fd(error_msg, 2); + exit(1); +} + +int check_map(char **map) +{ + if (!is_player(map)) + ft_error("You need just one player\n", map); + if (!is_exit(map)) + ft_error("There is no exit\n", map); + if (!is_close(map)) + ft_error("The map isn't close\n", map); + if (!is_item(map)) + ft_error("There is no item\n", map); + if (!is_rectangle(map)) + ft_error("The map isn't a rectangle\n", map); + return (1); +} \ No newline at end of file diff --git a/so_long.h b/so_long.h new file mode 100644 index 0000000..ddd90f9 --- /dev/null +++ b/so_long.h @@ -0,0 +1,13 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* so_long.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */ +/* Updated: 2022/02/13 19:14:29 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +int check_map(char **map); \ No newline at end of file