-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminmax.cpp
More file actions
175 lines (166 loc) · 6.74 KB
/
minmax.cpp
File metadata and controls
175 lines (166 loc) · 6.74 KB
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
#include <iostream>
#include <string>
#include <tuple>
#include <limits>
#include <vector>
#include "debug.h"
#include "moves.h"
#include "actions.h"
#include "utils.h"
#include "minmax.h"
u64 playMinimax(std::tuple<u64, u64> board) {
return std::get<MOVE>(_minimax(board, 0, 1, 6));
}
std::tuple<u64, int> _minimax(std::tuple<u64, u64> board, int ply, int player, int maxDepth) {
if (ply >= maxDepth) {
return std::make_tuple<u64, int>(~0UL, evaluate(board));
}
if (player > 0) {
int maxEval = std::numeric_limits<int>::min();
std::vector<u64> moveList = get_moves_list(std::get<AI>(board), std::get<OPPONENT>(board));
if (moveList.size() == 0) {
moveList.push_back(0UL);
}
std::tuple<u64, int> bestMove = std::make_tuple(moveList[0], 0);
for (int i = 0; i < moveList.size(); ++i) {
std::tuple<u64, u64> newBoard;
if (moveList[i] == 0UL) {
newBoard = std::make_tuple(std::get<AI>(board), std::get<OPPONENT>(board));
}else {
newBoard = play(moveList[i], std::get<AI>(board), std::get<OPPONENT>(board), true);
}
std::tuple<u64, int> tempMove = _minimax(newBoard, ply+1, -player, maxDepth);
if (std::get<VALUE>(tempMove) > maxEval) {
maxEval = std::get<VALUE>(tempMove);
std::get<VALUE>(bestMove) = std::get<VALUE>(tempMove);
std::get<MOVE>(bestMove) = moveList[i];
}
}
return bestMove;
}else {
int minEval = std::numeric_limits<int>::max();
std::vector<u64> moveList = get_moves_list(std::get<OPPONENT>(board), std::get<AI>(board));
if (moveList.size() == 0) {
moveList.push_back(0UL);
}
std::tuple<u64, int> bestMove = std::make_tuple(moveList[0], 0);
for (int i = 0; i < moveList.size(); ++i) {
std::tuple<u64, u64> newBoard;
if (moveList[i] == 0UL) {
newBoard = std::make_tuple(std::get<AI>(board), std::get<OPPONENT>(board));
}else {
newBoard = play(moveList[i], std::get<OPPONENT>(board), std::get<AI>(board), false);
}
std::tuple<u64, int> tempMove = _minimax(newBoard, ply+1, -player, maxDepth);
if (std::get<VALUE>(tempMove) < minEval) {
minEval = std::get<VALUE>(tempMove);
std::get<VALUE>(bestMove) = std::get<VALUE>(tempMove);
std::get<MOVE>(bestMove) = moveList[i];
}
}
return bestMove;
}
}
u64 playAlphaBeta(std::tuple<u64, u64> board) {
int timeForMove = (int)(timeAllocation[moveNumber-1]*(double)timeRemaining);
// moveTime = timeForMove;
time_t abStart = time(NULL);
std::cout << "C ..............Time for move: " << timeForMove << std::endl;
int i = 2;
int alpha = std::numeric_limits<int>::min();
int beta = std::numeric_limits<int>::max();
u64 daBest = std::get<MOVE>(_alphaBeta(board, 0, 1, alpha, beta, i));
while ((time(NULL) - abStart) < timeForMove) {
i += 2;
daBest = std::get<MOVE>(_alphaBeta(board, 0, 1, alpha, beta, i));
}
return daBest;
}
std::tuple<u64, int> _alphaBeta(std::tuple<u64, u64> board, int ply, int player, int alpha, int beta, int maxDepth) {
if (ply >= maxDepth) {
return std::make_tuple<u64, int>(~0UL, evaluate(board));
}
if (player > 0) {
int maxEval = std::numeric_limits<int>::min();
std::vector<u64> moveList = get_moves_list(std::get<AI>(board), std::get<OPPONENT>(board));
if (moveList.size() == 0) {
moveList.push_back(0UL);
}
std::tuple<u64, int> bestMove = std::make_tuple(moveList[0], 0);
for (int i = 0; i < moveList.size(); ++i) {
std::tuple<u64, u64> newBoard;
if (moveList[i] == 0UL) {
newBoard = std::make_tuple(std::get<AI>(board), std::get<OPPONENT>(board));
}else {
newBoard = play(moveList[i], std::get<AI>(board), std::get<OPPONENT>(board), true);
}
std::tuple<u64, int> tempMove = _alphaBeta(newBoard, ply+1, -player, alpha, beta, maxDepth);
if (std::get<VALUE>(tempMove) > maxEval) {
maxEval = std::get<VALUE>(tempMove);
std::get<VALUE>(bestMove) = std::get<VALUE>(tempMove);
std::get<MOVE>(bestMove) = moveList[i];
alpha = std::max(alpha, maxEval);
}
if (maxEval >= beta) {
break;
}
}
return bestMove;
}else {
int minEval = std::numeric_limits<int>::max();
std::vector<u64> moveList = get_moves_list(std::get<OPPONENT>(board), std::get<AI>(board));
if (moveList.size() == 0) {
moveList.push_back(0UL);
}
std::tuple<u64, int> bestMove = std::make_tuple(moveList[0], 0);
for (int i = 0; i < moveList.size(); ++i) {
std::tuple<u64, u64> newBoard;
if (moveList[i] == 0UL) {
newBoard = std::make_tuple(std::get<AI>(board), std::get<OPPONENT>(board));
}else {
newBoard = play(moveList[i], std::get<OPPONENT>(board), std::get<AI>(board), false);
}
std::tuple<u64, int> tempMove = _alphaBeta(newBoard, ply+1, -player, alpha, beta, maxDepth);
if (std::get<VALUE>(tempMove) < minEval) {
minEval = std::get<VALUE>(tempMove);
std::get<VALUE>(bestMove) = std::get<VALUE>(tempMove);
std::get<MOVE>(bestMove) = moveList[i];
beta = std::min(beta, minEval);
}
if (minEval <= alpha) {
break;
}
}
return bestMove;
}
}
int evaluate(std::tuple<u64, u64> board) {
int countSelf = bitCount(std::get<AI>(board));
int countOpponent = bitCount(std::get<OPPONENT>(board));
int netPieces = countSelf - countOpponent;
if (is_end(std::get<AI>(board), std::get<OPPONENT>(board))) {
return netPieces * 100000;
}
return netPieces;
}
std::vector<u64> get_moves_list(u64 player, u64 opponent) {
u64 moves = get_legal_moves(player, opponent);
std::vector<u64> moveList;
u64 scanner = 1UL;
for (int i = 0; i < 64; ++i) {
if ((scanner & moves) != 0UL) {
// TODO start ordering moves here
moveList.push_back(scanner);
}
scanner <<= 1;
}
if (DEBUG) {
// print_uint(moves);
// std::cout << bitCount(moves) << std::endl;
// std::cout << moveList.size() << std::endl;
if (bitCount(moves) != moveList.size()) {
std::cout << "mismatch between moves and moveList sizes" << std::endl;
}
}
return moveList;
}