forked from skaptox/JuegoDelOso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.cpp
214 lines (174 loc) · 5.24 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
202
203
204
205
206
207
208
209
210
211
212
213
// Copyright (c) 2017 Richard Games. All rights reserved.
#include <time.h>
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <string>
#include "./ai.h"
using std::cout;
using std::endl;
int counter = 0;
std::string printState(const STATE &s) {
if (s == STATE::EMPTY)
return "EMPTY";
if (s == STATE::ERROR)
return "ERROR";
if (s == STATE::O)
return "O";
if (s == STATE::S)
return "S";
return "UNDEFINED";
}
std::string printMove(const Move &mv) {
std::string out = "Move :";
out += std::to_string(mv.row) + " - ";
out += std::to_string(mv.col) + ", ";
out += "L: " + printState(mv.state) + ", ";
out += "D: " + std::to_string(mv.dValue) + ", ";
out += "H: " + std::to_string(mv.hValue) + "\n";
return out;
}
void showBoard(const Board &board) {
for (int i = 0; i < board.rowsTam(); ++i) {
for (int j = 0; j < board.colsTam(); ++j) {
if ( board.statusSquare(j, i) == STATE::S)
std::cout << "| S ";
else if (board.statusSquare(j, i) == STATE::O)
std::cout << "| O ";
else
std::cout << "| ";
}
std::cout << std::endl;
}
}
int playerToggle(int player) {
return player*-1;
}
int evalBoard(const Board &board , int player1, int player2) {
return (player1 - player2);
}
std::vector<Move> generateMoves(const Board &board) {
std::vector<Move> moves;
int rows = board.rowsTam();
int cols = board.colsTam();
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (board.statusSquare(i, j) == STATE::EMPTY) {
moves.push_back(Move(i, j, STATE::O));
moves.push_back(Move(i, j, STATE::S));
}
}
}
return moves;
}
void deterministValue(std::vector<Move> &mvs, Board &board) {
for (Move &mv : mvs) {
board.setState(mv);
mv.dValue = board.osoWordAround(mv);
board.unSetState(mv);
}
}
void sortMoves(std::vector<Move> &mvs) {
// Order from highest to lowest without swap equals
std::sort(mvs.rbegin(), mvs.rend(), std::less<Move>());
}
/*
function alphabeta(node, depth, α, β, Player)
if depth = 0 or node is a terminal node
return the heuristic dValue of node
if Player = MaxPlayer
for each child of node
α := max(α, alphabeta(child, depth-1, α, β, not(Player) ))
if β ≤ α
break (* Beta cut-off *)
return α
else
for each child of node
β := min(β, alphabeta(child, depth-1, α, β, not(Player) ))
if β ≤ α
break (* Alpha cut-off *)
return β
(* Llamada INicial: *)
alphabeta(ORIGEN NODO, depth, -infinity, +infinity, MaxPlayer
*/
int alphaBeta(int alpha, int beta, int depth, Board &board, bool turn,
int player1, int player2) {
counter++;
if (depth == 0 || board.statusGame() == GAME_STATUS::END)
return evalBoard(board, player1, player2);
std::vector<Move> movements(generateMoves(board));
for (Move &move : movements) {
board.setState(move);
move.dValue = board.osoWordAround(move.row, move.col);
int points = move.dValue;
if (points > 0) {
if (turn)
alpha = std::max(alpha, alphaBeta(alpha, beta, depth - 1, board, turn,
player1 + points , player2));
else
beta = std::min(beta, alphaBeta(alpha, beta, depth - 1, board, turn,
player1, player2 + points));
} else { // points == 0
if (turn)
alpha = std::max(alpha, alphaBeta(alpha , beta, depth - 1, board, !turn,
player1 , player2));
else
beta = std::min(beta, alphaBeta(alpha, beta, depth - 1, board, !turn,
player1, player2));
}
if (turn) {
if (beta <= alpha) {
board.unSetState(move);
break;
}
} else {
if (beta <= alpha) {
board.unSetState(move);
break;
}
}
board.unSetState(move);
}
return turn ? alpha : beta;
}
Move playAI(Board &board, int d, bool turn, int P1G, int P2G) {
if (board.statusGame() == GAME_STATUS::END)
return Move();
std::vector<Move> movements = generateMoves(board);
deterministValue(movements, board);
sortMoves(movements);
int hValue, dValue;
int maxHValue = -1000;
int minHValue = 1000;
for (Move &move : movements) {
board.setState(move);
dValue = move.dValue;
if (dValue > 0) {
if (turn) // maximize
hValue = alphaBeta(-500, 500, d - 1, board, turn, P1G + dValue, P2G);
else // minimize
hValue = alphaBeta(-500, 500, d - 1, board, turn, P1G, P2G + dValue);
} else { // move.dValue == 0
hValue = alphaBeta(-500, 500, d - 1, board, !turn, P1G, P2G);
}
if (hValue < minHValue) {
minHValue = hValue;
}
if (hValue > maxHValue) {
maxHValue = hValue;
}
move.hValue = hValue;
board.unSetState(move);
}
std::function<bool(const Move& mv)> lambda;
if (turn) {
lambda = [&] (const Move& mv) { return mv.hValue < maxHValue; };
} else {
lambda = [&] (const Move& mv) { return mv.hValue > minHValue; };
}
movements.erase(std::remove_if(movements.begin(), movements.end(), lambda),
movements.end());
srand(time(0));
int mod = movements.empty() ? 1 : movements.size();
return movements[rand() % mod];
}