This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax_AI.c
182 lines (139 loc) · 4.7 KB
/
minimax_AI.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
******************************************************************
The code will determine the behavior of CPU (AI)
The only AI algorithm used here is Minimax
******************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include "chomp.h"
#include "const.h"
#include <limits.h> //a library that defines INT_MIN and INT_MAX for Minimax
/*
shadow_clone function will make an exact copy of the board when it's AI's turn
WHY ? Well it will be usuful when the AI has to make its move, so instead of missing around
with the real board, the AI can test its strategy with a copy
bfo(before) is the board during t and aft(after) is the one during t+dt
aft is the board that will be tested by the CPU (AI)
*/
void shadow_clone(int bfo[ROWS][COLS], int aft[ROWS][COLS])
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
bfo[i][j] = aft[i][j];
}
}
}
int game_over0(int board[ROWS][COLS])
{
return board[0][0] == 0;
}
/*
The code below is for the minimax algorithm
The variable whos_turn defines the current player (Human or AI)
opt_score is the optimal score
The depth is going to limit the AI to certain number of possibilities instead of crashing out with tons of cases
Depth will be limited to only 5.
Q. "bUt WhY oNlY 5 ?" A."BECAUSE I SAID SO"
During Maximisation's phase, opt_score is set to INT_MIN
During Minimisation's phase, opt_score is set to INT_MAX
*/
int minimax(int table[ROWS][COLS],int depth ,int whos_turn)
{
if (game_over0(table))
{
return whos_turn ? -1 : 1;
}
if (depth == 0) // if depth = 0, the match is finished. Q."BuT wHy ?" A."bc no more possiblities"
{
return 0;
}
int opt_score;
if (whos_turn)
{
opt_score = INT_MIN; //it's the turn of max player
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (table[i][j] == 1) //Running through the board to find a valid square to destroy
{
int board_t[ROWS][COLS]; //Making a clone of the board
shadow_clone(board_t, table);
int destroyed = calculate_num_to_delete(board_t, i, j);
if (destroyed <= NUM_MAX_TO_DELETE)
{
int score = minimax(board_t,depth-1 ,0);
opt_score = (score > opt_score) ? score : opt_score;
}
}
}
}
}
else
{
opt_score = INT_MAX;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (table[i][j] == 1)
{
int board_t[ROWS][COLS];
shadow_clone(board_t, table);
int destroyed = calculate_num_to_delete(board_t, i, j);
if (destroyed <= NUM_MAX_TO_DELETE)
{
int score = minimax(board_t, depth-1 , 1);
opt_score = (score < opt_score) ? score : opt_score;
}
}
}
}
}
return opt_score;
}
/*
****************************************
The function arise_solider, will simulate the AI behavior
To be clear, it will determine how does the cpu(AI) move
****************************************
*/
void arise_solider(int board[ROWS][COLS])
{
int opt_score = INT_MIN;
int bestLig = -1;
int bestCol = -1;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (board[i][j] == 1) //is this cell available ?
{
int plateau_t[ROWS][COLS];
shadow_clone(plateau_t, board);
int destroyed = calculate_num_to_delete(plateau_t, i, j);
if (destroyed <= NUM_MAX_TO_DELETE)
{
int score = minimax(plateau_t, 5, 0);
if (score > opt_score)
{
opt_score = score;
bestLig = i;
bestCol = j;
}
}
}
}
}
if (bestLig != -1 && bestCol != -1) // AI will choose the best cell
{
printf("The CPU had chosen (%d, %d)\n", bestLig, bestCol);
calculate_num_to_delete(board, bestLig, bestCol);
}
}