mirror of
https://github.com/LucasCodeur/alcu.git
synced 2026-04-28 17:44:34 +02:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4150e95378 | |||
| bf668cf5e6 |
@@ -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))
|
||||||
|
|||||||
+9
-8
@@ -6,19 +6,20 @@
|
|||||||
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef ALCU_H
|
#ifndef ALCU_H
|
||||||
#define ALCU_H
|
# define ALCU_H
|
||||||
|
|
||||||
#include <stddef.h>
|
# include <stddef.h>
|
||||||
|
|
||||||
char *read_file(int fd);
|
char *read_file(int fd);
|
||||||
int check_input(int fd);
|
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
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -6,57 +6,68 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/03/28 17:22:33 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"
|
#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;
|
{
|
||||||
|
int best;
|
||||||
|
|
||||||
if (prev_should_start)
|
if (prev_should_start)
|
||||||
best = 2;
|
best = 2;
|
||||||
else
|
else
|
||||||
best = 1;
|
best = 1;
|
||||||
while (best <= line - 4) {
|
while (best <= line - 4)
|
||||||
best += 4;
|
{
|
||||||
}
|
best += 4;
|
||||||
return (best);
|
}
|
||||||
|
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);
|
int goal;
|
||||||
if (line - goal < 3) {
|
|
||||||
return (line - goal) + 1;
|
goal = best_to_take(prev_should_start, line);
|
||||||
} else {
|
if (line - goal < 3)
|
||||||
return (1);
|
{
|
||||||
}
|
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 && line == 1)
|
||||||
if (prev_should_start)
|
return (!prev_should_start);
|
||||||
return (line % 4 != 1);
|
if (prev_should_start)
|
||||||
else
|
return (line % 4 != 1);
|
||||||
return (line % 4 != 0);
|
else
|
||||||
|
return (line % 4 != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ai(int *gamestate, int nb_line) {
|
int ai(int *gamestate, int nb_line)
|
||||||
int prev_should_start;
|
{
|
||||||
int i;
|
int prev_should_start;
|
||||||
|
int i;
|
||||||
|
|
||||||
prev_should_start = 0;
|
prev_should_start = 0;
|
||||||
i = -1;
|
i = -1;
|
||||||
while (++i < nb_line && gamestate[i] != 0) {
|
while (++i < nb_line && gamestate[i] != 0)
|
||||||
if (i == nb_line - 1 || gamestate[i + 1] == 0) {
|
{
|
||||||
if (gamestate[i] == 1)
|
if (i == nb_line - 1 || gamestate[i + 1] == 0)
|
||||||
return (1);
|
{
|
||||||
return (to_play(prev_should_start, gamestate[i]));
|
if (gamestate[i] == 1)
|
||||||
}
|
return (1);
|
||||||
prev_should_start = should_start(prev_should_start, gamestate[i]);
|
return (to_play(prev_should_start, gamestate[i]));
|
||||||
}
|
}
|
||||||
return (1);
|
prev_should_start = should_start(prev_should_start, gamestate[i]);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-22
@@ -6,34 +6,36 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/03/28 13:01:37 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 <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../inc/get_next_line.h"
|
int check_input(int fd)
|
||||||
#include "../libft/libft.h"
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
char *line;
|
||||||
|
|
||||||
int check_input(int fd) {
|
line = NULL;
|
||||||
int i;
|
line = get_next_line(fd);
|
||||||
int j;
|
j = 0;
|
||||||
char *line = NULL;
|
while (line != NULL)
|
||||||
|
{
|
||||||
line = get_next_line(fd);
|
i = -1;
|
||||||
j = 0;
|
while (line[++i])
|
||||||
while (line != NULL) {
|
if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n')
|
||||||
i = -1;
|
return (free(line), -1);
|
||||||
while (line[++i])
|
if (ft_atoi(line) < 1 || ft_atoi(line) > 10000)
|
||||||
if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n')
|
return (free(line), -1);
|
||||||
return (free(line), -1);
|
free(line);
|
||||||
if (ft_atoi(line) < 1 || ft_atoi(line) > 10000)
|
j++;
|
||||||
return (free(line), -1);
|
line = get_next_line(fd);
|
||||||
free(line);
|
}
|
||||||
j++;
|
return (j);
|
||||||
line = get_next_line(fd);
|
|
||||||
}
|
|
||||||
return (j);
|
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-7
@@ -6,22 +6,24 @@
|
|||||||
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/03/28 13:21:46 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 "../libft/libft.h"
|
||||||
#include "get_next_line.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)
|
if (res == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
line = get_next_line(fd);
|
||||||
char* line = get_next_line(fd);
|
i = 0;
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
while (line != NULL)
|
while (line != NULL)
|
||||||
{
|
{
|
||||||
res[i] = ft_atoi(line);
|
res[i] = ft_atoi(line);
|
||||||
|
|||||||
+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);
|
||||||
|
}
|
||||||
+106
-96
@@ -6,123 +6,133 @@
|
|||||||
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
/* By: lud-adam <lud-adam@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/03/28 15:07:17 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 "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) {
|
i = 0;
|
||||||
size_t i;
|
if (!str)
|
||||||
|
return (0);
|
||||||
i = 0;
|
while (str[i] && str[i] != c)
|
||||||
if (!str)
|
i++;
|
||||||
return (0);
|
return (i);
|
||||||
while (str[i] && str[i] != c)
|
|
||||||
i++;
|
|
||||||
return (i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t detect_newline(char *str) {
|
size_t detect_newline(char *str)
|
||||||
size_t i;
|
{
|
||||||
|
size_t i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (!str)
|
if (!str)
|
||||||
return (0);
|
return (0);
|
||||||
while (str[i]) {
|
while (str[i])
|
||||||
if (str[i] == '\n')
|
{
|
||||||
return (1);
|
if (str[i] == '\n')
|
||||||
i++;
|
return (1);
|
||||||
}
|
i++;
|
||||||
return (0);
|
}
|
||||||
|
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;
|
size_t i;
|
||||||
|
size_t j;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
j = 0;
|
j = 0;
|
||||||
while (c_remaining && c_remaining[i]) {
|
while (c_remaining && c_remaining[i])
|
||||||
line[i] = c_remaining[i];
|
{
|
||||||
i++;
|
line[i] = c_remaining[i];
|
||||||
}
|
i++;
|
||||||
while (src[j] && src[j] != '\n') {
|
}
|
||||||
line[i + j] = src[j];
|
while (src[j] && src[j] != '\n')
|
||||||
src[j] = 0;
|
{
|
||||||
j++;
|
line[i + j] = src[j];
|
||||||
}
|
src[j] = 0;
|
||||||
if (src[j] == '\n')
|
j++;
|
||||||
line[i + j] = '\n';
|
}
|
||||||
*index = j;
|
if (src[j] == '\n')
|
||||||
|
line[i + j] = '\n';
|
||||||
|
*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;
|
char *line;
|
||||||
size_t is_jump;
|
size_t size;
|
||||||
|
size_t is_jump;
|
||||||
|
|
||||||
is_jump = detect_newline(src);
|
is_jump = detect_newline(src);
|
||||||
size = ft_strlen_with_c(src, '\n') + ft_strlen_with_c(c_remaining, '\0') +
|
size = ft_strlen_with_c(src, '\n') + ft_strlen_with_c(c_remaining, '\0')
|
||||||
is_jump + 1;
|
+ is_jump + 1;
|
||||||
line = ft_calloc(size, sizeof(char));
|
line = ft_calloc(size, sizeof(char));
|
||||||
if (!line) {
|
if (!line)
|
||||||
free(c_remaining);
|
{
|
||||||
return (NULL);
|
free(c_remaining);
|
||||||
}
|
return (NULL);
|
||||||
if (!src)
|
}
|
||||||
return (NULL);
|
if (!src)
|
||||||
fill_line(line, c_remaining, src, index);
|
return (NULL);
|
||||||
if (c_remaining)
|
fill_line(line, c_remaining, src, index);
|
||||||
free(c_remaining);
|
if (c_remaining)
|
||||||
return (line);
|
free(c_remaining);
|
||||||
|
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)
|
*nb_read = read(fd, buffer, BUFFER_SIZE);
|
||||||
return;
|
if (*nb_read < 0)
|
||||||
buffer[*nb_read] = '\0';
|
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;
|
char *line;
|
||||||
ssize_t nb_read;
|
size_t i;
|
||||||
|
ssize_t nb_read;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
nb_read = 0;
|
nb_read = 0;
|
||||||
line = NULL;
|
line = NULL;
|
||||||
if (buffer[0] != '\0')
|
if (buffer[0] != '\0')
|
||||||
{
|
{
|
||||||
line = build_line(buffer, NULL, &i);
|
line = build_line(buffer, NULL, &i);
|
||||||
if (!line)
|
if (!line)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0'));
|
ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0'));
|
||||||
}
|
}
|
||||||
while (detect_newline(line) == 0)
|
while (detect_newline(line) == 0)
|
||||||
{
|
{
|
||||||
read_file(fd, buffer, &nb_read);
|
read_file(fd, buffer, &nb_read);
|
||||||
if (nb_read < 0)
|
if (nb_read < 0)
|
||||||
return (free(line), NULL);
|
return (free(line), NULL);
|
||||||
if (nb_read == 0)
|
if (nb_read == 0)
|
||||||
break ;
|
break ;
|
||||||
line = build_line(buffer, line, &i);
|
line = build_line(buffer, line, &i);
|
||||||
ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0'));
|
ft_memcpy(buffer, buffer + i + 1, ft_strlen_with_c(buffer + i, '\0'));
|
||||||
}
|
}
|
||||||
return (line);
|
return (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_next_line(int fd) {
|
char *get_next_line(int fd)
|
||||||
static char buffer[BUFFER_SIZE + 1] = "";
|
{
|
||||||
char *line;
|
static char buffer[BUFFER_SIZE + 1] = "";
|
||||||
|
char *line;
|
||||||
|
|
||||||
if (fd < 0 || BUFFER_SIZE <= 0)
|
if (fd < 0 || BUFFER_SIZE <= 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
line = get_the_line(fd, buffer);
|
line = get_the_line(fd, buffer);
|
||||||
if (!line)
|
if (!line)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
return (line);
|
return (line);
|
||||||
}
|
}
|
||||||
|
|||||||
+39
-106
@@ -6,120 +6,53 @@
|
|||||||
/* 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 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 <fcntl.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../libft/libft.h"
|
static int get_fd(int argc, char **argv)
|
||||||
#include "alcu.h"
|
{
|
||||||
#include "get_next_line.h"
|
if (argc == 2)
|
||||||
|
{
|
||||||
int check_char_int(char *str) {
|
return (open(argv[1], O_RDONLY));
|
||||||
int i;
|
}
|
||||||
|
else if (argc == 1)
|
||||||
i = -1;
|
return (0);
|
||||||
while (str[++i] && i < 100)
|
else
|
||||||
if (!(str[i] >= '0' && str[i] <= '9') && str[i] != '\n')
|
return (-1);
|
||||||
return (0);
|
|
||||||
|
|
||||||
return (1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_turn(int *line) {
|
int main(int argc, char *argv[])
|
||||||
char *temp;
|
{
|
||||||
int choice;
|
int fd;
|
||||||
|
int size;
|
||||||
|
int *lines;
|
||||||
|
|
||||||
while (1) {
|
fd = get_fd(argc, argv);
|
||||||
ft_putstr_fd("\nPlease choose between 1 and 3 items\n", 1);
|
if (fd < 0)
|
||||||
temp = get_next_line(0);
|
{
|
||||||
if (temp && ft_strlen(temp) > 0) {
|
ft_putstr_fd("ERROR", 2);
|
||||||
if (!check_char_int(temp)) {
|
return (1);
|
||||||
free(temp);
|
}
|
||||||
continue;
|
size = check_input(fd);
|
||||||
}
|
if (size < 0)
|
||||||
choice = ft_atoi(temp);
|
{
|
||||||
free(temp);
|
ft_putstr_fd("ERROR", 2);
|
||||||
if (choice > 0 && choice < 4) {
|
return (1);
|
||||||
if (choice > *line) {
|
}
|
||||||
ft_putstr_fd("-\nInvalid choice\n", 1);
|
close(fd);
|
||||||
continue;
|
fd = open(argv[1], O_RDONLY);
|
||||||
}
|
lines = fill_array(fd, size);
|
||||||
*line -= choice;
|
if (!lines)
|
||||||
break;
|
return (1);
|
||||||
} else {
|
game(lines, size);
|
||||||
ft_putstr_fd("-\nInvalid choice\n", 1);
|
close(fd);
|
||||||
continue;
|
free(lines);
|
||||||
}
|
return (0);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
if (argc != 2) {
|
|
||||||
ft_putstr_fd("ERROR", 2);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-32
@@ -6,7 +6,7 @@
|
|||||||
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
|
/* By: dgaillet <dgaillet@student.42lyon.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2026/03/28 16:06:30 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 <stddef.h>
|
#include <stddef.h>
|
||||||
#include <unistd.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;
|
size_t i;
|
||||||
|
int biggest;
|
||||||
|
|
||||||
i = 0;
|
biggest = 0;
|
||||||
while (i < nb_line) {
|
i = 0;
|
||||||
if (game_state[i] > biggest)
|
while (i < nb_line)
|
||||||
biggest = game_state[i];
|
{
|
||||||
i++;
|
if (game_state[i] > biggest)
|
||||||
}
|
biggest = game_state[i];
|
||||||
return (biggest);
|
i++;
|
||||||
|
}
|
||||||
|
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 biggest_line;
|
||||||
int j;
|
size_t i;
|
||||||
|
int j;
|
||||||
|
|
||||||
i = -1;
|
i = -1;
|
||||||
biggest_line = get_biggest_line(game_state, nb_line);
|
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])
|
j = -1;
|
||||||
write(1, " ", 1);
|
while (++j < biggest_line - game_state[i])
|
||||||
j = -1;
|
write(1, " ", 1);
|
||||||
while (++j < game_state[i]) {
|
j = -1;
|
||||||
write(1, "|", 1);
|
while (++j < game_state[i])
|
||||||
if (j < game_state[i] - 1)
|
{
|
||||||
write(1, " ", 1);
|
write(1, "|", 1);
|
||||||
}
|
if (j < game_state[i] - 1)
|
||||||
j = -1;
|
write(1, " ", 1);
|
||||||
while (++j < biggest_line - game_state[i])
|
}
|
||||||
write(1, " ", 1);
|
j = -1;
|
||||||
write(1, "\n", 1);
|
while (++j < biggest_line - game_state[i])
|
||||||
}
|
write(1, " ", 1);
|
||||||
|
write(1, "\n", 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user