-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
229 lines (203 loc) · 5.51 KB
/
client.c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*********************************************************
* client.c
* Nine-Board Tic-Tac-Toe Client
* COMP3411/9414/9814 Artificial Intelligence
* Alan Blair, CSE, UNSW
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#include "common.h"
#include "agent.h"
int port=31415;
char *local="localhost";
char *host;
int pipe_fd;
FILE* pipe_in_stream;
FILE* pipe_out_stream;
char client_buf[256];
/*********************************************************//*
Close the network connection
*/
void client_cleanup()
{
agent_cleanup();
fclose(pipe_in_stream);
fclose(pipe_out_stream);
close(pipe_fd);
exit(0);// must exit immediately, to avoid "zombie" process
}
/*********************************************************//*
Read from pipe input stream to buffer
*/
void pipe_read(char *buf)
{
fflush(pipe_in_stream);
buf[0] = '\0';
fscanf(pipe_in_stream, "%s", buf);
if( buf[0] == '\0' ) {
client_cleanup();
}
}
/*********************************************************//*
Get second move from agent and write it to output stream
*/
void client_second_move( int board_num, int prev_move )
{
int this_move;
this_move = agent_second_move( board_num,prev_move );
fprintf(pipe_out_stream, "%d\n",this_move);
fflush(pipe_out_stream);
}
/*********************************************************//*
Get third move from agent and write it to output stream
*/
void client_third_move(
int board_num,
int first_move,
int prev_move
)
{
int this_move;
this_move=agent_third_move(board_num,first_move,prev_move);
fprintf(pipe_out_stream, "%d\n",this_move);
fflush(pipe_out_stream);
}
/*********************************************************//*
Get next move from agent and write it to output stream
*/
void client_next_move( int prev_move )
{
int this_move;
this_move = agent_next_move( prev_move );
fprintf(pipe_out_stream,"%d\n",this_move);
fflush(pipe_out_stream);
}
/*********************************************************//*
Set up tcp connection
*/
int tcpopen()
{
int sd, rc;
struct hostent *h;
struct sockaddr_in servAddr; // localAddr
h = (struct hostent *)gethostbyname(host);
if(h==NULL) {
printf("unknown host '%s'\n",host);
exit(1);
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *)&servAddr.sin_addr.s_addr,
h->h_addr_list[0],h->h_length );
servAddr.sin_port = htons(port);
// create socket
sd = socket(AF_INET, SOCK_STREAM, 0);
if(sd<0) {
perror("cannot open socket ");
exit(1);
}
int tcp_no_delay = 1;
if(setsockopt(sd, IPPROTO_TCP, TCP_NODELAY,
(char *)&tcp_no_delay, sizeof(tcp_no_delay)) < 0) {
perror ("tcpecho: TCP_NODELAY options");
exit(1);
}
// connect to server
rc=connect(sd,(struct sockaddr *)&servAddr,sizeof(servAddr));
if(rc<0) {
perror("cannot connect ");
exit(1);
}
return sd;
}
/*********************************************************//*
Determine cause for win, loss or draw
*/
int get_cause( char *buf )
{
int cause=TRIPLE;
if( strcmp(client_buf,"triple).") == 0) {
cause = TRIPLE;
}
else if( strcmp(client_buf,"timeout).") == 0) {
cause = TIMEOUT;
}
else if( strcmp(client_buf,"illegal_move).") == 0) {
cause = ILLEGAL_MOVE;
}
else if( strcmp(client_buf,"full_board).") == 0) {
cause = FULL_BOARD;
}
return( cause );
}
/*********************************************************/
int main(int argc, char** argv)
{
int player=0;
int result=DRAW; // WIN, LOSS or DRAW
int cause =TRIPLE;// TRIPLE, TIMEOUT, ILLEGAL_MOVE or FULL_BOARD
int board_num,first_move,prev_move;
int sd;
char ch;
host = local; // default
agent_parse_args( argc, argv );
sd = tcpopen(); // host,port );
pipe_fd = sd;
pipe_in_stream = fdopen(sd,"r");
pipe_out_stream = fdopen(sd,"w");
client_buf[0] = '\0';
pipe_read(client_buf);
while( TRUE ) {
if(strcmp(client_buf,"init.") == 0) {
agent_init();
}
else if(sscanf(client_buf,"start(%c).",&ch) == 1) {
player = ( ch == 'x' ) ? 0 : 1 ;
agent_start( player );
}
else if(sscanf(client_buf,"second_move(%d,%d).",
&board_num,&prev_move) == 2 ) {
client_second_move( board_num,prev_move );
}
else if(sscanf(client_buf,"third_move(%d,%d,%d).",
&board_num,&first_move,&prev_move) == 3) {
client_third_move( board_num,first_move,prev_move );
}
else if(sscanf(client_buf,"next_move(%d).",&prev_move)==1){
client_next_move( prev_move );
}
else if(sscanf(client_buf,"last_move(%d).",&prev_move)==1){
agent_last_move( prev_move );
}
else if( strcmp(client_buf,"win(") > 0
&& strcmp(client_buf,"win)") < 0 ) {
result = WIN;
cause = get_cause(client_buf+4);
agent_gameover(result,cause);
}
else if( strcmp(client_buf,"loss(") > 0
&& strcmp(client_buf,"loss)") < 0 ) {
result = LOSS;
cause = get_cause(client_buf+5);
agent_gameover(result,cause);
}
else if( strcmp(client_buf,"draw(") > 0
&& strcmp(client_buf,"draw)") < 0 ) {
result = DRAW;
cause = get_cause(client_buf+5);
agent_gameover(result,cause);
}
else if(strcmp(client_buf,"end") == 0) {
client_cleanup();
return 0;
}
client_buf[0] = '\0';
pipe_read(client_buf);
}
return 0;
}