diff --git a/Makefile b/Makefile index f21d0e9..3b762f1 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,8 @@ SRC = main.c \ fill_array.c \ check_input.c \ print_board.c \ - ai.c + ai.c \ + game.c SRCS = $(addprefix $(P_SRC), $(SRC)) OBJS = $(patsubst $(P_SRC)%.c,$(P_OBJ)%.o,$(SRCS)) diff --git a/alum1 b/alum1 index a7b5820..8cddbd2 100755 Binary files a/alum1 and b/alum1 differ diff --git a/inc/alcu.h b/inc/alcu.h index a8ebfdc..11a7228 100644 --- a/inc/alcu.h +++ b/inc/alcu.h @@ -6,19 +6,20 @@ /* By: lud-adam +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/03/28 13:27:29 by lud-adam #+# #+# */ -/* Updated: 2026/03/28 15:26:01 by lud-adam ### ########.fr */ +/* Updated: 2026/03/28 21:41:55 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #ifndef ALCU_H -#define ALCU_H +# define ALCU_H -#include +# include -char *read_file(int fd); -int check_input(int fd); -void print_board(int *game_state, size_t nb_line); -int *fill_array(int fd, int size); -int ai(int *gamestate, int nb_line); +char *read_file(int fd); +int check_input(int fd); +void print_board(int *game_state, size_t nb_line); +int *fill_array(int fd, int size); +int ai(int *gamestate, int nb_line); +void game(int *lines, int size); #endif diff --git a/src/game.c b/src/game.c new file mode 100644 index 0000000..9ef9f52 --- /dev/null +++ b/src/game.c @@ -0,0 +1,116 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/03/28 21:14:00 by dgaillet #+# #+# */ +/* Updated: 2026/03/28 21:41:04 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft/libft.h" +#include "alcu.h" +#include "get_next_line.h" + +static 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); +} + +static int play_choice(int *line, int choice) +{ + if (choice > 0 && choice < 4) + { + if (choice > *line) + { + ft_putstr_fd("-\nInvalid choice\n", 1); + return (0); + } + *line -= choice; + return (1); + } + else + { + ft_putstr_fd("-\nInvalid choice\n", 1); + return (0); + } +} + +static 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); + if (play_choice(line, choice)) + break ; + } + else + ft_putstr_fd("-\nInvalid choice\n", 1); + if (temp) + free(temp); + } +} + +static void play_turns(int *lines, int i, int *ai_turn, int size) +{ + int choice; + + 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; + } +} + +void game(int *lines, int size) +{ + int ai_turn; + int i; + + ai_turn = 1; + i = size - 1; + while (i >= 0) + { + play_turns(lines, i, &ai_turn, size); + i--; + } + if (!ai_turn) + ft_putstr_fd("AI win ! It will replace you\n", 1); + else + ft_putstr_fd("You win ! Well done\n", 1); +} diff --git a/src/main.c b/src/main.c index b1bbf9a..fc17d34 100644 --- a/src/main.c +++ b/src/main.c @@ -6,98 +6,26 @@ /* By: lud-adam +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/03/28 12:30:29 by lud-adam #+# #+# */ -/* Updated: 2026/03/28 20:54:09 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/03/28 21:41:29 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include "../libft/libft.h" #include "alcu.h" -#include "get_next_line.h" #include #include #include -int check_char_int(char *str) +static int get_fd(int argc, char **argv) { - int i; - - 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; - - while (1) + if (argc == 2) { - 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 ; - } - } + return (open(argv[1], O_RDONLY)); } -} - -void game(int *lines, int size) -{ - int ai_turn; - int choice; - - 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 if (argc == 1) + return (0); else - ft_putstr_fd("You win ! Well done\n", 1); + return (-1); } int main(int argc, char *argv[]) @@ -106,13 +34,8 @@ int main(int argc, char *argv[]) int size; int *lines; - if (argc != 2) - { - ft_putstr_fd("ERROR", 2); - return (1); - } - fd = open(argv[1], O_RDONLY); - if (fd == -1) + fd = get_fd(argc, argv); + if (fd < 0) { ft_putstr_fd("ERROR", 2); return (1); @@ -128,11 +51,6 @@ int main(int argc, char *argv[]) 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);