-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
124 lines (106 loc) · 3.42 KB
/
player.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
#include "player.hpp"
//Alycia's comment
//LC's contribution
/*
* 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(Side side) {
// Will be set to true in test_minimax.cpp.
testingMinimax = false;
board = new Board();
color = side;
/*
* TODO: Do any initialization you need to do here (setting up the board,
* precalculating things, etc.) However, remember that you will only have
* 30 seconds.
*/
}
/*
* Destructor for the player.
*/
Player::~Player() {
}
/*
* 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) {
/*
* TODO: Implement how moves your AI should play here. You should first
* process the opponent's opponents move before calculating your own move
*/
Side opponent;
if(color == WHITE)
{
std::cerr << "WHITE" << std::endl;
opponent = BLACK;
}
else
{
std::cerr << "BLACK" << std::endl;
opponent = WHITE;
}
if(opponentsMove != nullptr){
board->doMove(opponentsMove, opponent);
}
int time = msLeft;
if(msLeft == -1){
time = 500000000;
}
Move* theMove = nullptr;
while(time > 0){
if(board->hasMoves(color))
{
int max_score = -30000;
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
Move *move = new Move(i, j);
if(board->checkMove(move, color))
{
// std::cerr << "COORDINATES" << std::endl;
// std::cerr << move->getX() << std::endl;
// std::cerr << move->getY() << std::endl;
int score = board->findWeight(move);
if(score > max_score) {
max_score = score;
theMove = move;
//std::cerr << "check " << board->checkMove
//(theMove, color) << std::endl;
}
}
//delete(move);
}
}
board->doMove(theMove, color);
//std::cerr << theMove->getX() << std::endl;
//std::cerr << theMove->getY() << std::endl;
return theMove;
}
}
// while(time > 0)
// {
// x = rand() % 8;
// y = rand() % 8;
// if(board->hasMoves(color))
// {
// Move *move = new Move(x, y);
// if(board->checkMove(move, color))
// {
// board->doMove(move, color);
// return move;
// }
// }
// }
return nullptr;
}