some fix for game management + fix for ai (it's unbeatable)

This commit is contained in:
2026-03-28 20:32:16 +01:00
parent 60632fc2a7
commit 004b073669
5 changed files with 72 additions and 43 deletions
+8 -3
View File
@@ -1,5 +1,10 @@
8 2
9
10
5 5
3 9
2 8
2 2
1
8
10
BIN
View File
Binary file not shown.
Executable
+15
View File
@@ -0,0 +1,15 @@
#!/bin/bash
if [ $# -lt 2 ]; then
echo "usage: bash map_gen.sh [size] [range]"
exit 1
fi
unset var
for i in $( seq 1 $1 )
do
var+="\n$(( RANDOM % $2 + 1))"
done
var="${var}\n";
echo -e $var | tail -n +2
+2 -5
View File
@@ -15,16 +15,13 @@
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;
while (best <= line - 2) else
best += 2;
} else {
best = 1; best = 1;
while (best <= line - 4) { while (best <= line - 4) {
best += 4; best += 4;
} }
}
return (best); return (best);
} }
+45 -33
View File
@@ -12,58 +12,70 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "../libft/libft.h" #include "../libft/libft.h"
#include "alcu.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')
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) { void game(int *lines, int size) {
int ai_turn = 1; int ai_turn = 1;
int nb_read;
int choice; int choice;
for (int i = size - 1; i >= 0; i--) { for (int i = size - 1; i >= 0; i--) {
while (lines[i] != 0) { while (lines[i] != 0) {
char buffer[100] = ""; ft_putstr_fd("\n------------------------------\n", 1);
// ft_putstr_fd("Player : ", 1);
// char* choice = ft_itoa(i % 2 == 0 ? 1 : 2);
// ft_putstr_fd(choice, 1);
// ft_putstr_fd("\n", 1);
if (!ai_turn) { if (!ai_turn) {
print_board(lines, size); print_board(lines, size);
ft_putstr_fd("\nPlease choose between 1 and 3 items\n", 1); player_turn(&lines[i]);
nb_read = read(0, buffer, sizeof(buffer));
// char *temp = NULL;
if (nb_read > 0) {
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;
}
}
} 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);
ft_putstr_fd("AI play ", 1); ft_putstr_fd("AI play ", 1);
ft_putnbr_fd(choice, 1); ft_putnbr_fd(choice, 1);
ft_putstr_fd("\n\n", 1); ft_putstr_fd("\n", 1);
lines[i] -= choice; lines[i] -= choice;
} }
ai_turn = ai_turn == 0; ai_turn = ai_turn == 0;