FEAT: add vector, fix standard input

This commit is contained in:
LucasCodeur
2026-03-29 15:42:32 +02:00
parent 4150e95378
commit 3e599160d8
57 changed files with 961 additions and 56 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+10 -8
View File
@@ -6,7 +6,7 @@
# By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ # # By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2025/07/18 12:51:21 by lud-adam #+# #+# # # Created: 2025/07/18 12:51:21 by lud-adam #+# #+# #
# Updated: 2026/03/28 15:07:38 by lud-adam ### ########.fr # # Updated: 2026/03/29 14:43:31 by lud-adam ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@@ -22,13 +22,15 @@ P_OBJ = .obj/
P_INC = inc/ P_INC = inc/
P_LIBFT = libft/ P_LIBFT = libft/
SRC = main.c \ SRC = \
get_next_line.c \ main.c \
fill_array.c \ get_next_line.c \
check_input.c \ fill_vector.c \
print_board.c \ check_input.c \
ai.c \ print_board.c \
game.c ai.c \
game.c \
vector.c
SRCS = $(addprefix $(P_SRC), $(SRC)) SRCS = $(addprefix $(P_SRC), $(SRC))
OBJS = $(patsubst $(P_SRC)%.c,$(P_OBJ)%.o,$(SRCS)) OBJS = $(patsubst $(P_SRC)%.c,$(P_OBJ)%.o,$(SRCS))
BIN
View File
Binary file not shown.
+669 -6
View File
@@ -7,8 +7,8 @@
"-Wextra", "-Wextra",
"-Werror", "-Werror",
"-MMD", "-MMD",
"-I", "-Iinc/",
"inc/", "-Ilibft/",
"-c", "-c",
"src/main.c", "src/main.c",
"-o", "-o",
@@ -24,8 +24,8 @@
"-Wextra", "-Wextra",
"-Werror", "-Werror",
"-MMD", "-MMD",
"-I", "-Iinc/",
"inc/", "-Ilibft/",
"-c", "-c",
"src/fill_array.c", "src/fill_array.c",
"-o", "-o",
@@ -41,13 +41,676 @@
"-Wextra", "-Wextra",
"-Werror", "-Werror",
"-MMD", "-MMD",
"-I", "-Iinc/",
"inc/", "-Ilibft/",
"-c", "-c",
"src/check_input.c", "src/check_input.c",
"-o", "-o",
".obj/check_input.o" ".obj/check_input.o"
], ],
"file": "src/check_input.c" "file": "src/check_input.c"
},
{
"directory": "/home/lud-adam/Documents/rush",
"arguments": [
"cc",
"-Wall",
"-Wextra",
"-Werror",
"-MMD",
"-Iinc/",
"-Ilibft/",
"-c",
"src/get_next_line.c",
"-o",
".obj/get_next_line.o"
],
"file": "src/get_next_line.c"
},
{
"directory": "/home/lud-adam/Documents/rush",
"arguments": [
"cc",
"-Wall",
"-Wextra",
"-Werror",
"-MMD",
"-Iinc/",
"-Ilibft/",
"-c",
"src/print_board.c",
"-o",
".obj/print_board.o"
],
"file": "src/print_board.c"
},
{
"directory": "/home/lud-adam/Documents/rush",
"arguments": [
"cc",
"-Wall",
"-Wextra",
"-Werror",
"-MMD",
"-Iinc/",
"-Ilibft/",
"-c",
"src/ai.c",
"-o",
".obj/ai.o"
],
"file": "src/ai.c"
},
{
"directory": "/home/lud-adam/Documents/rush",
"arguments": [
"cc",
"-Wall",
"-Wextra",
"-Werror",
"-MMD",
"-Iinc/",
"-Ilibft/",
"-c",
"src/game.c",
"-o",
".obj/game.o"
],
"file": "src/game.c"
},
{
"directory": "/home/lud-adam/Documents/rush",
"arguments": [
"cc",
"-Wall",
"-Wextra",
"-Werror",
"-MMD",
"-Iinc/",
"-Ilibft/",
"-c",
"src/vector.c",
"-o",
".obj/vector.o"
],
"file": "src/vector.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_isdigit.o",
"-c",
"ft_isdigit.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_isdigit.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_isalpha.o",
"-c",
"ft_isalpha.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_isalpha.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_isalnum.o",
"-c",
"ft_isalnum.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_isalnum.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_isascii.o",
"-c",
"ft_isascii.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_isascii.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_isprint.o",
"-c",
"ft_isprint.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_isprint.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strlen.o",
"-c",
"ft_strlen.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strlen.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_memset.o",
"-c",
"ft_memset.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_memset.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_bzero.o",
"-c",
"ft_bzero.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_bzero.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_memcpy.o",
"-c",
"ft_memcpy.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_memcpy.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_memmove.o",
"-c",
"ft_memmove.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_memmove.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strlcpy.o",
"-c",
"ft_strlcpy.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strlcpy.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strlcat.o",
"-c",
"ft_strlcat.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strlcat.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_toupper.o",
"-c",
"ft_toupper.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_toupper.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_tolower.o",
"-c",
"ft_tolower.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_tolower.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strchr.o",
"-c",
"ft_strchr.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strchr.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strrchr.o",
"-c",
"ft_strrchr.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strrchr.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strncmp.o",
"-c",
"ft_strncmp.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strncmp.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_memcmp.o",
"-c",
"ft_memcmp.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_memcmp.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_memchr.o",
"-c",
"ft_memchr.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_memchr.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strnstr.o",
"-c",
"ft_strnstr.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strnstr.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_atoi.o",
"-c",
"ft_atoi.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_atoi.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_calloc.o",
"-c",
"ft_calloc.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_calloc.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strdup.o",
"-c",
"ft_strdup.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strdup.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_substr.o",
"-c",
"ft_substr.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_substr.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strjoin.o",
"-c",
"ft_strjoin.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strjoin.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strtrim.o",
"-c",
"ft_strtrim.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strtrim.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_split.o",
"-c",
"ft_split.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_split.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_itoa.o",
"-c",
"ft_itoa.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_itoa.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_strmapi.o",
"-c",
"ft_strmapi.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_strmapi.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_striteri.o",
"-c",
"ft_striteri.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_striteri.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_putchar_fd.o",
"-c",
"ft_putchar_fd.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_putchar_fd.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_putstr_fd.o",
"-c",
"ft_putstr_fd.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_putstr_fd.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_putendl_fd.o",
"-c",
"ft_putendl_fd.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_putendl_fd.c"
},
{
"directory": "/home/lud-adam/Documents/rush/libft",
"arguments": [
"cc",
"-MMD",
"-MP",
"-o",
"ft_putnbr_fd.o",
"-c",
"ft_putnbr_fd.c",
"-Wall",
"-Wextra",
"-Werror",
"-Ilibft.h"
],
"file": "ft_putnbr_fd.c"
} }
] ]
Binary file not shown.
+11 -7
View File
@@ -6,20 +6,24 @@
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */ /* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 13:27:29 by lud-adam #+# #+# */ /* Created: 2026/03/28 13:27:29 by lud-adam #+# #+# */
/* Updated: 2026/03/28 21:41:55 by dgaillet ### ########lyon.fr */ /* Updated: 2026/03/29 14:58:00 by lud-adam ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef ALCU_H #ifndef ALCU_H
# define ALCU_H # define ALCU_H
# include <stddef.h> #include "vector.h"
char *read_file(int fd); #include <stddef.h>
int check_input(int fd); #include <stdbool.h>
void print_board(int *game_state, size_t nb_line);
int *fill_array(int fd, int size); char *read_file(int fd);
bool check_input(t_vector* lines);
void print_board(int *game_state, size_t nb_line);
bool fill_vector(t_vector* lines, int fd);
int ai(int *gamestate, int nb_line); int ai(int *gamestate, int nb_line);
void game(int *lines, int size); void game(int *lines, int size);
int* fill_array(t_vector* lines, int* size);
#endif #endif
+1
View File
@@ -0,0 +1 @@
[]
+46
View File
@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/29 14:24:01 by lud-adam #+# #+# */
/* Updated: 2026/03/29 14:40:21 by lud-adam ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef VECTOR_H
# define VECTOR_H
#define VECTOR_INIT_CAPACITY 6
#define UNDEFINE -1
#define SUCCESS 0
#define VECTOR_INIT(vec) t_vector vec;\
vector_init(&vec)
typedef struct s_vector_list
{
void **items;
int capacity;
int total;
} t_vector_list;
typedef struct s_vector t_vector;
struct s_vector
{
t_vector_list vector_list; /**< List to store vector elements */
int (*pfVectorTotal)(t_vector *); /**< Retrieves the total number of elements in the vector */
int (*pfVectorResize)(t_vector *, int); /**< Resizes the vector to a new capacity */
int (*pfVectorAdd)(t_vector *, void *); /**< Adds an element to the vector */
int (*pfVectorSet)(t_vector *, int, void *); /**< Sets an element at a specific index in the vector */
void *(*pfVectorGet)(t_vector *, int); /**< Retrieves an element from the vector */
int (*pfVectorDelete)(t_vector *, int); /**< Deletes an element from the vector */
int (*pfVectorFree)(t_vector *); /**< Frees the memory allocated for the vector */
};
void vector_init(t_vector* v);
#endif
+11 -10
View File
@@ -12,30 +12,31 @@
#include "../libft/libft.h" #include "../libft/libft.h"
#include "get_next_line.h" #include "get_next_line.h"
#include "vector.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <stdbool.h>
int check_input(int fd) bool check_input(t_vector* lines)
{ {
int i; int i;
int j; int j;
char *line; char* line;
line = NULL;
line = get_next_line(fd);
j = 0; j = 0;
while (line != NULL) line = NULL;
while (j < lines->pfVectorTotal(lines))
{ {
line = (char*)lines->pfVectorGet(lines, j);
i = -1; i = -1;
while (line[++i]) while (line[++i])
if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n') if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n')
return (free(line), -1); return (false);
if (ft_atoi(line) < 1 || ft_atoi(line) > 10000) if (ft_atoi(line) < 1 || ft_atoi(line) > 10000)
return (free(line), -1); return (false);
free(line);
j++; j++;
line = get_next_line(fd);
} }
return (j); return (true);
} }
+29 -13
View File
@@ -1,35 +1,51 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* fill_array.c :+: :+: :+: */ /* fill_vector.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */ /* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 13:21:46 by lud-adam #+# #+# */ /* Created: 2026/03/28 13:21:46 by lud-adam #+# #+# */
/* Updated: 2026/03/28 20:53:02 by dgaillet ### ########lyon.fr */ /* Updated: 2026/03/29 14:58:09 by lud-adam ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../libft/libft.h" #include "../libft/libft.h"
#include "get_next_line.h" #include "get_next_line.h"
int *fill_array(int fd, int size) #include "alcu.h"
{
int *res; bool fill_vector(t_vector* lines, int fd)
char *line; {
int i; char *line;
res = malloc(sizeof(int) * size);
if (res == NULL)
return (NULL);
line = get_next_line(fd); line = get_next_line(fd);
i = 0;
while (line != NULL) while (line != NULL)
{ {
lines->pfVectorAdd(lines, line);
line = get_next_line(fd);
}
return (true);
}
int* fill_array(t_vector* lines, int* size)
{
int* res;
int i;
char* line;
i = 0;
line = NULL;
*size = lines->pfVectorTotal(lines);
res = malloc(sizeof(int) * *size);
if (!res)
return (NULL);
while (i < *size)
{
line = (char*)lines->pfVectorGet(lines, i);
res[i] = ft_atoi(line); res[i] = ft_atoi(line);
i++; i++;
free(line);
line = get_next_line(fd);
} }
return (res); return (res);
} }
+2
View File
@@ -69,6 +69,8 @@ static void player_turn(int *line)
if (temp) if (temp)
free(temp); free(temp);
} }
if (temp)
free(temp);
} }
static void play_turns(int *lines, int i, int *ai_turn, int size) static void play_turns(int *lines, int i, int *ai_turn, int size)
+23 -12
View File
@@ -6,14 +6,14 @@
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */ /* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/28 12:30:29 by lud-adam #+# #+# */ /* Created: 2026/03/28 12:30:29 by lud-adam #+# #+# */
/* Updated: 2026/03/28 21:41:29 by dgaillet ### ########lyon.fr */ /* Updated: 2026/03/29 14:58:10 by lud-adam ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../libft/libft.h" #include "../libft/libft.h"
#include "alcu.h" #include "alcu.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
static int get_fd(int argc, char **argv) static int get_fd(int argc, char **argv)
@@ -32,27 +32,38 @@ int main(int argc, char *argv[])
{ {
int fd; int fd;
int size; int size;
int *lines; int* lines_int;
VECTOR_INIT(lines);
fd = get_fd(argc, argv); fd = get_fd(argc, argv);
lines_int = NULL;
size = 0;
if (fd < 0) if (fd < 0)
{ {
ft_putstr_fd("ERROR", 2); ft_putstr_fd("ERROR", 2);
return (1); return (1);
} }
size = check_input(fd); fill_vector(&lines, fd);
if (size < 0) if (check_input(&lines) == false)
{ {
lines.pfVectorFree(&lines);
ft_putstr_fd("ERROR", 2);
return (1);
}
if (argc == 1)
fd = open("/dev/tty", O_RDONLY);
// // fd = get_fd(argc, argv);
lines_int = fill_array(&lines, &size);
if (!lines_int)
{
lines.pfVectorFree(&lines);
ft_putstr_fd("ERROR", 2); ft_putstr_fd("ERROR", 2);
return (1); return (1);
} }
game(lines_int, size);
close(fd); close(fd);
fd = open(argv[1], O_RDONLY); free(lines_int);
lines = fill_array(fd, size); lines.pfVectorFree(&lines);
if (!lines)
return (1);
game(lines, size);
close(fd);
free(lines);
return (0); return (0);
} }
+159
View File
@@ -0,0 +1,159 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* vector.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/03/29 14:23:01 by lud-adam #+# #+# */
/* Updated: 2026/03/29 14:50:19 by lud-adam ### ########.fr */
/* */
/* ************************************************************************** */
#include "vector.h"
#include "libft.h"
#include <stdlib.h>
int vectorResize(t_vector *v, int capacity);
int vectorPushBack(t_vector *v, void *item);
int vectorSet(t_vector *v, int index, void *item);
void *vectorGet(t_vector *v, int index);
int vectorDelete(t_vector *v, int index);
int vectorFree(t_vector *v);
int vectorTotal(t_vector *v);
void vector_init(t_vector *v)
{
// Initialize function pointers
v->pfVectorTotal = vectorTotal;
v->pfVectorResize = vectorResize;
v->pfVectorAdd = vectorPushBack;
v->pfVectorSet = vectorSet;
v->pfVectorGet = vectorGet;
v->pfVectorFree = vectorFree;
v->pfVectorDelete = vectorDelete;
// Allocate memory and check for failure
v->vector_list.capacity = VECTOR_INIT_CAPACITY;
v->vector_list.total = 0;
v->vector_list.items = malloc(sizeof(void *) * v->vector_list.capacity);
if (!v->vector_list.items)
{
ft_putstr_fd("Memory allocation failed in vector_init\n", 2);
exit(EXIT_FAILURE);
}
}
int vectorTotal(t_vector *v)
{
int totalCount = UNDEFINE;
if(v)
{
totalCount = v->vector_list.total;
}
return totalCount;
}
int vectorResize(t_vector *v, int capacity)
{
int status = UNDEFINE;
if(v)
{
void **items = realloc(v->vector_list.items, sizeof(void *) * capacity);
if (items)
{
v->vector_list.items = items;
v->vector_list.capacity = capacity;
status = SUCCESS;
}
}
return status;
}
int vectorPushBack(t_vector *v, void *item)
{
int status = UNDEFINE;
if(v)
{
if (v->vector_list.capacity == v->vector_list.total)
{
status = vectorResize(v, v->vector_list.capacity * 2);
if(status != UNDEFINE)
{
v->vector_list.items[v->vector_list.total++] = item;
}
}
else
{
v->vector_list.items[v->vector_list.total++] = item;
status = SUCCESS;
}
}
return status;
}
int vectorSet(t_vector *v, int index, void *item)
{
int status = UNDEFINE;
if(v)
{
if ((index >= 0) && (index < v->vector_list.total))
{
v->vector_list.items[index] = item;
status = SUCCESS;
}
}
return status;
}
void *vectorGet(t_vector *v, int index)
{
void *readData = NULL;
if(v)
{
if ((index >= 0) && (index < v->vector_list.total))
{
readData = v->vector_list.items[index];
}
}
return readData;
}
int vectorDelete(t_vector *v, int index)
{
int status = UNDEFINE;
int i = 0;
if(v)
{
if ((index < 0) || (index >= v->vector_list.total))
return status;
v->vector_list.items[index] = NULL;
for (i = index; (i < v->vector_list.total - 1); ++i)
{
v->vector_list.items[i] = v->vector_list.items[i + 1];
v->vector_list.items[i + 1] = NULL;
}
v->vector_list.total--;
if ((v->vector_list.total > 0) && ((v->vector_list.total) == (v->vector_list.capacity / 4)))
{
vectorResize(v, v->vector_list.capacity / 2);
}
status = SUCCESS;
}
return status;
}
int vectorFree(t_vector *v)
{
int status = UNDEFINE;
int total = v->pfVectorTotal(v);
if(v)
{
for (int i = 0; i < total; i++)
{
void * temp = v->pfVectorGet(v, i);
free (temp);
}
free(v->vector_list.items);
}
return status;
}