-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
174 lines (151 loc) · 5.06 KB
/
player.cpp
File metadata and controls
174 lines (151 loc) · 5.06 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
#include "player.hpp"
/*
* Constructor for the player; initialize everything here. The side your AI is
* on (BLACK or WHITE) is passed in as "side". The constructor must finish
* within 30 seconds.
*/
Player::Player(bool side, char* weightName) {
othelloBoard = new Board();
ourSide = side;
otherSide = !side;
movesPlayed = 0;
endGameHead = nullptr;
ifstream ifile(weightName);
if(!ifile.is_open()) {
cerr << "Error opening file: " << weightName << endl;
exit(1);
}
string heuristicType;
ifile >> heuristicType;
ifile.close();
if (heuristicType.compare("linear") == 0) {
mainHeuristic = new LinearHeuristic(weightName);
}
else if (heuristicType.compare("time") == 0) {
mainHeuristic = new TimeHeuristic(weightName);
}
else {
cerr << "Heuristic type \"" << heuristicType << "\" not supported." << endl;
exit(1);
}
endgameHeuristic = new LinearHeuristic("weights/endgame.weights");
transTable = new TransTableEntry[NUM_T_TABLE_ENTRIES]();
}
/*
* Destructor for the player.
*/
Player::~Player() {
delete othelloBoard;
delete mainHeuristic;
delete endgameHeuristic;
if (transTable) delete transTable;
}
/**
* Sets the player's board to a given board. Used for testing
* @param b Board to set the board to
*/
void Player::setBoard(Board* b){
othelloBoard = b;
}
/*
* Compute the next move given the opponent's last move. Your AI is
* expected to keep track of the board on its own. If this is the first move,
* or if the opponent passed on the last move, then opponentsMove will be
* nullptr.
*
* msLeft represents the time your AI has left for the total game, in
* milliseconds. doMove() must take no longer than msLeft, or your AI will
* be disqualified! An msLeft value of -1 indicates no time limit.
*
* The move returned must be legal; if there are no valid moves for your side,
* return nullptr.
*/
Move Player::doMove(Move opponentsMove, int msLeft) {
if(!opponentsMove.isNull()){
movesPlayed ++;
}
othelloBoard->doMove(opponentsMove);
Move moveToMake = NULL_MOVE(ourSide);
if(movesPlayed < 40){
moveToMake = minimax(mainHeuristic, 10, msLeft);
}
else if(movesPlayed >= 60){
moveToMake = NULL_MOVE(ourSide);
}
else{
moveToMake = endGameSolve(opponentsMove, msLeft);
}
othelloBoard->doMove(moveToMake);
cerr << "sudormrf-" << (ourSide==BLACK ? "Black" : "White") << ": ";
if(moveToMake.isNull()){
cerr << "pass" << endl;
}
else{
cerr << moveToMake.getX() << " " << moveToMake.getY() << endl;
}
if(!moveToMake.isNull()){
movesPlayed ++;
}
return moveToMake;
}
/**
* Determines the move to make during the early/mid game using minimax
* @param heuristic Heuristic function to use for this search
* @param depth Depth to search into minimax tree
* @param msLeft Time remaining to make moves
* @return Move to make
*/
Move Player::minimax(Heuristic* heuristic, int depth, int msLeft){
BoardNode* root = new BoardNode(othelloBoard, ourSide);
Move test = root->getBestChoice(depth, heuristic, transTable);
delete root;
return test;
}
/**
* Determines the move to make during endgame using an endgame solver
* @param opponentsMove Move that the opponent made last round
* @param msLeft Time remaining to make moves
* @return Move to make
*/
Move Player::endGameSolve(Move opponentsMove, int msLeft){
if(endGameHead == nullptr){
delete[] transTable;
transTable = nullptr;
endGameHead = new BoardNode(othelloBoard, ourSide);
int score = endGameHead->searchTreeEndGame(endgameHeuristic, ourSide);
if(score < 1){
cerr << "sudormrf-" << (ourSide==BLACK ? "Black" : "White") << ": "
<< "No solution found" << endl;
delete endGameHead;
endGameHead = nullptr;
transTable = new TransTableEntry[NUM_T_TABLE_ENTRIES]();
return minimax(mainHeuristic, 10, msLeft);
}
cerr << "sudormrf-" << (ourSide==BLACK ? "Black" : "White") << ": "
<< "CHOKEHOLD SOLUTION FOUND" << endl;
endGameTracker = endGameHead->getChildren()[0];
return endGameTracker->getMove();
}
else{
vector<BoardNode*> nodes = endGameTracker->getChildren();
if(opponentsMove.isNull()){
endGameTracker = endGameTracker->getChildren()[0];
}
else{
for(int i = 0; i < (int)nodes.size(); i++){
Move temp = nodes[i]->getMove();
if(temp.getX() == opponentsMove.getX() && temp.getY() == opponentsMove.getY()){
endGameTracker = endGameTracker->getChildren()[i];
break;
}
}
}
if(endGameTracker->getChildren().size() == 0){
return NULL_MOVE(ourSide);
}
else{
endGameTracker = endGameTracker->getChildren()[0];
return endGameTracker->getMove();
}
}
}