diff --git a/alum1 b/alum1 index b3f7828..a7b5820 100755 Binary files a/alum1 and b/alum1 differ diff --git a/libft/ft_lltoa.c b/libft/ft_lltoa.c deleted file mode 100644 index 02e0200..0000000 --- a/libft/ft_lltoa.c +++ /dev/null @@ -1,62 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_lltoa.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: dgaillet +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2025/11/07 13:06:35 by dgaillet #+# #+# */ -/* Updated: 2025/12/07 12:29:25 by dgaillet ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -static size_t count_digits(long long n) -{ - size_t size; - - size = 0; - if (n <= 0) - size++; - while (n) - { - n /= 10; - size++; - } - return (size); -} - -static void insert_char(char *str, unsigned long long nbr, size_t index) -{ - while (nbr) - { - str[index--] = nbr % 10 + '0'; - nbr /= 10; - } -} - -char *ft_lltoa(long long n) -{ - unsigned long long nbr; - char *str; - size_t size; - - nbr = n; - if (n < 0) - nbr = n * -1; - size = count_digits(n); - str = malloc(sizeof(char) * (size + 1)); - if (!str) - return (NULL); - str[size] = '\0'; - if (nbr == 0) - str[0] = '0'; - else - { - if (n < 0) - str[0] = '-'; - insert_char(str, nbr, size - 1); - } - return (str); -} diff --git a/src/ai.c b/src/ai.c index 4e9ee81..0664f80 100644 --- a/src/ai.c +++ b/src/ai.c @@ -6,57 +6,68 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/03/28 17:22:33 by dgaillet #+# #+# */ -/* Updated: 2026/03/28 17:22:35 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/03/28 20:51:49 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "alcu.h" -static int best_to_take(int prev_should_start, int line) { - int best; +static int best_to_take(int prev_should_start, int line) +{ + int best; - if (prev_should_start) - best = 2; - else - best = 1; - while (best <= line - 4) { - best += 4; - } - return (best); + if (prev_should_start) + best = 2; + else + best = 1; + while (best <= line - 4) + { + best += 4; + } + return (best); } -static int to_play(int prev_should_start, int line) { - int goal; - goal = best_to_take(prev_should_start, line); - if (line - goal < 3) { - return (line - goal) + 1; - } else { - return (1); - } +static int to_play(int prev_should_start, int line) +{ + int goal; + + goal = best_to_take(prev_should_start, line); + if (line - goal < 3) + { + return ((line - goal) + 1); + } + else + { + return (1); + } } -static int should_start(int prev_should_start, int line) { - if (prev_should_start && line == 1) - return (!prev_should_start); - if (prev_should_start) - return (line % 4 != 1); - else - return (line % 4 != 0); +static int should_start(int prev_should_start, int line) +{ + if (prev_should_start && line == 1) + return (!prev_should_start); + if (prev_should_start) + return (line % 4 != 1); + else + return (line % 4 != 0); } -int ai(int *gamestate, int nb_line) { - int prev_should_start; - int i; +int ai(int *gamestate, int nb_line) +{ + int prev_should_start; + int i; - prev_should_start = 0; - i = -1; - while (++i < nb_line && gamestate[i] != 0) { - if (i == nb_line - 1 || gamestate[i + 1] == 0) { - if (gamestate[i] == 1) - return (1); - return (to_play(prev_should_start, gamestate[i])); - } - prev_should_start = should_start(prev_should_start, gamestate[i]); - } - return (1); + prev_should_start = 0; + i = -1; + while (++i < nb_line && gamestate[i] != 0) + { + if (i == nb_line - 1 || gamestate[i + 1] == 0) + { + if (gamestate[i] == 1) + return (1); + return (to_play(prev_should_start, gamestate[i])); + } + prev_should_start = should_start(prev_should_start, gamestate[i]); + } + return (1); } diff --git a/src/check_input.c b/src/check_input.c index eb8a8c8..f25aa80 100644 --- a/src/check_input.c +++ b/src/check_input.c @@ -6,34 +6,36 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/03/28 13:01:37 by dgaillet #+# #+# */ -/* Updated: 2026/03/28 16:05:37 by lud-adam ### ########.fr */ +/* Updated: 2026/03/28 20:52:29 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ +#include "../libft/libft.h" +#include "get_next_line.h" #include #include #include -#include "../inc/get_next_line.h" -#include "../libft/libft.h" +int check_input(int fd) +{ + int i; + int j; + char *line; -int check_input(int fd) { - int i; - int j; - char *line = NULL; - - line = get_next_line(fd); - j = 0; - while (line != NULL) { - i = -1; - while (line[++i]) - if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n') - return (free(line), -1); - if (ft_atoi(line) < 1 || ft_atoi(line) > 10000) - return (free(line), -1); - free(line); - j++; - line = get_next_line(fd); - } - return (j); + line = NULL; + line = get_next_line(fd); + j = 0; + while (line != NULL) + { + i = -1; + while (line[++i]) + if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n') + return (free(line), -1); + if (ft_atoi(line) < 1 || ft_atoi(line) > 10000) + return (free(line), -1); + free(line); + j++; + line = get_next_line(fd); + } + return (j); } diff --git a/src/fill_array.c b/src/fill_array.c index b6bc993..90b2a21 100644 --- a/src/fill_array.c +++ b/src/fill_array.c @@ -6,22 +6,24 @@ /* By: lud-adam +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/03/28 13:21:46 by lud-adam #+# #+# */ -/* Updated: 2026/03/28 16:05:46 by lud-adam ### ########.fr */ +/* Updated: 2026/03/28 20:53:02 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "../libft/libft.h" #include "get_next_line.h" -int* fill_array(int fd, int size) +int *fill_array(int fd, int size) { - int* res = malloc(sizeof(int) * size); + int *res; + char *line; + int i; + + res = malloc(sizeof(int) * size); if (res == NULL) return (NULL); - - char* line = get_next_line(fd); - int i = 0; - + line = get_next_line(fd); + i = 0; while (line != NULL) { res[i] = ft_atoi(line); diff --git a/src/get_next_line.c b/src/get_next_line.c index 55d80e1..41fc314 100644 --- a/src/get_next_line.c +++ b/src/get_next_line.c @@ -6,123 +6,133 @@ /* By: lud-adam +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/03/28 15:07:17 by lud-adam #+# #+# */ -/* Updated: 2026/03/28 16:04:59 by lud-adam ### ########.fr */ +/* Updated: 2026/03/28 20:53:31 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ +#include "../libft/libft.h" #include "get_next_line.h" -#include "../libft/libft.h" +size_t ft_strlen_with_c(char *str, char c) +{ + size_t i; -size_t ft_strlen_with_c(char *str, char c) { - size_t i; - - i = 0; - if (!str) - return (0); - while (str[i] && str[i] != c) - i++; - return (i); + i = 0; + if (!str) + return (0); + while (str[i] && str[i] != c) + i++; + return (i); } -size_t detect_newline(char *str) { - size_t i; +size_t detect_newline(char *str) +{ + size_t i; - i = 0; - if (!str) - return (0); - while (str[i]) { - if (str[i] == '\n') - return (1); - i++; - } - return (0); + i = 0; + if (!str) + return (0); + while (str[i]) + { + if (str[i] == '\n') + return (1); + i++; + } + return (0); } -static void fill_line(char *line, char *c_remaining, char *src, size_t *index) { - size_t i; - size_t j; +static void fill_line(char *line, char *c_remaining, char *src, size_t *index) +{ + size_t i; + size_t j; - i = 0; - j = 0; - while (c_remaining && c_remaining[i]) { - line[i] = c_remaining[i]; - i++; - } - while (src[j] && src[j] != '\n') { - line[i + j] = src[j]; - src[j] = 0; - j++; - } - if (src[j] == '\n') - line[i + j] = '\n'; - *index = j; + i = 0; + j = 0; + while (c_remaining && c_remaining[i]) + { + line[i] = c_remaining[i]; + i++; + } + while (src[j] && src[j] != '\n') + { + line[i + j] = src[j]; + src[j] = 0; + j++; + } + if (src[j] == '\n') + line[i + j] = '\n'; + *index = j; } -char *build_line(char *src, char *c_remaining, size_t *index) { - char *line; - size_t size; - size_t is_jump; +char *build_line(char *src, char *c_remaining, size_t *index) +{ + char *line; + size_t size; + size_t is_jump; - is_jump = detect_newline(src); - size = ft_strlen_with_c(src, '\n') + ft_strlen_with_c(c_remaining, '\0') + - is_jump + 1; - line = ft_calloc(size, sizeof(char)); - if (!line) { - free(c_remaining); - return (NULL); - } - if (!src) - return (NULL); - fill_line(line, c_remaining, src, index); - if (c_remaining) - free(c_remaining); - return (line); + is_jump = detect_newline(src); + size = ft_strlen_with_c(src, '\n') + ft_strlen_with_c(c_remaining, '\0') + + is_jump + 1; + line = ft_calloc(size, sizeof(char)); + if (!line) + { + free(c_remaining); + return (NULL); + } + if (!src) + return (NULL); + fill_line(line, c_remaining, src, index); + if (c_remaining) + free(c_remaining); + return (line); } -static void read_file(int fd, char *buffer, ssize_t *nb_read) { - *nb_read = read(fd, buffer, BUFFER_SIZE); - if (*nb_read < 0) - return; - buffer[*nb_read] = '\0'; +static void read_file(int fd, char *buffer, ssize_t *nb_read) +{ + *nb_read = read(fd, buffer, BUFFER_SIZE); + if (*nb_read < 0) + return ; + buffer[*nb_read] = '\0'; } -static char *get_the_line(int fd, char *buffer) { - char *line; - size_t i; - ssize_t nb_read; +static char *get_the_line(int fd, char *buffer) +{ + char *line; + size_t i; + ssize_t nb_read; - i = 0; - nb_read = 0; - line = NULL; - if (buffer[0] != '\0') - { - line = build_line(buffer, NULL, &i); - if (!line) - return (NULL); - ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0')); - } - while (detect_newline(line) == 0) - { - read_file(fd, buffer, &nb_read); - if (nb_read < 0) - return (free(line), NULL); - if (nb_read == 0) - break ; - line = build_line(buffer, line, &i); - ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0')); - } - return (line); + i = 0; + nb_read = 0; + line = NULL; + if (buffer[0] != '\0') + { + line = build_line(buffer, NULL, &i); + if (!line) + return (NULL); + ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0')); + } + while (detect_newline(line) == 0) + { + read_file(fd, buffer, &nb_read); + if (nb_read < 0) + return (free(line), NULL); + if (nb_read == 0) + break ; + line = build_line(buffer, line, &i); + ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0')); + } + return (line); } -char *get_next_line(int fd) { - static char buffer[BUFFER_SIZE + 1] = ""; - char *line; +char *get_next_line(int fd) +{ + static char buffer[BUFFER_SIZE + 1] = ""; + char *line; - if (fd < 0 || BUFFER_SIZE <= 0) - return (NULL); - line = get_the_line(fd, buffer); - if (!line) - return (NULL); - return (line); + if (fd < 0 || BUFFER_SIZE <= 0) + return (NULL); + line = get_the_line(fd, buffer); + if (!line) + return (NULL); + return (line); } diff --git a/src/main.c b/src/main.c index 0eea6fb..b1bbf9a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,120 +6,135 @@ /* By: lud-adam +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/03/28 12:30:29 by lud-adam #+# #+# */ -/* Updated: 2026/03/28 16:06:09 by lud-adam ### ########.fr */ +/* Updated: 2026/03/28 20:54:09 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ -#include -#include -#include -#include - #include "../libft/libft.h" #include "alcu.h" #include "get_next_line.h" +#include +#include +#include -int check_char_int(char *str) { - int i; +int check_char_int(char *str) +{ + int i; - i = -1; - while (str[++i] && i < 100) - if (!(str[i] >= '0' && str[i] <= '9') && str[i] != '\n') - return (0); - - return (1); + i = -1; + while (str[++i] && i < 100) + if (!(str[i] >= '0' && str[i] <= '9') && str[i] != '\n') + return (0); + return (1); } -void player_turn(int *line) { - char *temp; - int choice; +void player_turn(int *line) +{ + char *temp; + int choice; - while (1) { - ft_putstr_fd("\nPlease choose between 1 and 3 items\n", 1); - temp = get_next_line(0); - if (temp && ft_strlen(temp) > 0) { - if (!check_char_int(temp)) { - free(temp); - continue; - } - choice = ft_atoi(temp); - free(temp); - if (choice > 0 && choice < 4) { - if (choice > *line) { - ft_putstr_fd("-\nInvalid choice\n", 1); - continue; - } - *line -= choice; - break; - } else { - ft_putstr_fd("-\nInvalid choice\n", 1); - continue; - } - } - } + while (1) + { + ft_putstr_fd("\nPlease choose between 1 and 3 items\n", 1); + temp = get_next_line(0); + if (temp && ft_strlen(temp) > 0) + { + if (!check_char_int(temp)) + { + free(temp); + continue ; + } + choice = ft_atoi(temp); + free(temp); + if (choice > 0 && choice < 4) + { + if (choice > *line) + { + ft_putstr_fd("-\nInvalid choice\n", 1); + continue ; + } + *line -= choice; + break ; + } + else + { + ft_putstr_fd("-\nInvalid choice\n", 1); + continue ; + } + } + } } -void game(int *lines, int size) { - int ai_turn = 1; - int choice; +void game(int *lines, int size) +{ + int ai_turn; + int choice; - for (int i = size - 1; i >= 0; i--) { - while (lines[i] != 0) { - ft_putstr_fd("\n------------------------------\n", 1); - if (!ai_turn) { - print_board(lines, size); - player_turn(&lines[i]); - } else { - print_board(lines, size); - ft_putstr_fd("\n", 1); - choice = ai(lines, size); - ft_putstr_fd("AI play ", 1); - ft_putnbr_fd(choice, 1); - ft_putstr_fd("\n", 1); - lines[i] -= choice; - } - ai_turn = ai_turn == 0; - } - } - if (!ai_turn) - ft_putstr_fd("AI win ! It will replace you\n", 1); - else - ft_putstr_fd("You win ! Well done\n", 1); + ai_turn = 1; + for (int i = size - 1; i >= 0; i--) + { + while (lines[i] != 0) + { + ft_putstr_fd("\n------------------------------\n", 1); + if (!ai_turn) + { + print_board(lines, size); + player_turn(&lines[i]); + } + else + { + print_board(lines, size); + ft_putstr_fd("\n", 1); + choice = ai(lines, size); + ft_putstr_fd("AI play ", 1); + ft_putnbr_fd(choice, 1); + ft_putstr_fd("\n", 1); + lines[i] -= choice; + } + ai_turn = ai_turn == 0; + } + } + if (!ai_turn) + ft_putstr_fd("AI win ! It will replace you\n", 1); + else + ft_putstr_fd("You win ! Well done\n", 1); } -int main(int argc, char *argv[]) { - if (argc != 2) { - ft_putstr_fd("ERROR", 2); - return (1); - } +int main(int argc, char *argv[]) +{ + int fd; + int size; + int *lines; - int fd = open(argv[1], O_RDONLY); - - if (fd == -1) { - ft_putstr_fd("ERROR", 2); - return (1); - } - - int size = check_input(fd); - if (size < 0) { - ft_putstr_fd("ERROR", 2); - return (1); - } - - close(fd); - fd = open(argv[1], O_RDONLY); - int *lines = fill_array(fd, size); - if (!lines) - return (1); - - // for (int i = 0; i < size; i++) - // { - // printf("lines[i] : %d\n", lines[i]); - // } - - // int player = 1; - game(lines, size); - close(fd); - free(lines); - return (0); + if (argc != 2) + { + ft_putstr_fd("ERROR", 2); + return (1); + } + fd = open(argv[1], O_RDONLY); + if (fd == -1) + { + ft_putstr_fd("ERROR", 2); + return (1); + } + size = check_input(fd); + if (size < 0) + { + ft_putstr_fd("ERROR", 2); + return (1); + } + close(fd); + fd = open(argv[1], O_RDONLY); + lines = fill_array(fd, size); + if (!lines) + return (1); + // for (int i = 0; i < size; i++) + // { + // printf("lines[i] : %d\n", lines[i]); + // } + // int player = 1; + game(lines, size); + close(fd); + free(lines); + return (0); } diff --git a/src/print_board.c b/src/print_board.c index 9275920..98d1c3d 100644 --- a/src/print_board.c +++ b/src/print_board.c @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/03/28 16:06:30 by dgaillet #+# #+# */ -/* Updated: 2026/03/28 16:06:32 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/03/28 20:54:31 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -14,39 +14,45 @@ #include #include -static int get_biggest_line(int *game_state, size_t nb_line) { - size_t i; - int biggest = 0; +static int get_biggest_line(int *game_state, size_t nb_line) +{ + size_t i; + int biggest; - i = 0; - while (i < nb_line) { - if (game_state[i] > biggest) - biggest = game_state[i]; - i++; - } - return (biggest); + biggest = 0; + i = 0; + while (i < nb_line) + { + if (game_state[i] > biggest) + biggest = game_state[i]; + i++; + } + return (biggest); } -void print_board(int *game_state, size_t nb_line) { - int biggest_line; - size_t i; - int j; +void print_board(int *game_state, size_t nb_line) +{ + int biggest_line; + size_t i; + int j; - i = -1; - biggest_line = get_biggest_line(game_state, nb_line); - while (++i < nb_line && game_state[i] != 0) { - j = -1; - while (++j < biggest_line - game_state[i]) - write(1, " ", 1); - j = -1; - while (++j < game_state[i]) { - write(1, "|", 1); - if (j < game_state[i] - 1) - write(1, " ", 1); - } - j = -1; - while (++j < biggest_line - game_state[i]) - write(1, " ", 1); - write(1, "\n", 1); - } + i = -1; + biggest_line = get_biggest_line(game_state, nb_line); + while (++i < nb_line && game_state[i] != 0) + { + j = -1; + while (++j < biggest_line - game_state[i]) + write(1, " ", 1); + j = -1; + while (++j < game_state[i]) + { + write(1, "|", 1); + if (j < game_state[i] - 1) + write(1, " ", 1); + } + j = -1; + while (++j < biggest_line - game_state[i]) + write(1, " ", 1); + write(1, "\n", 1); + } }