-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
351 lines (299 loc) · 9.09 KB
/
main.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
** main.c
** Started on Thu May 13 13:29:26 2010 najon
**
** Author(s):
** - jmassot <[email protected]>
*/
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <glib/ghash.h>
#include <glib/gi18n.h>
#include <glib/gtypes.h>
#include "libbox.h"
#define BUFFSIZE 255
#define MAX_POS 50000
typedef struct {
//store each crossing
box *crossing[MAX_POS];
int crossing_current;
//store the road made
box *history[MAX_POS];
int history_current;
//if rewind is active, this is the destination
box *rewind_destination;
box *current_box;
box *previous_box;
//store the board in a hashtable
GHashTable *ht;
} Map;
void win()
{
printf("\n");
printf("################################################################\n");
printf("BIM je mange ta maman et je saute en case terminale.\n");
printf("################################################################\n");
//TODO: faire le deplacement de la fin
//return 0;
exit(0);
}
void die(char *mess)
{
perror(mess);
exit(1);
}
/*
* set the buffer to be sent to the server according to the move computed by explore
*/
void avancer(Map *map, char *buffersend){
printf("moving from [%d, %d] to[%d, %d]\n",
map->previous_box->x, map->previous_box->y,
map->current_box->x, map->current_box->y);
//box_print(map->current_box);
if (map->previous_box->up == map->current_box)
strcpy(buffersend, "MOVE North");
else if (map->previous_box->down == map->current_box)
strcpy(buffersend,"MOVE South");
else if (map->previous_box->right == map->current_box)
strcpy(buffersend,"MOVE East");
else if (map->previous_box->left == map->current_box)
strcpy(buffersend,"MOVE West");
else
{
//print_map(ht, map->current_box);
printf("FAIL MON POTE, this destination is impossible to reach (%d, %d) -> (%d, %d)\n",
map->previous_box->x, map->previous_box->y, map->current_box->x, map->current_box->y);
}
}
int startswith(const char *buffer, const char *start)
{
int len = strlen(start);
int i;
for (i = 0; i < len; ++i)
if (buffer[i] != start[i])
return 0;
return 1;
}
void rb_store(Map *map)
{
map->history[map->history_current++] = map->current_box;
if (map->history_current > MAX_POS)
{
printf("map is too big\b");
exit(1);
}
}
/* return the next map to go, 0 if nothing to rewind */
box *rb_rewind(Map *map)
{
box *next;
//not rewinding
if (!map->rewind_destination)
return 0;
if (map->history_current <= 0)
{
printf("nothing to rewind!!!\n");
return 0;
}
next = map->history[--map->history_current];
//rewind finish, stop rewinding
if (next == map->rewind_destination)
{
map->rewind_destination = 0;
//remove the cross from the list of cross, will be readded latter if it's still interesting
map->crossing_current--;
}
return next;
}
void rb_setup_rewind(Map *map, box *dest)
{
map->rewind_destination = dest;
}
/*
* return the next box to explore
*/
box *explore(Map *map) {
int c = box_count_interesting_road(map->current_box);
box *next = 0;
//box_print(map->current_box);
//mark current box as visited
box_mark_visited(map->current_box);
next = rb_rewind(map);
if (next)
return next;
//store the current box (should be after rewind), we dont want to store your move when rewinding
rb_store(map);
if (box_is_exit(map->current_box))
{
win();
}
//more than one read => store the crossing for later
if (c >= 1) {
map->crossing[map->crossing_current++] = map->current_box;
if (map->crossing_current > MAX_POS){
printf("map too big\n");
exit(1);
}
}
next = box_get_interesting_neighboor(map->current_box);
//we are in a "cul de sac" (with french accent)
//rollback to the previous cross
if (!next)
{
if (map->crossing_current < 1)
{
printf("no more crossing: there is no solution\n");
exit(1);
}
printf("rewinding:\n");
rb_setup_rewind(map, map->crossing[map->crossing_current - 1]);
//cancel the current move
//rb_rewind();
map->history_current--;
return rb_rewind(map);
}
return next;
}
/* fgets for socket */
int sgets(char *buffer, int max, int fd)
{
int i = 0;
int bytes = 0;
char *p = buffer;
while (i < max)
{
bytes = recv(fd, p, 1, 0);
if (bytes < 1)
{
printf("Receive returned: %d\n", bytes);
die("Failed to receive bytes from server AHHH");
}
++i;
if (*p == '\n')
break;
p++;
}
//on est jamais trop sure
*p = 0;
buffer[max] = 0;
return i;
}
/*
* on n'a pas encore le protocole alors je simule la premiere lettre du msg
*v pour voir a pour
*/
void WhatweGonnaDo(Map *map, const char *bufferrecv, char *buffersend) {
buffersend[0] = 0;
if (startswith(bufferrecv, "North"))
{
if (box_is_unknown(map->current_box->up))
map->current_box->up = newbox(box_decode_state(bufferrecv[6]),map->current_box->x, map->current_box->y + 1, map->ht);
if (box_is_unknown(map->current_box->up->up))
map->current_box->up->up = newbox(box_decode_state(bufferrecv[8]),map->current_box->x, map->current_box->y + 2, map->ht);
}
else if (startswith(bufferrecv, "South"))
{
if (box_is_unknown(map->current_box->down))
map->current_box->down = newbox(box_decode_state(bufferrecv[6]),map->current_box->x, map->current_box->y - 1, map->ht);
if (box_is_unknown(map->current_box->down->down))
map->current_box->down->down = newbox(box_decode_state(bufferrecv[8]),map->current_box->x, map->current_box->y - 2, map->ht);
}
else if (startswith(bufferrecv, "West"))
{
if (box_is_unknown(map->current_box->left))
map->current_box->left = newbox(box_decode_state(bufferrecv[5]),map->current_box->x - 1, map->current_box->y, map->ht);
if (box_is_unknown(map->current_box->left->left))
map->current_box->left->left = newbox(box_decode_state(bufferrecv[7]),map->current_box->x - 2, map->current_box->y, map->ht);
}
else if (startswith(bufferrecv, "East"))
{
if (box_is_unknown(map->current_box->right))
map->current_box->right = newbox(box_decode_state(bufferrecv[5]),map->current_box->x + 1, map->current_box->y, map->ht);
if (box_is_unknown(map->current_box->right->right))
map->current_box->right->right = newbox(box_decode_state(bufferrecv[7]),map->current_box->x + 2, map->current_box->y, map->ht);
}
else if (startswith(bufferrecv, "FAILED"))
{
//print_map(map->ht, map->current_box);
printf("Fail sucker... invalid move\n");
exit(1);
}
else if (startswith(bufferrecv, "You win"))
{
printf("Command : %s\n", bufferrecv);
win();
}
//TODO: ack => the first time the server doest not send Play but Timeout (we start after each empty line as a hack)
//if (startswith(bufferrecv, "Play") || bufferrecv[0] == 0)
else if (bufferrecv[0] == 0)
{
//TODO : what we do whith this fucking buffer!
map->previous_box = map->current_box;
map->current_box = explore(map);
if (!map->current_box)
{
printf("error: no destination... trying again!!\n");
map->current_box = map->previous_box;
}
avancer(map, buffersend);
}
else
{
printf("Command : %s\n", bufferrecv);
}
}
int main(int argc, char *argv[]) {
int sock;
struct sockaddr_in echoserver;
char bufferrecv[256];
char buffersend[256];
Map mymap;
Map *map = &mymap;
if (argc != 4) {
fprintf(stderr, "USAGE: %s <server_ip> <port> <pseudo>\n", argv[0]);
exit(1);
}
memset(map, 0, sizeof(Map));
map->ht = g_hash_table_new(g_str_hash, g_str_equal);
map->current_box = newbox(ROUTE, 0, 0, map->ht);
/* the TCP socket */
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
die("Failed to create socket");
}
/* Construct the server sockaddr_in structure */
memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */
echoserver.sin_family = AF_INET; /* Internet/IP */
echoserver.sin_addr.s_addr = inet_addr(argv[1]); /* IP address */
echoserver.sin_port = htons(atoi(argv[2])); /* server port */
/* Establish connection */
if (connect(sock, (struct sockaddr *) &echoserver, sizeof(echoserver)) < 0)
die("Failed to connect with server");
//send name
send(sock, argv[3], strlen(argv[3]), 0);
//fuck header
if (!sgets(bufferrecv, 255, sock))
die("Failed to receive bytes from server");
//fuck header
if (!sgets(bufferrecv, 255, sock))
die("Failed to receive bytes from server");
while (1) {
if (!sgets(bufferrecv, 255, sock))
die("Failed to receive bytes from server bim");
WhatweGonnaDo(map, bufferrecv, buffersend);
if (buffersend[0])
{
printf("Sending: %s\n", buffersend);
send(sock, buffersend, strlen(buffersend), 0);
//easyier debug
printf("history: %4d, crossing: %4d\n", map->history_current, map->crossing_current);
print_map(map->ht, map->previous_box);
//sleep(1);
}
}
printf("toto");
}