FEAT: game design

This commit is contained in:
LucasCodeur
2026-03-28 17:49:08 +01:00
parent d6aa22de54
commit 1d96452240
2 changed files with 50 additions and 17 deletions
+49 -16
View File
@@ -34,34 +34,67 @@ int main(int argc, char *argv[])
} }
int size = check_input(fd); int size = check_input(fd);
printf("size %d\n", size);
if (size < 0) if (size < 0)
{ {
ft_putstr_fd("ERROR", 2); ft_putstr_fd("ERROR", 2);
return (1); return (1);
} }
fd = open(argv[1], O_RDONLY);
int* lines = fill_array(fd, size); int* 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]);
// }
// int player = 1;
for (int i = size - 1; i >= 0; i--)
{ {
char buffer[100] = ""; while (lines[i] != 0)
ft_putstr_fd("Player : ", 1);
char* temp = ft_itoa(i % 2 == 0 ? 1 : 2);
ft_putstr_fd(temp, 1);
ft_putstr_fd("\n", 1);
ft_putstr_fd("Enter the number of matchs to take off: ", 1);
int nb_read = read(0, buffer, sizeof(buffer));
if (nb_read > 0)
{ {
int temp = ft_atoi(buffer); char buffer[100] = "";
if (temp > 1 && temp < 4) // ft_putstr_fd("Player : ", 1);
lines[i] -= temp; // char* choice = ft_itoa(i % 2 == 0 ? 1 : 2);
else // ft_putstr_fd(choice, 1);
ft_putstr_fd("Between 1 and 3", fd); // ft_putstr_fd("\n", 1);
print_board(lines, 5); ft_putstr_fd("Please choose between 1 and 3 items\n", 1);
print_board(lines, size);
int nb_read = read(0, buffer, sizeof(buffer));
char* temp = NULL;
if (nb_read > 0)
{
int choice = ft_atoi(buffer);
// temp = ft_itoa(choice);
// ft_putstr_fd(temp, 1);
if (choice > 0 && choice < 4)
{
if (choice > lines[i])
{
ft_putstr_fd("-", 1);
ft_putstr_fd("\n", 1);
ft_putstr_fd("Invalid choice\n", 1);
i++;
free(temp);
continue ;
}
lines[i] -= choice;
}
else
{
ft_putstr_fd("-", 1);
ft_putstr_fd("\n", 1);
ft_putstr_fd("Invalid choice\n", 1);
i++;
free(temp);
continue ;
}
}
} }
} }
close(fd); close(fd);
free(lines);
return (0); return (0);
} }
+1 -1
View File
@@ -34,7 +34,7 @@ void print_board(int *game_state, size_t nb_line) {
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) { 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);