From bf668cf5e6ea306a78d08177287809df1fcc98a0 Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Sat, 28 Mar 2026 20:55:39 +0100 Subject: [PATCH] norminette --- alum1 | Bin 28152 -> 28160 bytes libft/ft_lltoa.c | 62 ------------- src/ai.c | 91 ++++++++++--------- src/check_input.c | 46 +++++----- src/fill_array.c | 16 ++-- src/get_next_line.c | 202 +++++++++++++++++++++-------------------- src/main.c | 213 ++++++++++++++++++++++++-------------------- src/print_board.c | 70 ++++++++------- 8 files changed, 342 insertions(+), 358 deletions(-) delete mode 100644 libft/ft_lltoa.c diff --git a/alum1 b/alum1 index b3f7828df304cfc90961a0bea96eac829af59872..a7b58202515be5c9837c12a4ce253776731b33cc 100755 GIT binary patch delta 3860 zcmaJ@e{36f760Cy-`(XBCw6SdX_Gc-+L5-((!sQ4luPTT+1gz?NTUNu5CIQ-TbVHi}wM;@Fq)IeY>Ppt3it$HNgc$pyViZx3@&}VZbVFLFO;A4X zd*>{b7*&1uec$)q=ly<{53eiF{z<8BRx%4)c52BFGL3I068C-K_Q=gce|~%FiG{)k zFP%TB75cp|2_cl^lp=(t&KFCPItD7D&QHfx@LHHB=$_^bUP9c&Psxt+vjZA)AAzfM zJRN8hpXU1wHT^PEU4edQA_Gc@36{@J;iL>gf8X3U<&gnEmcAn?R)GV>mza7eq^2d@ z9;WpBL$qihSdgAN{?B%$zH*dxCRvKz`@w3e_^PDJfztGknK}}p7o_E>keZNQeU-fu z#w|Q3Vx9nD%=sG!y6C648StHfF0TWZiuYY4N_zvBv-l~`)oI1y3{EnLk~HI;fe5Md z1(~lhD+rwfGNh%5Fz5J2srxlvreUe)QW>0tkSWOi0VgF^fflDlRw>3936MvB%Ra;y z3 zL?YACOE}KrsQt>E_TL*^M;?fO`-ulPdR3g1*tek!^Fb`JYCsib=SqsJ`+b&dX33Sv z5-$hTn2apRdP3)C*xEWzi4ioQcmd~#cm*gXenK>eGm22fkNnr*#sY&Uc*?nIG7FM| zsgeXwF%MK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Ch?f|Sfzt{1;?hoJF z>zUX!c5=gWKwAXu4bY5)CueMZJD=@hNuY-%ygL{vdc7;lv}zPTV?54s9U<*!+*z}Sh;xLvyqsC^Qh|euOHmxA0Nr!(j~H=B72@1Oqc8`HLp`! zQ5R2>)|(tkm}`_WSE=2hjw1D~(rpe66zT3&iaQiDr%AsuLB>3(E1>xi)meMUHs>gA zj({&mn~RimsV7Hqmo`JX4mJ`M98E*SHS^@P6{rcD4LO|c0iz3Zc}nBmU8ICd^$^XN z4z*x80C%QgAyt48hZ2;gg-XIuG=?LCydfHYJnRz-kAH|%e^fSe)W z|IHH9z=sQ9caWq!EK~@FHp9CvY&AJ#kHA7vl%Esfk|)0F{9oa`m0cox37v47+Ka5! zj<#_1SQRnvLdzlM%}_z>k;Cb!caaA)Ag17hbB2VCvhrOHTvF5vo?$ISp&ruTUrE*rnYS8bT#p{14X+#sZoi z#$D<{o5Ne^r(injVyKseAqC@HbIM_BZG$wOr!CmUfXAy3LT}`M+t7tj)q|h+;`U3_b}GmukI)D;&O@5rz&$pPl%XC~Zi9Nh1~B)yYPyPa%PQ@3 z=*}Vyu2O%Nwc#cxa-D8<^%1_yxAL^DvEwP+ z5K+&Lr<{FTY$oc}s3N-;#8zYL~INAgTniI518ov_} +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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); + } }