]> git.cworth.org Git - rrsolve/blob - src/rrsolve.c
Permit rr_client_next_notice to return null notice
[rrsolve] / src / rrsolve.c
1 /* rrsolve - Simple RR solver and librr client.
2  *
3  * Copyright © 2003 Carl Worth
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of Carl Worth
10  * not be used in advertising or publicity pertaining to distribution
11  * of the software without specific, written prior permission.
12  * Carl Worth makes no representations about the suitability of this
13  * software for any purpose.  It is provided "as is" without express
14  * or implied warranty.
15  * 
16  * CARL WORTH DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
18  * NO EVENT SHALL CARL WORTH BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
20  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
21  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Carl Worth <carl@theworths.org>
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/time.h>
31 #include <time.h>
32
33 #include "rrsolve.h"
34
35 #define HOST "localhost"
36 #define PORT "5252"
37 #define USER "rrsolve"
38 #define GAME "game"
39
40 /* Tuning this can reduce excess reallocs */
41 #define RRS_BRANCHING_FACTOR_ESTIMATE 10
42
43 #define RRS_STATE_SET_ROBOT(s, ri, x, y) ((s) |= (((y) << ((ri)<<3)) | ((x) << (((ri)<<3) + 4))))
44 #define RRS_STATE_GET_ROBOT(s, ri, x, y) { (y) = ((s) >> ((ri)<<3)) & 0xf; (x) = ((s) >> (((ri)<<3) + 4)) & 0xf; }
45
46 static void
47 handle_events (rr_client_t *client);
48
49 static rrs_state_t
50 rrs_state_get_from_board (rr_board_t *board);
51
52 static void
53 rrs_solution_print (rrs_solution_t *solution);
54
55 static rr_status_t
56 solve_board (rr_board_t *board, rrs_solution_t *solution);
57
58 static rr_status_t
59 find_solution_states (rr_board_t *board,
60                       rrs_state_buf_t ***solution_states,
61                       int *num_state_buf,
62                       rrs_state_t *final_state);
63
64 static int
65 rrs_find_new_states (rr_board_t *board,
66                      rrs_state_buf_t **previous,
67                      int moves,
68                      rrs_state_buf_t *new,
69                      rrs_state_t *solution_state);
70
71 static void
72 trace_solution (rr_board_t *board,
73                 rrs_state_buf_t **states, int moves,
74                 rrs_state_t solution_state,
75                 rrs_solution_t *solution);
76
77 char TOUGH[] = "\n"
78 " === === === === === === === === === === === === === === === === \n"
79 "|... ... ... ...|r.. ... ... ... ... ... ... ...|... ... ... ...|\n"
80 "                                                     ===         \n"
81 "|... ... ... ... ... ... ... ... ... ... ... ... ...|.rs Y.. ...|\n"
82 "                                                                 \n"
83 "|... ... ... ... ... .bo|... ... ... .bt|... ... ... ... ... ...|\n"
84 "                     ===             ===                         \n"
85 "|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n"
86 "                                                             === \n"
87 "|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n"
88 "         ===                                                     \n"
89 "|... ... .gc|... ... ... ...|.rt ... ... ... ... ... ...|.go ...|\n"
90 " ===                         ===             ===         ===     \n"
91 "|... ... ... ... ... ... ... ... ... ... ... .yc|... ... ... ...|\n"
92 "     ===                     === ===                             \n"
93 "|...|.YS ... ... ... ... ...|... ...|... ... ... ... ... ... ...|\n"
94 "                                                 ===             \n"
95 "|... ... ... ... ... ... ...|... ...|... ... ...|.ww ... ... ...|\n"
96 "     ===             ===     === ===                             \n"
97 "|... .yo|... ... ...|bbs ... ... ... ...|.bc ... ... ... ... ...|\n"
98 "                                         ===                     \n"
99 "|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n"
100 " ===                                                         === \n"
101 "|... ... ... ... ... ... ... ... ... .yt|... ... ... ... ... ...|\n"
102 "                                     ===                 ===     \n"
103 "|... ... ... ... ... ... .rc|... ... ... ... ... ... ... .gs|...|\n"
104 "                         ===                                     \n"
105 "|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... g..|\n"
106 "                                                     ===         \n"
107 "|... ...|.gt ... ... ... ... ... ... ... ... ... ...|.ro ... ...|\n"
108 "         ===                                                     \n"
109 "|... ... ... ... ... ...|... ... ... ... ...|... ... ... ... ...|\n"
110 " === === === === === === === === === === === === === === === === ";
111 /*
112 Move #1 generated 11 new states.
113 Move #2 generated 59 new states.
114 Move #3 generated 216 new states.
115 Move #4 generated 640 new states.
116 Move #5 generated 1701 new states.
117 Move #6 generated 4239 new states.
118 Move #7 generated 10041 new states.
119 Move #8 generated 22678 new states.
120 Move #9 generated 49103 new states.
121 Move #10 generated 102154 new states.
122 Move #11 generated 204086 new states.
123 Move #12 generated 391534 new states.
124 Move #13 generated 722808 new states.
125 Move #14 generated 1285932 new states.
126 Move #15 generated 2204971 new states.
127 Found solution of 16 moves in 3694.8 seconds.
128 Traced solution in 0.052438 seconds.
129 Solution (16 moves):
130     Move #0: yellow east, south, west
131 Move #3: green south, west, north
132 Move #6: blue east, north, west, south
133 Move #10: yellow south, east, south
134 Move #13: green west
135 Move #14: yellow north
136 */
137
138 int
139 main (int argc, char *argv[])
140 {
141     args_t args;
142     rr_status_t status;
143     rr_client_t *client;
144
145     args_parse (&args, argc, argv);
146
147     client = rr_client_create (args.host, args.port, args.user);
148     if (client == NULL) {
149         fprintf (stderr, "Failed connecting to %s:%s as %s\n",
150                  args.host, args.port, args.user);
151         return 1;
152     }
153
154     status = rr_client_join (client, GAME);
155     if (status == RR_STATUS_NO_GAME)
156         status = rr_client_new (client, GAME);
157     if (status) {
158         fprintf (stderr, "Error joining or creating game: %s\n", rr_status_str (status));
159         return 1;
160     }
161
162     handle_events (client);
163
164     rr_client_destroy (client);
165
166     return 0;
167 }
168
169 static void
170 handle_events (rr_client_t *client)
171 {
172     int i;
173     rr_status_t status;
174     rr_notice_t *notice;
175     rrs_solution_t solution;
176     rr_board_t *board = NULL;
177     char *diagram;
178     struct timespec move_delay = { 1, 200000000l };
179
180     while (1) {
181         status = rr_client_next_notice (client, &notice);
182         if (status) {
183             fprintf (stderr, "ERROR during rr_client_next_notice: %s\n",
184                      rr_status_str (status));
185             return;
186         }
187         if (!notice) {
188             fprintf (stderr, "No notice during rr_client_next_notice\n");
189             continue;
190         }
191
192         switch  (notice->type) {
193         case RR_NOTICE_GAMEOVER:
194         case RR_NOTICE_JOIN:
195             status = rr_client_show (client, &diagram);
196             if (status) {
197                 fprintf (stderr, "Error in rr_client_show: %s\n", rr_status_str (status));
198                 goto DONE;
199             }
200             if (board == NULL)
201                 board = rr_board_create_from_str (diagram);
202             else
203                 rr_board_parse (board, diagram);
204
205             /* XXX: Fixing the server to send a NOTICE TURN here would let me drop this code */
206             rrs_solution_init (&solution);
207             solve_board (board, &solution);
208             rr_client_bid (client, solution.num_moves);
209             break;
210         case RR_NOTICE_TURN:
211             rr_board_set_goal_target (board, notice->u.target);
212             rrs_solution_init (&solution);
213             solve_board (board, &solution);
214             rr_client_bid (client, solution.num_moves);
215             break;
216         case RR_NOTICE_ACTIVATE:
217             for (i = 0; i < solution.num_moves; i++) {
218                 status = rr_client_move (client,
219                                          solution.move[i].robot,
220                                          solution.move[i].dir);
221                 if (status) {
222                     rr_client_message (client, "Drat, looks like I was wrong.");
223                     rr_client_pass (client);
224                     break;
225                 }
226                 nanosleep (&move_delay, NULL);
227             }
228             rrs_solution_fini (&solution);
229             break;
230         case RR_NOTICE_ABANDON:
231             rr_client_abandon (client);
232             break;
233         case RR_NOTICE_NOBID:
234             rr_client_nobid (client);
235             break;
236         case RR_NOTICE_POSITION:
237             rr_board_position_robot (board,
238                                      notice->u.position.robot,
239                                      notice->u.position.x,
240                                      notice->u.position.y);
241             break;
242         case RR_NOTICE_GAMESTATE:
243             if (notice->u.gamestate == RR_GAMESTATE_SHOW) {
244                 if (solution.num_moves) {
245                     printf ("My solution (%d moves):", solution.num_moves);
246                     rrs_solution_print (&solution);
247                 }
248             }
249             break;
250         case RR_NOTICE_GAME:
251         case RR_NOTICE_USER:
252         case RR_NOTICE_QUIT:
253         case RR_NOTICE_DISPOSE:
254         case RR_NOTICE_MESSAGE:
255         case RR_NOTICE_WATCH:
256         case RR_NOTICE_PART:
257         case RR_NOTICE_BID:
258         case RR_NOTICE_REVOKE:
259         case RR_NOTICE_TIMER:
260         case RR_NOTICE_ACTIVE:
261         case RR_NOTICE_MOVE:
262         case RR_NOTICE_UNDO:
263         case RR_NOTICE_RESET:
264         case RR_NOTICE_SCORE:
265             /* Ignore these notices */
266             break;
267         }
268     }
269
270   DONE:
271     if (board)
272         rr_board_destroy (board);
273 }
274
275 static void
276 rrs_solution_print (rrs_solution_t *solution)
277 {
278     int i;
279     rr_robot_t last_robot;
280     rrs_move_t *move;
281
282     last_robot = RR_ROBOT_NONE;
283     for (i=0; i < solution->num_moves; i++) {
284         move = &solution->move[i];
285         if (move->robot == last_robot)
286             printf (", %s", rr_direction_str (move->dir));
287         else
288             printf ("\n Move #%d: %s %s",
289                     i, rr_robot_str (move->robot), rr_direction_str (move->dir));
290         last_robot = move->robot;
291     }
292     printf ("\n");
293 }
294
295 static rrs_state_t
296 rrs_state_get_from_board (rr_board_t *board)
297 {
298     int i;
299     int x, y;
300     rrs_state_t state;
301
302     state = 0;
303
304     for (i=0; i < RR_NUM_ROBOTS; i++) {
305         rr_board_find_robot (board, rr_robot_from_idx (i), &x, &y);
306         RRS_STATE_SET_ROBOT (state, i, x, y);
307     }
308
309     return state;
310 }
311
312 static void
313 rrs_state_set_board (rrs_state_t state, rr_board_t *board)
314 {
315     int i;
316     int x, y;
317
318     for (i=0; i < RR_NUM_ROBOTS; i++) {
319         RRS_STATE_GET_ROBOT (state, i, x, y);
320         rr_board_position_robot (board, rr_robot_from_idx (i), x, y);
321     }
322
323 }
324
325 static rr_status_t
326 solve_board (rr_board_t *board, rrs_solution_t *solution)
327 {
328     rr_status_t status;
329     int i, moves;
330     rrs_state_t solution_state;
331     rrs_state_buf_t **states;
332     struct timeval tv_start, tv_end;
333
334     if (rr_board_solved (board)) {
335         printf ("Board is solved to begin with.\n");
336         return RR_STATUS_SUCCESS;
337     }
338
339     printf ("Now trying to solve:");
340     puts (rr_board_to_str (board));
341     
342     gettimeofday (&tv_start, NULL);
343
344     status = find_solution_states (board, &states, &moves, &solution_state);
345     if (status)
346         return status;
347
348     gettimeofday (&tv_end, NULL);
349
350     if (moves < 0) {
351         printf ("It seems impossible.\n");
352     } else {
353         printf ("Found solution of %d moves in %g seconds.\n",
354                 moves,
355                 tv_end.tv_sec - tv_start.tv_sec
356                 + (tv_end.tv_usec - tv_start.tv_usec) / 1e6);
357
358         gettimeofday (&tv_start, NULL);
359
360         trace_solution (board, states, moves, solution_state, solution);
361
362         gettimeofday (&tv_end, NULL);
363
364         printf ("Traced solution in %g seconds.\n",
365                 tv_end.tv_sec - tv_start.tv_sec
366                 + (tv_end.tv_usec - tv_start.tv_usec) / 1e6);
367     }
368
369     for (i=0; i <= moves; i++) {
370         rrs_state_buf_destroy (states[i]);
371         states[i] = NULL;
372     }
373
374     free (states);
375
376     return RR_STATUS_SUCCESS;
377 }
378
379 static int
380 rrs_state_connected (rrs_state_t a, rrs_state_t b,
381                      rr_robot_t *robot, rr_direction_t *dir)
382 {
383     int i;
384     int nib;
385     rrs_state_t diff, mask;
386     int shift, delta;
387
388     diff = a ^ b;
389
390     nib = -1;
391     for (i=0; i < 8; i++) {
392         if (diff & 0xf) {
393             if (nib > 0)
394                 return 0;
395             else
396                 nib = i;
397         }
398         diff >>= 4;
399     }
400
401     switch (nib) {
402     case 0:
403     case 1:
404         *robot = RR_ROBOT_BLUE;
405         break;
406     case 2:
407     case 3:
408         *robot = RR_ROBOT_GREEN;
409         break;
410     case 4:
411     case 5:
412         *robot = RR_ROBOT_RED;
413         break;
414     case 6:
415     case 7:
416         *robot = RR_ROBOT_YELLOW;
417         break;
418     }
419
420     shift = 4 * nib;
421     mask = 0xf << shift;
422     delta = ((b & mask) >> shift) - ((a & mask) >> shift);
423     
424     if (nib % 2)
425         if (delta < 0)
426             *dir = RR_DIRECTION_WEST;
427         else
428             *dir = RR_DIRECTION_EAST;
429     else
430         if (delta < 0)
431             *dir = RR_DIRECTION_NORTH;
432         else
433             *dir = RR_DIRECTION_SOUTH;
434
435     return 1;
436 }
437
438 static void
439 trace_solution (rr_board_t *board,
440                 rrs_state_buf_t **states, int moves,
441                 rrs_state_t solution_state,
442                 rrs_solution_t *solution)
443 {
444     rr_status_t status;
445     int i, j;
446     rrs_state_buf_t *buf;
447     rr_robot_t robot;
448     rr_direction_t dir;
449
450     for (i = moves-1; i >= 0; i--) {
451         buf = states[i];
452         for (j = 0; j < buf->num_states; j++) {
453             if (rrs_state_connected (buf->state[j], solution_state,
454                                      &robot, &dir)) {
455                 rrs_state_set_board (buf->state[j], board);
456                 status = rr_board_move (board, robot, dir);
457                 if (status == RR_STATUS_SUCCESS) {
458                     if (rrs_state_get_from_board (board) == solution_state) {
459                         rrs_solution_prepend (solution, robot, dir);
460                         solution_state = buf->state[j];
461                         goto NEXT_MOVE;
462                     }
463                 }
464             }
465         }
466         fprintf (stderr, "ERROR: Failed to trace solution backwards to 0x%x at move %d\n",
467                  solution_state, i+1);
468         break;
469       NEXT_MOVE:
470         ;
471     }
472 }
473
474 static rr_status_t
475 find_solution_states (rr_board_t *board,
476                       rrs_state_buf_t ***solution_states,
477                       int *num_state_buf,
478                       rrs_state_t *final_state)
479 {
480     int solved, moves;
481     rrs_state_t initial;
482     rrs_state_buf_t **states, **new_states;
483
484     states = malloc (sizeof (rrs_state_buf_t *));
485     if (states == NULL)
486         return RR_STATUS_NO_MEMORY;
487
488     initial = rrs_state_get_from_board (board);
489
490     states[0] = rrs_state_buf_create (1);
491     if (states[0] == NULL)
492         return RR_STATUS_NO_MEMORY;
493     rrs_state_buf_add (states[0], initial);
494
495     moves = 0;
496     while (1) {
497         moves++;
498
499         new_states = realloc (states, (moves + 1) * sizeof (rrs_state_buf_t *));
500         if (new_states == NULL)
501             return RR_STATUS_NO_MEMORY;
502
503         states = new_states;
504         states[moves] = rrs_state_buf_create (RRS_BRANCHING_FACTOR_ESTIMATE
505                                               * states[moves-1]->num_states);
506         if (states[moves] == NULL)
507             return RR_STATUS_NO_MEMORY;
508
509         solved = rrs_find_new_states (board, states, moves-1, states[moves], final_state);
510
511         if (solved)
512             break;
513
514         if (states[moves]->num_states == 0) {
515             printf ("Can't make any further progress after %d moves.\n", moves);
516             moves = -1;
517             break;
518         } else {
519             printf ("Move #%d generated %d new states.\n",
520                     moves, states[moves]->num_states);
521         }
522     }
523
524     *num_state_buf = moves;
525     *solution_states = states;
526
527     return RR_STATUS_SUCCESS;
528 }
529
530 static int
531 state_has_been_seen (rrs_state_buf_t **previous,
532                      int moves,
533                      rrs_state_t state)
534 {
535     int i;
536
537     for (i=0; i <= moves; i++)
538         if (rrs_state_buf_contains (previous[i], state))
539             return 1;
540     return 0;
541 }
542
543 static int
544 rrs_find_new_states (rr_board_t *board,
545                      rrs_state_buf_t **previous,
546                      int moves,
547                      rrs_state_buf_t *new,
548                      rrs_state_t *solution_state)
549 {
550     int i, ri;
551     rr_direction_t dir;
552     rrs_state_t state, new_state;
553     rrs_state_buf_t *initial;
554     rr_status_t status;
555     rr_robot_t robot;
556
557     initial = previous[moves];
558
559     for (i = 0; i < initial->num_states; i++) {
560         state = initial->state[i];
561
562         rrs_state_set_board (state, board);
563         for (ri = 0; ri < RR_NUM_ROBOTS; ri++) {
564             robot = rr_robot_from_idx (ri);
565             for (dir = RR_DIRECTION_NORTH; dir <= RR_DIRECTION_EAST; dir++) {
566                 status = rr_board_move (board, robot, dir);
567                 if (status == RR_STATUS_SUCCESS) {
568                     new_state = rrs_state_get_from_board (board);
569                     if (! state_has_been_seen (previous, moves, new_state)) {
570                         rrs_state_buf_add_sorted (new, new_state);
571                         if (rr_board_solved (board)) {
572                             *solution_state = new_state;
573                             return 1;
574                         }
575                     }
576                     rr_board_undo (board);
577                 }
578             }
579         }
580     }
581
582     return 0;
583 }
584