refactor of files for norminette

This commit is contained in:
2026-03-28 21:44:36 +01:00
parent bf668cf5e6
commit 4150e95378
5 changed files with 136 additions and 100 deletions
+2 -1
View File
@@ -27,7 +27,8 @@ SRC = main.c \
fill_array.c \ fill_array.c \
check_input.c \ check_input.c \
print_board.c \ print_board.c \
ai.c ai.c \
game.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.
+2 -1
View File
@@ -6,7 +6,7 @@
/* 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 15:26:01 by lud-adam ### ########.fr */ /* Updated: 2026/03/28 21:41:55 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -20,5 +20,6 @@ int check_input(int fd);
void print_board(int *game_state, size_t nb_line); void print_board(int *game_state, size_t nb_line);
int *fill_array(int fd, int size); int *fill_array(int fd, int size);
int ai(int *gamestate, int nb_line); int ai(int *gamestate, int nb_line);
void game(int *lines, int size);
#endif #endif
+116
View File
@@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
+10 -92
View File
@@ -6,98 +6,26 @@
/* 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 20:54:09 by dgaillet ### ########lyon.fr */ /* Updated: 2026/03/28 21:41:29 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../libft/libft.h" #include "../libft/libft.h"
#include "alcu.h" #include "alcu.h"
#include "get_next_line.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
int check_char_int(char *str) static int get_fd(int argc, char **argv)
{ {
int i; if (argc == 2)
{
i = -1; return (open(argv[1], O_RDONLY));
while (str[++i] && i < 100) }
if (!(str[i] >= '0' && str[i] <= '9') && str[i] != '\n') else if (argc == 1)
return (0); return (0);
return (1);
}
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 else
{ return (-1);
ft_putstr_fd("-\nInvalid choice\n", 1);
continue ;
}
}
}
}
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
ft_putstr_fd("You win ! Well done\n", 1);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@@ -106,13 +34,8 @@ int main(int argc, char *argv[])
int size; int size;
int *lines; int *lines;
if (argc != 2) fd = get_fd(argc, argv);
{ if (fd < 0)
ft_putstr_fd("ERROR", 2);
return (1);
}
fd = open(argv[1], O_RDONLY);
if (fd == -1)
{ {
ft_putstr_fd("ERROR", 2); ft_putstr_fd("ERROR", 2);
return (1); return (1);
@@ -128,11 +51,6 @@ int main(int argc, char *argv[])
lines = fill_array(fd, size); lines = fill_array(fd, size);
if (!lines) if (!lines)
return (1); return (1);
// for (int i = 0; i < size; i++)
// {
// printf("lines[i] : %d\n", lines[i]);
// }
// int player = 1;
game(lines, size); game(lines, size);
close(fd); close(fd);
free(lines); free(lines);