-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI.cpp
201 lines (169 loc) · 5.85 KB
/
AI.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "AI.hpp"
AI::AI(const t_ia_type& t) {
_type = t;
}
AI::~AI() {
}
int AI::higher(const int& a, const int& b) {
if (a > b)
return a;
return b;
}
int AI::verifAlign(std::vector<int> tab) {
int ret = 0;
for (std::vector<int>::iterator it = tab.begin(); it != tab.end(); ++it) {
if ((*it) < 0 && (*it) > -5)
(*it) = (*it) * -1;
if ((*it) > ret)
ret = (*it);
}
return ret;
}
int AI::howManyCoin(const int& x, const int& y) {
std::vector<int> tab;
t_flag teams;
int ret;
if (_mapRule[y][x] == _initTeam)
teams = _initTeam;
else
teams = invertTeam(_initTeam);
tab.push_back(checkWinDiagonalOne(y, x, teams));
tab.push_back(checkWinDiagonalTwo(y, x, teams));
tab.push_back(checkWinHorizontal(y, x, teams));
tab.push_back(checkWinVertical(y, x, teams));
ret = verifAlign(tab);
ret = ret * 100;
return ret;
}
int AI::verifEat(const int& x, const int& y) {
t_flag team;
int ret = 0;
if (_mapRule[y][x] == _initTeam)
team = _initTeam;
else
team = invertTeam(_initTeam);
if ((y - 2 >= 0 && y + 1 <= 18 && !canIEat(((y + 1) * 19) + x, ((y - 1) * 19) + x, ((y - 2) * 19) + x, team)) ||
(y - 1 >= 0 && y + 2 <= 18 && !canIEat(((y - 1) * 19) + x, ((y + 1) * 19) + x, ((y + 2) * 19) + x, team)) ||
(x - 2 >= 0 && x + 1 <= 18 && !canIEat((y * 19) + x + 1, (y * 19) + x - 1, (y * 19) + x - 2, team)) ||
(x - 1 >= 0 && x + 2 <= 18 && !canIEat((y * 19) + x - 1, (y * 19) + x + 1, (y * 19) + x + 2, team)) ||
(x - 2 >= 0 && y - 2 >= 0 && x + 1 <= 18 && y + 1 <= 18 && !canIEat(((y + 1) * 19) + x + 1, ((y - 1) * 19) + x - 1, ((y - 2) * 19) + x - 2, team)) ||
(x - 1 >= 0 && y - 1 >= 0 && x + 2 <= 18 && y + 2 <= 18 && !canIEat(((y - 1) * 19) + x - 1, ((y + 1) * 19) + x + 1, ((y + 2) * 19) + x + 2, team)) ||
(x - 2 >= 0 && y + 2 <= 18 && x + 1 <= 18 && y - 1 >= 0 && !canIEat(((y - 1) * 19) + x + 1, ((y + 1) * 19) + x - 1, ((y + 2) * 19) + x - 2, team)) ||
(x - 1 >= 0 && y + 1 <= 18 && x + 2 <= 18 && y - 2 <= 0 && !canIEat(((y + 1) * 19) + x - 1, ((y - 1) * 19) + x + 1, ((y - 2) * 19) + x + 2, team))) {
if (team == _initTeam)
ret = ((_nbPionTwo / 2) + 1) * 100;
else
ret = ((_nbPionOne / 2) + 1) * 100;
}
return ret + 1000;
}
int AI::checkBlock(const int& x, const int& y) {
std::vector<int> tab;
int ret = 0;
tab.push_back(checkWinDiagonalOne(y, x, invertTeam(_initTeam)));
tab.push_back(checkWinDiagonalTwo(y, x, invertTeam(_initTeam)));
tab.push_back(checkWinHorizontal(y, x, invertTeam(_initTeam)));
tab.push_back(checkWinVertical(y, x, invertTeam(_initTeam)));
for (std::vector<int>::iterator it = tab.begin(); it != tab.end(); ++it) {
if ((*it) < 0 && (*it) >= -5)
ret += ((*it) * -1) * 200;
else if ((*it) < -5 && (*it) >= -10)
ret += ((*it) * -1) * 200;
}
return ret;
}
int AI::eval() {
int ret = 0;
int retEat = 0;
int max = -1000000000;
if (verifWin() == _initTeam)
return 1000000000;
if (verifWin() == invertTeam(_initTeam))
return -1000000000;
for (int y = 0; y != 19; y++) {
for (int x = 0; x != 19; x++) {
if (_mapRule[y][x] != FREE && _mapRule[y][x] != TRY) {
retEat = verifEat(x, y);
ret += howManyCoin(x, y);
ret = ret + retEat;
if (_type == DEF && _mapRule[y][x] == invertTeam(_initTeam))
ret += checkBlock(x, y);
if (ret > max)
max = ret;
}
}
}
return max;
}
int AI::verifWin() {
if (!checkWin(TEAM_1))
return TEAM_1;
if (!checkWin(TEAM_2))
return TEAM_2;
return 0;
}
int AI::negamax(const t_flag& team, const int& profondeur) {
int max;
int tmp;
max = eval();
if (profondeur == 0 || verifWin() != 0)
return max;
for (int y = 0; y != 19; y++) {
for (int x = 0; x != 19; x++) {
if (_mapRule[y][x] == FREE) {
_mapRule[y][x] = team;
tmp = negamax(invertTeam(team), profondeur - 1);
if ((tmp > max))
max = tmp;
_mapRule[y][x] = FREE;
}
}
}
return max;
}
int AI::minMax(const t_flag& team) {
int max = -1000000000;
int tmp, maxY = 0, maxX = 0;
int profondeur = 1;
for (int y = 0; y != 19; y++) {
for (int x = 0; x != 19; x++) {
if (_mapRule[y][x] == FREE) {
_mapRule[y][x] = team;
tmp = negamax(invertTeam(team), profondeur - 1);
if (tmp > max) {
max = tmp;
maxY = y;
maxX = x;
}
_mapRule[y][x] = FREE;
}
}
}
return (maxY * 19) + maxX;
}
void AI::displayInfo(const int& poid) {
std::cout << YELLOW << "\n\n";
std::cout << "------------------------" << std::endl;
std::cout << "Poid = " << poid << std::endl;
for (int i = 0; i != 19; i++) {
std::cout << " ";
for (int j = 0; j!= 19; j++) {
std::cout << _mapRule[i][j];
}
std::cout << END << std::endl;
}
}
int AI::launchAI(const int tab[19][19], const t_flag& team, const int& pionOne, const int& pionTwo) {
int toRet;
for (int y = 0; y != 19; y++) {
for (int x = 0; x != 19; x++) {
_mapRule[y][x] = tab[y][x];
}
}
_nbPionOne = pionOne;
_nbPionTwo = pionTwo;
_initTeam = team;
toRet = minMax(team);
std::cout << MAGENTA << "[IA] Value ask : " << toRet << END << std::endl;
return toRet;
}