diff --git a/alcu.map b/alcu.map index 0b470c6..e7adaf8 100644 --- a/alcu.map +++ b/alcu.map @@ -1,5 +1,10 @@ -8 +2 +9 +10 5 -3 -2 +9 +8 2 +1 +8 +10 diff --git a/alum1 b/alum1 index 31f2243..b3f7828 100755 Binary files a/alum1 and b/alum1 differ diff --git a/map_gen.sh b/map_gen.sh new file mode 100755 index 0000000..ad8eaf3 --- /dev/null +++ b/map_gen.sh @@ -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 + diff --git a/src/ai.c b/src/ai.c index e5fc61d..4e9ee81 100644 --- a/src/ai.c +++ b/src/ai.c @@ -15,15 +15,12 @@ static int best_to_take(int prev_should_start, int line) { int best; - if (prev_should_start) { + if (prev_should_start) best = 2; - while (best <= line - 2) - best += 2; - } else { + else best = 1; - while (best <= line - 4) { - best += 4; - } + while (best <= line - 4) { + best += 4; } return (best); } diff --git a/src/main.c b/src/main.c index c5840a7..0eea6fb 100644 --- a/src/main.c +++ b/src/main.c @@ -12,58 +12,70 @@ #include #include +#include #include #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') + 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 nb_read; int choice; for (int i = size - 1; i >= 0; i--) { while (lines[i] != 0) { - char buffer[100] = ""; - // ft_putstr_fd("Player : ", 1); - // char* choice = ft_itoa(i % 2 == 0 ? 1 : 2); - // ft_putstr_fd(choice, 1); - // ft_putstr_fd("\n", 1); + ft_putstr_fd("\n------------------------------\n", 1); if (!ai_turn) { print_board(lines, size); - ft_putstr_fd("\nPlease choose between 1 and 3 items\n", 1); - 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; - } - } + 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\n", 1); + ft_putstr_fd("\n", 1); lines[i] -= choice; } ai_turn = ai_turn == 0;