Skip to content

Commit

Permalink
fix:(vSKAH): Fix norm errors
Browse files Browse the repository at this point in the history
add:(vSKAH): "esc" key now exit game
add:(vSKAH): Nb of movements are now print in shell
add:(vSKAH): ft_printf is now in libft
  • Loading branch information
vSKAH committed Oct 14, 2023
1 parent 8be4f2c commit f7d913f
Show file tree
Hide file tree
Showing 19 changed files with 414 additions and 129 deletions.
25 changes: 12 additions & 13 deletions errors_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ int handle_file_error(char *argv[])
int fd;

if (!argv[1])
return (ft_putstr_fd("Error\n -> Invalid file path.\n", 1), -2);
return (ft_printf("Error\n -> Invalid file path.\n"), -2);
fd = open(argv[1], O_RDONLY);
if (fd == -1)
return (ft_putstr_fd("Error\n -> Invalid file path.\n", 1), -2);
return (ft_printf("Error\n -> Invalid file path.\n"), -2);
return (fd);
}

Expand All @@ -30,34 +30,33 @@ int handle_map_error(char *path, t_game game)
int elements_code;
t_world cloned;

if (has_illegal_character(game.world)){
if (has_illegal_character(game.world))
{
free_map(&game.world);
free_collectibles(game.world.player.collectibles);
return (ft_putstr_fd("Error\n -> Invalid Map / illegal character", 1), -1);

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_putstr_fd("Error\n -> Invalid shape / not closed", 1), -1);
return (ft_printf("Error\n -> Invalid shape / not closed"), -1);
}
elements_code = valid_elements(game.world);
if (elements_code < 0)
{
ft_putstr_fd("Error\n -> Please, check your map !", 1);
ft_putstr_fd("\nThe map must contains 1 E, 1 P, minimum 1 C", 1);
ft_printf("Error\n -> Map must contains 1 E, 1 P, minimum 1 C");
return (-1);
}
load_map(open(path, O_RDONLY), path, &cloned);
is_solvable(cloned.map, game.world.player.location.x, game.world.player.location.y, game.world.length_y);
is_solvable(cloned.map, game.world.player.location.x, \
game.world.player.location.y, game.world.length_y);
if (count_element(cloned, 'C') > 0 || count_element(cloned, 'E') > 0)
{
ft_putstr_fd("Error\n -> Sorry, your map is not resolvable", 1);
free_map(&game.world);
ft_printf("Error\n -> Sorry, your map is not resolvable");
free_collectibles(game.world.player.collectibles);
return (free_map(&cloned), -2);
return (free_map(&game.world), free_map(&cloned), -2);
}
free_map(&cloned);
return (0);
}
}
2 changes: 1 addition & 1 deletion frees.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int free_unavailable_texture(t_game game)
{
if (has_unavailable_texture(game.textures))
{
ft_putstr_fd("Error\n -> Textures can't be loaded.", 1);
ft_printf("Error\n -> Textures can't be loaded.");
free_map(&game.world);
free_textures(&game);
free_collectibles(game.world.player.collectibles);
Expand Down
126 changes: 67 additions & 59 deletions libft/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,80 +10,88 @@
# #
# **************************************************************************** #

NAME = libft.a
RM = rm -rf
CC = cc
FLAGS = -Wall -Wextra -Werror -g
OBJ_DIRECTORY = ./.obj/
INCLUDE = libft.h
NAME = libft.a
RM = rm -rf
CC = gcc
FLAGS = -Wall -Wextra -Werror -g3
OBJ_DIRECTORY = ./.obj/
INCLUDE = libft.h

SRCS = \
ft_bzero.c \
ft_isalnum.c \
ft_isalpha.c \
ft_isascii.c \
ft_isdigit.c \
ft_isprint.c \
ft_memset.c \
ft_strlen.c \
ft_strlcat.c \
ft_tolower.c \
ft_toupper.c \
ft_strrchr.c \
ft_strchr.c \
ft_strncmp.c \
ft_memchr.c \
ft_memcmp.c \
ft_strnstr.c \
ft_atoi.c \
ft_calloc.c \
ft_strdup.c \
ft_memcpy.c \
ft_memmove.c \
ft_strlcpy.c \
ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \
ft_split.c \
ft_itoa.c \
ft_putchar_fd.c\
ft_putstr_fd.c \
ft_putendl_fd.c\
ft_striteri.c\
ft_strmapi.c\
ft_putnbr_fd.c \
ft_lstnew.c \
ft_lstadd_front.c\
ft_lstsize.c \
ft_lstlast.c \
ft_lstadd_back.c \
ft_lstdelone.c \
ft_lstclear.c \
ft_lstiter.c \
get_next_line.c \
get_next_line_utils.c \
SRCS = \
ft_bzero.c \
ft_isalnum.c \
ft_isalpha.c \
ft_isascii.c \
ft_isdigit.c \
ft_isprint.c \
ft_memset.c \
ft_strlen.c \
ft_strlcat.c \
ft_tolower.c \
ft_toupper.c \
ft_strrchr.c \
ft_strchr.c \
ft_strncmp.c \
ft_memchr.c \
ft_memcmp.c \
ft_strnstr.c \
ft_atoi.c \
ft_calloc.c \
ft_strdup.c \
ft_memcpy.c \
ft_memmove.c \
ft_strlcpy.c \
ft_substr.c \
ft_strjoin.c \
ft_strtrim.c \
ft_split.c \
ft_itoa.c \
ft_putchar_fd.c \
ft_putstr_fd.c \
ft_putendl_fd.c \
ft_striteri.c \
ft_strmapi.c \
ft_putnbr_fd.c \
ft_lstnew.c \
ft_lstadd_front.c \
ft_lstsize.c \
ft_lstlast.c \
ft_lstadd_back.c \
ft_lstdelone.c \
ft_lstclear.c \
ft_lstiter.c \
ft_base.c \
get_next_line.c \
get_next_line_utils.c \
ft_printf/ft_printf.c \
ft_printf/ft_args_handler.c \
ft_printf/ft_get_address.c \
ft_printf/ft_get_base16.c \
ft_printf/ft_get_char.c \
ft_printf/ft_get_integer.c \
ft_printf/ft_get_string.c \

all: $(NAME)
OBJS = $(addprefix $(OBJ_DIRECTORY), $(SRCS:.c=.o))

so: $(all)
all: $(NAME)

OBJS = $(addprefix $(OBJ_DIRECTORY), $(SRCS:.c=.o))
so: $(all)

$(NAME): $(OBJ_DIRECTORY) $(OBJS) $(INCLUDE)
ar -rcs $(NAME) $(OBJS)

$(OBJ_DIRECTORY)%.o:%.c $(INCLUDE)
$(OBJ_DIRECTORY)%.o: %.c $(INCLUDE) Makefile
$(CC) $(FLAGS) -I. -c $< -o $@

$(OBJ_DIRECTORY):
mkdir -p $(OBJ_DIRECTORY)
mkdir -p $(OBJ_DIRECTORY) $(OBJ_DIRECTORY)ft_printf

clean:
$(RM) $(OBJ_DIRECTORY)

fclean: clean
fclean: clean
$(RM) $(NAME) *.out *.gch *.o .so

re: fclean $(NAME)
re: fclean $(NAME)

.PHONY: fclean clean all re
.PHONY: fclean clean all re
32 changes: 32 additions & 0 deletions libft/ft_base.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/14 15:22:00 by jbadaire #+# #+# */
/* Updated: 2023/10/14 15:22:00 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

int ft_base(unsigned long number, int base_number, char base_array[], int value)
{
char character;

value = 0;
if (number >= (unsigned long)base_number)
{
value += ft_base(number / base_number, base_number, base_array, value);
value += ft_base(number % base_number, base_number, base_array, value);
}
else
{
character = base_array[number];
value += ft_putchar_fd(character, 1);
return (value);
}
return (value);
}
37 changes: 37 additions & 0 deletions libft/ft_printf/ft_args_handler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* args_handler.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/06 18:39:32 by jbadaire #+# #+# */
/* Updated: 2023/09/18 08:23:56 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "../libft.h"

int ft_args_handler(char c, va_list params)
{
int value;

value = 0;
if (c == 'c')
value = ft_get_char(params);
else if (c == 's')
value = ft_get_string(params);
else if (c == 'u')
value = ft_get_unsigned_integer(params);
else if (c == 'd' || c == 'i')
value = ft_get_integer(params);
else if (c == 'x')
value = ft_get_base16(params, "0123456789abcdef");
else if (c == 'X')
value = ft_get_base16(params, "0123456789ABCDEF");
else if (c == 'p')
value = ft_get_address(params, "0123456789abcdef");
else if (c == '%')
value = ft_putchar_fd('%', 1);
return (value);
}
27 changes: 27 additions & 0 deletions libft/ft_printf/ft_get_address.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_address.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/07 18:21:53 by jbadaire #+# #+# */
/* Updated: 2023/09/18 08:54:49 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "../libft.h"

int ft_get_address(va_list param, char *base_array)
{
size_t number;
int value;

number = va_arg(param, size_t);
value = 0;
if (number == 0)
return (ft_putstr_fd("(nil)", 1));
value += ft_putstr_fd("0x", 1);
value += ft_base(number, 16, base_array, 0);
return (value);
}
21 changes: 21 additions & 0 deletions libft/ft_printf/ft_get_base16.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_base16.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/07 18:10:35 by jbadaire #+# #+# */
/* Updated: 2023/09/12 14:58:19 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "../libft.h"

int ft_get_base16(va_list param, char *base)
{
unsigned int number;

number = va_arg(param, unsigned int);
return (ft_base(number, 16, base, 0));
}
21 changes: 21 additions & 0 deletions libft/ft_printf/ft_get_char.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/06 18:47:16 by jbadaire #+# #+# */
/* Updated: 2023/09/12 13:56:35 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "../libft.h"

int ft_get_char(va_list param)
{
char c;

c = va_arg(param, int);
return (ft_putchar_fd(c, 1));
}
32 changes: 32 additions & 0 deletions libft/ft_printf/ft_get_integer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_integer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/07 18:18:42 by jbadaire #+# #+# */
/* Updated: 2023/09/12 13:57:52 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */

#include "../libft.h"

int ft_get_integer(va_list param)
{
int value;

value = va_arg(param, int);
return (ft_putnbr_fd(value, 1));
}

int ft_get_unsigned_integer(va_list param)
{
unsigned int value;
int return_value;

value = va_arg(param, unsigned int);
return_value = 0;
return_value += ft_put_unsigned_nbr_fd(value, 1);
return (return_value);
}
Loading

0 comments on commit f7d913f

Please sign in to comment.