-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_player.cpp
123 lines (94 loc) · 1.96 KB
/
remote_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
#include"remote_player.h"
#include"stratego.h"
#include"piece.h"
#include<sstream>
remote_player::remote_player(int team, char * file, image *h, image **r,
TCPsocket s)
: ai(team,file,h,r)
{
_sock = s;
}
void remote_player::set_up(board * b, stratego * g)
{
int res, i, x, y, id;
piece * p;
char mess[1024];
char action[1024];
for (i = 0; i < 40; i++)
_pieces[i]->reset();
while (1)
{// start while(1)
res = SDLNet_TCP_Recv(_sock,mess,1024);
if (res <= 0)
{
cerr<<"Client disconnected"<<endl;
exit(1);
}
istringstream in(mess);
in>>action;
if (strcmp(action, "motion") == 0)
continue;
if (strcmp(action, "done") == 0)
break;
in>>id;
in>>x;
in>>y;
p = _pieces[id];
b->set_piece(x,y,p);
}// end while(1)
g->draw();
g->update();
}
int remote_player::move(board *b, stratego *g)
{
int res, oldx, oldy, x, y;
piece * p;
char mess[1024];
char action[1024];
while (1)
{// start while(1)
g->draw();
res = SDLNet_TCP_Recv(_sock, mess, 1024);
if (res <= 0)
{
cerr<<"disconnect"<<endl;
exit(1);
}
istringstream in(mess);
in>>action;
if (strcmp(action,"pickup") == 0)
{
in>>oldx;
in>>oldy;
p = b->get_piece(oldx,oldy);
b->set_piece(oldx,oldy,0);
}
else if (strcmp(action,"putdown") == 0)
{
in>>oldx;
in>>oldy;
b->set_piece(oldx,oldy,p);
p = 0;
g->draw();
}
else if (strcmp(action,"motion") == 0)
{
in>>x;
in>>y;
cout<<"x: "<<x<<" y: "<<y<<endl;
if (p && p->get_is_alive())
p->draw(g->get_screen(),x,y,1);
}
else if (strcmp(action,"move") == 0)
{
in>>oldx;
in>>oldy;
in>>x;
in>>y;
b->set_piece(oldx,oldy,p);
p = 0;
return b->move(oldx,oldy,x,y);
}
g->update();
}// end while(1)
}