-
Notifications
You must be signed in to change notification settings - Fork 0
/
ia_loop.c
60 lines (54 loc) · 1.43 KB
/
ia_loop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
** EPITECH PROJECT, 2020
** IA loop
** File description:
** all the loop functions related to the IA of the matchstick game
*/
#include "./header.h"
int ia_loop(game_t *game)
{
get_matches_nbr(game);
ia_match_check(game);
my_putstr("\nAI's turn...\n");
my_putstr("AI removed ");
my_put_nbr(game->ia_match);
my_putstr(" match(es) from line ");
my_put_nbr(game->ia_line);
my_putchar('\n');
ia_remove_match(game);
get_matches_nbr(game);
game->turn++;
return (1);
}
int ia_match_check(game_t *game)
{
game->ia_match = 0;
game->ia_line = 1;
for (; game->match_nbr[game->ia_line] <= 1
&& game->ia_line < game->lines+1;
game->ia_line++);
if (game->ia_line > game->lines)
ia_single_match_check(game);
if (game->match_nbr[game->ia_line] > game->pick)
game->ia_match = game->pick;
else game->ia_match = game->match_nbr[game->ia_line] - 1;
if (game->ia_match <= 0)
game->ia_match = 1;
}
int ia_single_match_check(game_t *game)
{
game->ia_line = 1;
for (; game->match_nbr[game->ia_line] != 1; game->ia_line++);
}
int ia_remove_match(game_t *game)
{
int len = (game->lines * 2) + 1;
if (game->end == 0)
return (0);
for (int j = len; j != 0; j--)
if (game->map[game->ia_line][j] == '|' && game->ia_match != 0) {
game->map[game->ia_line][j] = ' ';
game->ia_match--;
}
return (0);
}