norminette

This commit is contained in:
2026-03-28 20:55:39 +01:00
parent 004b073669
commit bf668cf5e6
8 changed files with 342 additions and 358 deletions
BIN
View File
Binary file not shown.
-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> +#+ +:+ +#+ */ /* 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; int goal;
goal = best_to_take(prev_should_start, line); goal = best_to_take(prev_should_start, line);
if (line - goal < 3) { if (line - goal < 3)
return (line - goal) + 1; {
} else { return ((line - goal) + 1);
}
else
{
return (1); 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) if (prev_should_start && line == 1)
return (!prev_should_start); return (!prev_should_start);
if (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); return (line % 4 != 0);
} }
int ai(int *gamestate, int nb_line) { int ai(int *gamestate, int nb_line)
{
int prev_should_start; int prev_should_start;
int i; 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 (i == nb_line - 1 || gamestate[i + 1] == 0)
{
if (gamestate[i] == 1) if (gamestate[i] == 1)
return (1); return (1);
return (to_play(prev_should_start, gamestate[i])); return (to_play(prev_should_start, gamestate[i]));
+9 -7
View File
@@ -6,25 +6,27 @@
/* 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 check_input(int fd) {
int i; int i;
int j; int j;
char *line = NULL; char *line;
line = NULL;
line = get_next_line(fd); line = get_next_line(fd);
j = 0; j = 0;
while (line != NULL) { while (line != NULL)
{
i = -1; i = -1;
while (line[++i]) while (line[++i])
if (!(line[i] >= '0' && line[i] <= '9') && line[i] != '\n') 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> +#+ +:+ +#+ */ /* 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);
+27 -17
View File
@@ -6,15 +6,15 @@
/* 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 ft_strlen_with_c(char *str, char c) {
size_t i; size_t i;
i = 0; i = 0;
@@ -25,13 +25,15 @@ size_t ft_strlen_with_c(char *str, char c) {
return (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') if (str[i] == '\n')
return (1); return (1);
i++; i++;
@@ -39,17 +41,20 @@ size_t detect_newline(char *str) {
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 i;
size_t j; 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]; line[i] = c_remaining[i];
i++; i++;
} }
while (src[j] && src[j] != '\n') { while (src[j] && src[j] != '\n')
{
line[i + j] = src[j]; line[i + j] = src[j];
src[j] = 0; src[j] = 0;
j++; j++;
@@ -59,16 +64,18 @@ static void fill_line(char *line, char *c_remaining, char *src, size_t *index) {
*index = j; *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; char *line;
size_t size; size_t size;
size_t is_jump; 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); free(c_remaining);
return (NULL); return (NULL);
} }
@@ -80,14 +87,16 @@ char *build_line(char *src, char *c_remaining, size_t *index) {
return (line); 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); *nb_read = read(fd, buffer, BUFFER_SIZE);
if (*nb_read < 0) if (*nb_read < 0)
return; return ;
buffer[*nb_read] = '\0'; buffer[*nb_read] = '\0';
} }
static char *get_the_line(int fd, char *buffer) { static char *get_the_line(int fd, char *buffer)
{
char *line; char *line;
size_t i; size_t i;
ssize_t nb_read; ssize_t nb_read;
@@ -115,7 +124,8 @@ static char *get_the_line(int fd, char *buffer) {
return (line); return (line);
} }
char *get_next_line(int fd) { char *get_next_line(int fd)
{
static char buffer[BUFFER_SIZE + 1] = ""; static char buffer[BUFFER_SIZE + 1] = "";
char *line; char *line;
+53 -38
View File
@@ -6,70 +6,83 @@
/* 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 20:54:09 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "../libft/libft.h" #include "../libft/libft.h"
#include "alcu.h" #include "alcu.h"
#include "get_next_line.h" #include "get_next_line.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
int check_char_int(char *str) { int check_char_int(char *str)
{
int i; int i;
i = -1; i = -1;
while (str[++i] && i < 100) while (str[++i] && i < 100)
if (!(str[i] >= '0' && str[i] <= '9') && str[i] != '\n') if (!(str[i] >= '0' && str[i] <= '9') && str[i] != '\n')
return (0); return (0);
return (1); return (1);
} }
void player_turn(int *line) { void player_turn(int *line)
{
char *temp; char *temp;
int choice; int choice;
while (1) { while (1)
{
ft_putstr_fd("\nPlease choose between 1 and 3 items\n", 1); ft_putstr_fd("\nPlease choose between 1 and 3 items\n", 1);
temp = get_next_line(0); temp = get_next_line(0);
if (temp && ft_strlen(temp) > 0) { if (temp && ft_strlen(temp) > 0)
if (!check_char_int(temp)) { {
if (!check_char_int(temp))
{
free(temp); free(temp);
continue; continue ;
} }
choice = ft_atoi(temp); choice = ft_atoi(temp);
free(temp); free(temp);
if (choice > 0 && choice < 4) { if (choice > 0 && choice < 4)
if (choice > *line) { {
if (choice > *line)
{
ft_putstr_fd("-\nInvalid choice\n", 1); ft_putstr_fd("-\nInvalid choice\n", 1);
continue; continue ;
} }
*line -= choice; *line -= choice;
break; break ;
} else { }
else
{
ft_putstr_fd("-\nInvalid choice\n", 1); ft_putstr_fd("-\nInvalid choice\n", 1);
continue; continue ;
} }
} }
} }
} }
void game(int *lines, int size) { void game(int *lines, int size)
int ai_turn = 1; {
int ai_turn;
int choice; int choice;
for (int i = size - 1; i >= 0; i--) { ai_turn = 1;
while (lines[i] != 0) { for (int i = size - 1; i >= 0; i--)
{
while (lines[i] != 0)
{
ft_putstr_fd("\n------------------------------\n", 1); ft_putstr_fd("\n------------------------------\n", 1);
if (!ai_turn) { if (!ai_turn)
{
print_board(lines, size); print_board(lines, size);
player_turn(&lines[i]); player_turn(&lines[i]);
} else { }
else
{
print_board(lines, size); print_board(lines, size);
ft_putstr_fd("\n", 1); ft_putstr_fd("\n", 1);
choice = ai(lines, size); choice = ai(lines, size);
@@ -87,36 +100,38 @@ void game(int *lines, int size) {
ft_putstr_fd("You win ! Well done\n", 1); ft_putstr_fd("You win ! Well done\n", 1);
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
if (argc != 2) { {
int fd;
int size;
int *lines;
if (argc != 2)
{
ft_putstr_fd("ERROR", 2); ft_putstr_fd("ERROR", 2);
return (1); return (1);
} }
fd = open(argv[1], O_RDONLY);
int fd = open(argv[1], O_RDONLY); if (fd == -1)
{
if (fd == -1) {
ft_putstr_fd("ERROR", 2); ft_putstr_fd("ERROR", 2);
return (1); return (1);
} }
size = check_input(fd);
int size = check_input(fd); if (size < 0)
if (size < 0) { {
ft_putstr_fd("ERROR", 2); ft_putstr_fd("ERROR", 2);
return (1); return (1);
} }
close(fd); close(fd);
fd = open(argv[1], O_RDONLY); fd = open(argv[1], O_RDONLY);
int *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++) // for (int i = 0; i < size; i++)
// { // {
// printf("lines[i] : %d\n", lines[i]); // printf("lines[i] : %d\n", lines[i]);
// } // }
// int player = 1; // int player = 1;
game(lines, size); game(lines, size);
close(fd); close(fd);
+13 -7
View File
@@ -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,12 +14,15 @@
#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; size_t i;
int biggest = 0; int biggest;
biggest = 0;
i = 0; i = 0;
while (i < nb_line) { while (i < nb_line)
{
if (game_state[i] > biggest) if (game_state[i] > biggest)
biggest = game_state[i]; biggest = game_state[i];
i++; i++;
@@ -27,19 +30,22 @@ static int get_biggest_line(int *game_state, size_t nb_line) {
return (biggest); 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; int biggest_line;
size_t i; size_t i;
int j; 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; j = -1;
while (++j < biggest_line - game_state[i]) while (++j < biggest_line - game_state[i])
write(1, " ", 1); write(1, " ", 1);
j = -1; j = -1;
while (++j < game_state[i]) { while (++j < game_state[i])
{
write(1, "|", 1); write(1, "|", 1);
if (j < game_state[i] - 1) if (j < game_state[i] - 1)
write(1, " ", 1); write(1, " ", 1);