-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimax.cpp
212 lines (188 loc) · 6.26 KB
/
minimax.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
#include "minimax.h"
// ---------------------------------- MINIMAX --------------------------------
int alphabeta_maxi(int depth, int alpha, int beta, Game &g) {
if (depth == 0) {
return evaluate(g.m_board, g.m_pieces, g.e_board, g.e_pieces);
}
// {src_x, src_y, dst_x, dst_y, piece_type, priority_code}
std::vector<std::vector<char>> all_moves = get_moves_m(g);
std::sort(all_moves.begin(), all_moves.end(), order_moves_by_priority);
for (auto &move : all_moves) {
int score;
// holds info to undo the move later: {piece_taken}
std::vector<char> info = g.apply_move_m(move);
if (g.is_check_m()) {
score = LOW - 1;
} else {
score = alphabeta_mini(depth - 1, alpha, beta, g);
}
// partea de alpha-beta
if (score >= beta) {
g.undo_move_m(info, move);
return beta;
}
if (score > alpha) {
alpha = score;
}
g.undo_move_m(info, move);
}
return alpha;
}
int alphabeta_mini(int depth, int alpha, int beta, Game &g){
if (depth == 0) {
return -evaluate(g.e_board, g.e_pieces, g.m_board, g.m_pieces);
}
// {src_x, src_y, dst_x, dst_y, piece_type, priority_code}
std::vector<std::vector<char>> all_moves = get_moves_e(g);
std::sort(all_moves.begin(), all_moves.end(), order_moves_by_priority);
for (auto &move : all_moves) {
std::vector<char> info = g.apply_move_e(move);
// holds info to undo the move later: {piece_taken}
int score;
if (g.is_check_e()) {
score = HIGH + 1; // not a good move for the mini part as it enters chess
}else {
score = alphabeta_maxi(depth - 1, alpha, beta, g);
}
// partea de alpha-beta
if (score <= alpha) {
g.undo_move_e(info, move);
return alpha;
}
if (score < beta) {
beta = score;
}
g.undo_move_e(info, move);
}
return beta;
}
// ---------------------------------- EVALUATE --------------------------------
int evaluate(std::vector<std::vector<char>> &p_board,std::vector<std::vector<char>> &p_pieces,
std::vector<std::vector<char>> &h_board, std::vector<std::vector<char>> &h_pieces) {
int score_p = 0;
int score_h = 0;
// sum of pieces in the game
// my pieces
for (auto &v : p_pieces) {
if (v[0] != PIECE_TAKEN && v[1] != PIECE_TAKEN) {
score_p += get_score(v[2]);
// check if I am in check
if (v[2] == KING_M) {
if (check_check(v[0], v[1], p_board) == 0) {
score_p += SCORE_KING_SAFE;
} else {
score_p += SCORE_KING_THREATENED;
}
}
}
}
// enemy's pieces
for (auto &v : h_pieces) {
if (v[0] != PIECE_TAKEN && v[1] != PIECE_TAKEN) {
score_h += get_score(v[2]);
// check if the enemy is in check
if (v[2] == KING_M) {
if (check_check(v[0], v[1], h_board) == 0) {
score_h += SCORE_KING_SAFE;
} else {
score_h += SCORE_KING_THREATENED;
}
}
}
}
// pieces that i can attack
for (auto &v : p_pieces) {
if (v[0] != PIECE_TAKEN && v[1] != PIECE_TAKEN) {
// a vector of {x, y, piece type}
std::vector<std::vector<char>> victims = check_attack(v[0], v[1], p_board);
int past_maxi_score = 0;
for (auto &victim : victims) {
int victim_piece = victim[2] - 10;
int victim_score = get_score(victim_piece);
if (victim_score > past_maxi_score) {
past_maxi_score = victim_score;
}
}
score_p += past_maxi_score - get_score(v[2]);
}
}
// pieces that are in danger
for (auto &v : p_pieces) {
if (v[0] != PIECE_TAKEN && v[1] != PIECE_TAKEN) {
// a vector of {x, y, piece type}
std::vector<std::vector<char>> perpetrators = check_attackers(v[0], v[1], p_board);
for (auto &perpetrator : perpetrators) {
int perpetrator_piece = perpetrator[2] - 10;
score_p -= get_score(v[2]) * (SCORE_INV_PRO / perpetrator_piece);
}
}
}
return (score_p - score_h);
}
int get_score(const int &piece_type) {
switch (piece_type) {
case PAWN_M:
return SCORE_PAWN;
case ROOK_M:
return SCORE_ROOK;
case KNIGHT_M:
return SCORE_KNIGHT;
case WHITE_BISHOP_M:
return SCORE_WHITE_BISHOP;
case BLACK_BISHOP_M:
return SCORE_BLACK_BISHOP;
case QUEEN_M:
return SCORE_QUEEN;
case KING_M:
return SCORE_KING;
default:
std::cout << "#ERROR: Found a piece_type that did not mach anything in \"get_score\"" << std::endl;
return 0;
}
}
// --------------------------------- GET_MOVES_X ------------------------------
// return vector structure: {src_x, src_y, dst_x, dst_y, piece_type, priority_code}
std::vector<std::vector<char>> get_moves_m(Game &g) {
std::vector<std::vector<char>> moves;
return get_moves(g.m_pieces, g.m_board);
}
// return vector structure: {src_x, src_y, dst_x, dst_y, piece_type, priority_code}
std::vector<std::vector<char>> get_moves_e(Game &g) {
std::vector<std::vector<char>> moves;
return get_moves(g.e_pieces, g.e_board);
}
// return vector structure: {src_x, src_y, dst_x, dst_y, piece_type, priority_code}
// priority code:
// 1 - an empty cell
// 2 - an enemy piece
// 3 - chess for the enemy << TODO >>
std::vector<std::vector<char>> get_moves(std::vector<std::vector<char>> &pieces, std::vector<std::vector<char>> &board) {
std::vector<std::vector<char>> moves;
// structure for piece: {x, y, piece_type}
for (std::vector<char> piece : pieces) {
if (piece[0] < 0 || piece[1] < 0) {
continue;
}
// {x, y, return code from "check_validity"}
std::vector<std::vector<char>> temp = get_piece_directions(piece[0], piece[1], board);
for (auto &x : temp) {
std::vector<char> v;
v.push_back(piece[0]); // src_x
v.push_back(piece[1]); // src_y
v.push_back(x[0]); // dst_x
v.push_back(x[1]); // dst_y
v.push_back(piece[2]); // piece_type
v.push_back(x[2]); // priority_code
moves.push_back(v);
}
}
return moves;
}
// {src_x, src_y, dst_x, dst_y, piece_type, priority_code}
bool order_moves_by_priority(const std::vector<char> &a, const std::vector<char> &b) {
if (a[5] != b[5]) {
return a[5] > b[5]; // order descending by the priority of the piece that can be taken
} else {
return a[4] < b[4]; // if priority is equal, order ascending by the type of piece doing the attack
}
}