Compare commits

...

2 Commits

Author SHA1 Message Date
da7e 4150e95378 refactor of files for norminette 2026-03-28 21:44:36 +01:00
da7e bf668cf5e6 norminette 2026-03-28 20:55:39 +01:00
11 changed files with 394 additions and 374 deletions
+2 -1
View File
@@ -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))
BIN
View File
Binary file not shown.
+4 -3
View File
@@ -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);
void game(int *lines, int size);
#endif
-62
View File
@@ -1,62 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lltoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
+22 -11
View File
@@ -6,36 +6,44 @@
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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) {
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) {
while (best <= line - 4)
{
best += 4;
}
return (best);
}
static int to_play(int prev_should_start, int line) {
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 {
if (line - goal < 3)
{
return ((line - goal) + 1);
}
else
{
return (1);
}
}
static int should_start(int prev_should_start, int line) {
static int should_start(int prev_should_start, int line)
{
if (prev_should_start && line == 1)
return (!prev_should_start);
if (prev_should_start)
@@ -44,14 +52,17 @@ static int should_start(int prev_should_start, int line) {
return (line % 4 != 0);
}
int ai(int *gamestate, int nb_line) {
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) {
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]));
+9 -7
View File
@@ -6,25 +6,27 @@
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "../inc/get_next_line.h"
#include "../libft/libft.h"
int check_input(int fd) {
int check_input(int fd)
{
int i;
int j;
char *line = NULL;
char *line;
line = NULL;
line = get_next_line(fd);
j = 0;
while (line != NULL) {
while (line != NULL)
{
i = -1;
while (line[++i])
if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n')
+9 -7
View File
@@ -6,22 +6,24 @@
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
+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);
}
+27 -17
View File
@@ -6,15 +6,15 @@
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 ft_strlen_with_c(char *str, char c)
{
size_t i;
i = 0;
@@ -25,13 +25,15 @@ size_t ft_strlen_with_c(char *str, char c) {
return (i);
}
size_t detect_newline(char *str) {
size_t detect_newline(char *str)
{
size_t i;
i = 0;
if (!str)
return (0);
while (str[i]) {
while (str[i])
{
if (str[i] == '\n')
return (1);
i++;
@@ -39,17 +41,20 @@ size_t detect_newline(char *str) {
return (0);
}
static void fill_line(char *line, char *c_remaining, char *src, size_t *index) {
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]) {
while (c_remaining && c_remaining[i])
{
line[i] = c_remaining[i];
i++;
}
while (src[j] && src[j] != '\n') {
while (src[j] && src[j] != '\n')
{
line[i + j] = src[j];
src[j] = 0;
j++;
@@ -59,16 +64,18 @@ static void fill_line(char *line, char *c_remaining, char *src, size_t *index) {
*index = j;
}
char *build_line(char *src, char *c_remaining, size_t *index) {
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;
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) {
if (!line)
{
free(c_remaining);
return (NULL);
}
@@ -80,14 +87,16 @@ char *build_line(char *src, char *c_remaining, size_t *index) {
return (line);
}
static void read_file(int fd, char *buffer, ssize_t *nb_read) {
static void read_file(int fd, char *buffer, ssize_t *nb_read)
{
*nb_read = read(fd, buffer, BUFFER_SIZE);
if (*nb_read < 0)
return;
return ;
buffer[*nb_read] = '\0';
}
static char *get_the_line(int fd, char *buffer) {
static char *get_the_line(int fd, char *buffer)
{
char *line;
size_t i;
ssize_t nb_read;
@@ -115,7 +124,8 @@ static char *get_the_line(int fd, char *buffer) {
return (line);
}
char *get_next_line(int fd) {
char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE + 1] = "";
char *line;
+24 -91
View File
@@ -6,118 +6,51 @@
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 21:41:29 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "../libft/libft.h"
#include "alcu.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "../libft/libft.h"
#include "alcu.h"
#include "get_next_line.h"
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')
static int get_fd(int argc, char **argv)
{
if (argc == 2)
{
return (open(argv[1], O_RDONLY));
}
else if (argc == 1)
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 {
ft_putstr_fd("-\nInvalid choice\n", 1);
continue;
}
}
}
}
void game(int *lines, int size) {
int ai_turn = 1;
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);
return (-1);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
int main(int argc, char *argv[])
{
int fd;
int size;
int *lines;
fd = get_fd(argc, argv);
if (fd < 0)
{
ft_putstr_fd("ERROR", 2);
return (1);
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1) {
size = check_input(fd);
if (size < 0)
{
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);
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);
+13 -7
View File
@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,12 +14,15 @@
#include <stddef.h>
#include <unistd.h>
static int get_biggest_line(int *game_state, size_t nb_line) {
static int get_biggest_line(int *game_state, size_t nb_line)
{
size_t i;
int biggest = 0;
int biggest;
biggest = 0;
i = 0;
while (i < nb_line) {
while (i < nb_line)
{
if (game_state[i] > biggest)
biggest = game_state[i];
i++;
@@ -27,19 +30,22 @@ static int get_biggest_line(int *game_state, size_t nb_line) {
return (biggest);
}
void print_board(int *game_state, size_t nb_line) {
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) {
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]) {
while (++j < game_state[i])
{
write(1, "|", 1);
if (j < game_state[i] - 1)
write(1, " ", 1);