mirror of
https://github.com/LucasCodeur/alcu.git
synced 2026-04-28 17:44:34 +02:00
refactor of files for norminette
This commit is contained in:
@@ -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))
|
||||
|
||||
+9
-8
@@ -6,19 +6,20 @@
|
||||
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stddef.h>
|
||||
# include <stddef.h>
|
||||
|
||||
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
|
||||
|
||||
+116
@@ -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);
|
||||
}
|
||||
+9
-91
@@ -6,98 +6,26 @@
|
||||
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user