1 /* rrsolve - Simple RR solver and librr client.
3 * Copyright © 2003 Carl Worth
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.
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.
24 * Author: Carl Worth <carl@theworths.org>
35 #define HOST "localhost"
37 #define USER "rrsolve"
40 /* Tuning this can reduce excess reallocs */
41 #define RRS_BRANCHING_FACTOR_ESTIMATE 10
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; }
47 handle_events (rr_client_t *client);
50 rrs_state_get_from_board (rr_board_t *board);
53 rrs_solution_print (rrs_solution_t *solution);
56 solve_board (rr_board_t *board, rrs_solution_t *solution);
59 find_solution_states (rr_board_t *board,
60 rrs_state_buf_t ***solution_states,
62 rrs_state_t *final_state);
65 rrs_find_new_states (rr_board_t *board,
66 rrs_state_buf_t **previous,
69 rrs_state_t *solution_state);
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);
78 " === === === === === === === === === === === === === === === === \n"
79 "|... ... ... ...|r.. ... ... ... ... ... ... ...|... ... ... ...|\n"
81 "|... ... ... ... ... ... ... ... ... ... ... ... ...|.rs Y.. ...|\n"
83 "|... ... ... ... ... .bo|... ... ... .bt|... ... ... ... ... ...|\n"
85 "|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n"
87 "|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n"
89 "|... ... .gc|... ... ... ...|.rt ... ... ... ... ... ...|.go ...|\n"
91 "|... ... ... ... ... ... ... ... ... ... ... .yc|... ... ... ...|\n"
93 "|...|.YS ... ... ... ... ...|... ...|... ... ... ... ... ... ...|\n"
95 "|... ... ... ... ... ... ...|... ...|... ... ...|.ww ... ... ...|\n"
97 "|... .yo|... ... ...|bbs ... ... ... ...|.bc ... ... ... ... ...|\n"
99 "|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n"
101 "|... ... ... ... ... ... ... ... ... .yt|... ... ... ... ... ...|\n"
103 "|... ... ... ... ... ... .rc|... ... ... ... ... ... ... .gs|...|\n"
105 "|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... g..|\n"
107 "|... ...|.gt ... ... ... ... ... ... ... ... ... ...|.ro ... ...|\n"
109 "|... ... ... ... ... ...|... ... ... ... ...|... ... ... ... ...|\n"
110 " === === === === === === === === === === === === === === === === ";
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.
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
135 Move #14: yellow north
139 main (int argc, char *argv[])
145 args_parse (&args, argc, argv);
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);
154 status = rr_client_join (client, GAME);
155 if (status == RR_STATUS_NO_GAME)
156 status = rr_client_new (client, GAME);
158 fprintf (stderr, "Error joining or creating game: %s\n", rr_status_str (status));
162 handle_events (client);
164 rr_client_destroy (client);
170 handle_events (rr_client_t *client)
175 rrs_solution_t solution;
176 rr_board_t *board = NULL;
178 struct timespec move_delay = { 1, 200000000l };
181 status = rr_client_next_notice (client, ¬ice);
183 fprintf (stderr, "ERROR during rr_client_next_notice: %s\n",
184 rr_status_str (status));
188 printf ("Received notice of type %d\n", notice->type);
190 switch (notice->type) {
191 case RR_NOTICE_GAMEOVER:
193 status = rr_client_show (client, &diagram);
195 fprintf (stderr, "Error in rr_client_show: %s\n", rr_status_str (status));
199 board = rr_board_create_from_str (diagram);
201 rr_board_parse (board, diagram);
203 /* XXX: Fixing the server to send a NOTICE TURN here would let me drop this code */
204 rrs_solution_init (&solution);
205 solve_board (board, &solution);
206 rr_client_bid (client, solution.num_moves);
209 rr_board_set_goal_target (board, notice->u.target);
210 rrs_solution_init (&solution);
211 solve_board (board, &solution);
212 rr_client_bid (client, solution.num_moves);
214 case RR_NOTICE_ACTIVATE:
215 for (i = 0; i < solution.num_moves; i++) {
216 status = rr_client_move (client,
217 solution.move[i].robot,
218 solution.move[i].dir);
220 rr_client_message (client, "Drat, looks like I was wrong.");
221 rr_client_pass (client);
224 nanosleep (&move_delay, NULL);
226 rrs_solution_fini (&solution);
228 case RR_NOTICE_ABANDON:
229 rr_client_abandon (client);
231 case RR_NOTICE_NOBID:
232 rr_client_nobid (client);
234 case RR_NOTICE_POSITION:
235 rr_board_position_robot (board,
236 notice->u.position.robot,
237 notice->u.position.x,
238 notice->u.position.y);
240 case RR_NOTICE_GAMESTATE:
241 if (notice->u.gamestate == RR_GAMESTATE_SHOW) {
242 if (solution.num_moves) {
243 printf ("My solution (%d moves):", solution.num_moves);
244 rrs_solution_print (&solution);
251 case RR_NOTICE_DISPOSE:
252 case RR_NOTICE_MESSAGE:
253 case RR_NOTICE_WATCH:
256 case RR_NOTICE_REVOKE:
257 case RR_NOTICE_TIMER:
258 case RR_NOTICE_ACTIVE:
261 case RR_NOTICE_RESET:
262 case RR_NOTICE_SCORE:
263 /* Ignore these notices */
270 rr_board_destroy (board);
274 rrs_solution_print (rrs_solution_t *solution)
277 rr_robot_t last_robot;
280 last_robot = RR_ROBOT_NONE;
281 for (i=0; i < solution->num_moves; i++) {
282 move = &solution->move[i];
283 if (move->robot == last_robot)
284 printf (", %s", rr_direction_str (move->dir));
286 printf ("\n Move #%d: %s %s",
287 i, rr_robot_str (move->robot), rr_direction_str (move->dir));
288 last_robot = move->robot;
294 rrs_state_get_from_board (rr_board_t *board)
302 for (i=0; i < RR_NUM_ROBOTS; i++) {
303 rr_board_find_robot (board, rr_robot_from_idx (i), &x, &y);
304 RRS_STATE_SET_ROBOT (state, i, x, y);
311 rrs_state_set_board (rrs_state_t state, rr_board_t *board)
316 for (i=0; i < RR_NUM_ROBOTS; i++) {
317 RRS_STATE_GET_ROBOT (state, i, x, y);
318 rr_board_position_robot (board, rr_robot_from_idx (i), x, y);
324 solve_board (rr_board_t *board, rrs_solution_t *solution)
328 rrs_state_t solution_state;
329 rrs_state_buf_t **states;
330 struct timeval tv_start, tv_end;
332 if (rr_board_solved (board)) {
333 printf ("Board is solved to begin with.\n");
334 return RR_STATUS_SUCCESS;
337 printf ("Now trying to solve:");
338 puts (rr_board_to_str (board));
340 gettimeofday (&tv_start, NULL);
342 status = find_solution_states (board, &states, &moves, &solution_state);
346 gettimeofday (&tv_end, NULL);
349 printf ("It seems impossible.\n");
351 printf ("Found solution of %d moves in %g seconds.\n",
353 tv_end.tv_sec - tv_start.tv_sec
354 + (tv_end.tv_usec - tv_start.tv_usec) / 1e6);
356 gettimeofday (&tv_start, NULL);
358 trace_solution (board, states, moves, solution_state, solution);
360 gettimeofday (&tv_end, NULL);
362 printf ("Traced solution in %g seconds.\n",
363 tv_end.tv_sec - tv_start.tv_sec
364 + (tv_end.tv_usec - tv_start.tv_usec) / 1e6);
367 for (i=0; i <= moves; i++) {
368 rrs_state_buf_destroy (states[i]);
374 return RR_STATUS_SUCCESS;
378 rrs_state_connected (rrs_state_t a, rrs_state_t b,
379 rr_robot_t *robot, rr_direction_t *dir)
383 rrs_state_t diff, mask;
389 for (i=0; i < 8; i++) {
402 *robot = RR_ROBOT_BLUE;
406 *robot = RR_ROBOT_GREEN;
410 *robot = RR_ROBOT_RED;
414 *robot = RR_ROBOT_YELLOW;
420 delta = ((b & mask) >> shift) - ((a & mask) >> shift);
424 *dir = RR_DIRECTION_WEST;
426 *dir = RR_DIRECTION_EAST;
429 *dir = RR_DIRECTION_NORTH;
431 *dir = RR_DIRECTION_SOUTH;
437 trace_solution (rr_board_t *board,
438 rrs_state_buf_t **states, int moves,
439 rrs_state_t solution_state,
440 rrs_solution_t *solution)
444 rrs_state_buf_t *buf;
448 for (i = moves-1; i >= 0; i--) {
450 for (j = 0; j < buf->num_states; j++) {
451 if (rrs_state_connected (buf->state[j], solution_state,
453 rrs_state_set_board (buf->state[j], board);
454 status = rr_board_move (board, robot, dir);
455 if (status == RR_STATUS_SUCCESS) {
456 if (rrs_state_get_from_board (board) == solution_state) {
457 rrs_solution_prepend (solution, robot, dir);
458 solution_state = buf->state[j];
464 fprintf (stderr, "ERROR: Failed to trace solution backwards to 0x%x at move %d\n",
465 solution_state, i+1);
473 find_solution_states (rr_board_t *board,
474 rrs_state_buf_t ***solution_states,
476 rrs_state_t *final_state)
480 rrs_state_buf_t **states, **new_states;
482 states = malloc (sizeof (rrs_state_buf_t *));
484 return RR_STATUS_NO_MEMORY;
486 initial = rrs_state_get_from_board (board);
488 states[0] = rrs_state_buf_create (1);
489 if (states[0] == NULL)
490 return RR_STATUS_NO_MEMORY;
491 rrs_state_buf_add (states[0], initial);
497 new_states = realloc (states, (moves + 1) * sizeof (rrs_state_buf_t *));
498 if (new_states == NULL)
499 return RR_STATUS_NO_MEMORY;
502 states[moves] = rrs_state_buf_create (RRS_BRANCHING_FACTOR_ESTIMATE
503 * states[moves-1]->num_states);
504 if (states[moves] == NULL)
505 return RR_STATUS_NO_MEMORY;
507 solved = rrs_find_new_states (board, states, moves-1, states[moves], final_state);
512 if (states[moves]->num_states == 0) {
513 printf ("Can't make any further progress after %d moves.\n", moves);
517 printf ("Move #%d generated %d new states.\n",
518 moves, states[moves]->num_states);
522 *num_state_buf = moves;
523 *solution_states = states;
525 return RR_STATUS_SUCCESS;
529 state_has_been_seen (rrs_state_buf_t **previous,
535 for (i=0; i <= moves; i++)
536 if (rrs_state_buf_contains (previous[i], state))
542 rrs_find_new_states (rr_board_t *board,
543 rrs_state_buf_t **previous,
545 rrs_state_buf_t *new,
546 rrs_state_t *solution_state)
550 rrs_state_t state, new_state;
551 rrs_state_buf_t *initial;
555 initial = previous[moves];
557 for (i = 0; i < initial->num_states; i++) {
558 state = initial->state[i];
560 rrs_state_set_board (state, board);
561 for (ri = 0; ri < RR_NUM_ROBOTS; ri++) {
562 robot = rr_robot_from_idx (ri);
563 for (dir = RR_DIRECTION_NORTH; dir <= RR_DIRECTION_EAST; dir++) {
564 status = rr_board_move (board, robot, dir);
565 if (status == RR_STATUS_SUCCESS) {
566 new_state = rrs_state_get_from_board (board);
567 if (! state_has_been_seen (previous, moves, new_state)) {
568 rrs_state_buf_add_sorted (new, new_state);
569 if (rr_board_solved (board)) {
570 *solution_state = new_state;
574 rr_board_undo (board);