-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
165 lines (130 loc) · 4.3 KB
/
main.cpp
File metadata and controls
165 lines (130 loc) · 4.3 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
#include "main.h"
#include <sstream>
#include <string>
//true after the board was set up
bool isInitialized = false;
//true when we have sent a move to the server and the server hasn't answered
bool sentMove = false;
/**
* Called when a new package is recieved
* @param msg
*/
void onPacket(char* msg){
//handle the package
Fisher::Catch(msg);
}
/**
* Called on every update
*/
void onTick(){
}
/**
* Called when the server sends a new board
*
* @param ones
* @param twos
* @param threes
*/
void onFieldRec(u_int64_t ones, u_int64_t twos, u_int64_t threes){
//only change the board the first time.
//We are not interessted in new boards because the places of the floes stay
//the same during the whole game. We keep track of the used floes our self.
if(isInitialized) return;
Globals::ones = ones;
Globals::twos = twos;
Globals::threes = threes;
Globals::_board = Board();
isInitialized = true;
}
/**
* Called when it is our turn to calculate a move and send it to the server.
*/
void onMoveReq(){
//fetch the time
clock_gettime(Globals::clockTime, &Globals::moveReqTime);
//create a pointer to the result move
Move move;
//there is a bug when the game ends no move is send
//set up the right evaluation methods
Evaluation::preEvaluate(Globals::_board.movecount);
//start the search
iterativeDeepening(Globals::_board, ID_WE, 60, 0, &move);
//check if the move is valid otherwise select a random "ok" move
if(!BoardTools::isValidMove(Globals::_board, move, ID_WE))
move = BoardTools::generateGoodMove(Globals::_board, ID_WE);
//send the move to the server
Ocean::Send(move);
//apply the move locally to keep track of the changes since the beginning of
//the game
BoardTools::apply(&Globals::_board, ID_WE, move);
//we have sent the last move
sentMove = true;
//fetch the time when the others beginn calculating
clock_gettime(Globals::clockTime, &Globals::beginningOther);
}
/**
* Called when the server sends the latest move
*
* @param move
*/
void onLastMove(Move move){
if(sentMove){//TODO move apply from onMoveReq to this method because no calculation is done in the free time
//we sent the last move. It is already apllied to the board in onMoveReq
sentMove = false;
return;
}
//apply the received move
BoardTools::apply(&Globals::_board, ID_OPPONENT, move);
//fetch the time to check how much time the opponent needed
struct timespec nowTime;
clock_gettime(Globals::clockTime, &nowTime);
}
int main(int argc, char** argv){
//print some very important information
theBreadfish();
usleep(100);
//set up callbacks
Fisher::SetDigestion(&onFieldRec, &onLastMove, &onMoveReq);
//read connection data
char* host = "127.0.0.1";
char* port = "13050";
std::string send("<protocol><joinPrepared reservationCode=\"");
bool reservated = false;
for(int i = 0; i < argc; i++){
if(strcmp(argv[i], "--host") == 0)host = argv[++i];
if(strcmp(argv[i], "--port") == 0)port = argv[++i];
if(strcmp(argv[i], "--reservation") == 0){
send.append(argv[++i]);
reservated = true;
}
if(strcmp(argv[i], "-h") == 0)host = argv[++i];
if(strcmp(argv[i], "-p") == 0)port = argv[++i];
if(strcmp(argv[i], "-r") == 0){
send.append(argv[++i]);
reservated = true;
}
}
//connect to server
Ocean::SwimTo(host, port, &onPacket, &onTick);
if(!reservated)
Ocean::Send("<protocol><join gameType=\"swc_2015_hey_danke_fuer_den_fisch\"/>");
else{
send.append("\"/>");
Ocean::Send(send);
}
//start the game
Globals::_runningGame = true;
//create an empty board
Globals::_board = Board();
//update state and calculate new moves while game is running
while(Globals::_runningGame && (Globals::_board.movecount <= 60))
//fetch the next package from the server and start calculating moves
Ocean::GetFood();
sleep(3);
//fetch last package
Ocean::GetFood();
sleep(1);
//close connection
Ocean::Close();
return 0;
}