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 /* Tuning this can reduce excess reallocs */
36 #define RRS_BRANCHING_FACTOR_ESTIMATE 10
38 #define RRS_STATE_SET_ROBOT(s, ri, x, y) ((s) |= (((y) << ((ri)<<3)) | ((x) << (((ri)<<3) + 4))))
39 #define RRS_STATE_GET_ROBOT(s, ri, x, y) { (y) = ((s) >> ((ri)<<3)) & 0xf; (x) = ((s) >> (((ri)<<3) + 4)) & 0xf; }
42 handle_events (rr_client_t *client);
45 rrs_state_get_from_board (rr_board_t *board);
48 rrs_solution_print (rrs_solution_t *solution);
51 solve_board (rr_board_t *board, rrs_solution_t *solution);
54 find_solution_states (rr_board_t *board,
55 rrs_state_buf_t ***solution_states,
57 rrs_state_t *final_state);
60 rrs_find_new_states (rr_board_t *board,
61 rrs_state_buf_t **previous,
64 rrs_state_t *solution_state);
67 trace_solution (rr_board_t *board,
68 rrs_state_buf_t **states, int moves,
69 rrs_state_t solution_state,
70 rrs_solution_t *solution);
73 main (int argc, char *argv[])
79 args_parse (&args, argc, argv);
84 rrs_solution_t solution;
86 for (i = 0; args.files[i]; i++) {
87 board = rr_board_create_from_file (args.files[i]);
90 rrs_solution_init (&solution);
91 solve_board (board, &solution);
92 rrs_solution_print (&solution);
93 rrs_solution_fini (&solution);
94 rr_board_destroy (board);
97 client = rr_client_create (args.host, args.port, args.user);
99 fprintf (stderr, "Failed connecting to %s:%s as %s\n",
100 args.host, args.port, args.user);
104 status = rr_client_join (client, args.game);
105 if (status == RR_STATUS_NO_GAME)
106 status = rr_client_new (client, args.game);
108 fprintf (stderr, "Error joining or creating game: %s\n", rr_status_str (status));
112 handle_events (client);
114 rr_client_destroy (client);
121 handle_events (rr_client_t *client)
126 rrs_solution_t solution;
129 struct timespec move_delay = { 1, 200000000l };
131 /* XXX: This block of code can go away when add a NOTICE BOARD
132 for new users joining a game. */
134 status = rr_client_show (client, &diagram);
136 fprintf (stderr, "Error in rr_client_show: %s\n", rr_status_str (status));
139 board = rr_board_create_from_str (diagram);
143 /* XXX: This block of code can go away when we add a NOTICE TURN
144 for new users joining a game in progress. */
146 rrs_solution_init (&solution);
147 solve_board (board, &solution);
148 rr_client_bid (client, solution.num_moves);
149 rr_client_nobid (client);
153 status = rr_client_next_notice (client, ¬ice);
155 if (status == RR_STATUS_EOF)
156 fprintf (stderr, "Server has disconnected. Exiting.\n");
158 fprintf (stderr, "ERROR during rr_client_next_notice: %s\n",
159 rr_status_str (status));
163 fprintf (stderr, "No notice during rr_client_next_notice\n");
167 switch (notice->type) {
168 /* XXX: The processing needed for GAMEOVER, JOIN, and TURN
169 is a mess right now. There should be one NOTICE to say
170 the board has changed and one to say a TURN has started,
171 rather than the current mess. */
172 case RR_NOTICE_GAMEOVER:
173 status = rr_client_show (client, &diagram);
175 fprintf (stderr, "Error in rr_client_show: %s\n", rr_status_str (status));
178 rr_board_parse (board, diagram);
182 rr_board_set_goal_target (board, notice->u.target);
183 rrs_solution_init (&solution);
184 solve_board (board, &solution);
185 rr_client_bid (client, solution.num_moves);
186 rr_client_nobid (client);
188 case RR_NOTICE_ACTIVATE:
189 for (i = 0; i < solution.num_moves; i++) {
190 status = rr_client_move (client,
191 solution.move[i].robot,
192 solution.move[i].dir);
194 rr_client_message (client, "Drat, looks like I was wrong.");
195 rr_client_pass (client);
198 nanosleep (&move_delay, NULL);
200 rrs_solution_fini (&solution);
202 case RR_NOTICE_ABANDON:
203 rr_client_abandon (client);
205 case RR_NOTICE_NOBID:
206 rr_client_nobid (client);
208 case RR_NOTICE_POSITION:
209 rr_board_position_robot (board,
210 notice->u.position.robot,
211 notice->u.position.x,
212 notice->u.position.y);
214 case RR_NOTICE_GAMESTATE:
215 if (notice->u.gamestate == RR_GAMESTATE_SHOW) {
216 if (solution.num_moves) {
217 printf ("My solution (%d moves):", solution.num_moves);
218 rrs_solution_print (&solution);
226 case RR_NOTICE_DISPOSE:
227 case RR_NOTICE_MESSAGE:
228 case RR_NOTICE_WATCH:
231 case RR_NOTICE_REVOKE:
232 case RR_NOTICE_TIMER:
233 case RR_NOTICE_ACTIVE:
236 case RR_NOTICE_RESET:
237 case RR_NOTICE_SCORE:
238 /* Ignore these notices */
248 rr_board_destroy (board);
252 rrs_solution_print (rrs_solution_t *solution)
255 rr_robot_t last_robot;
258 last_robot = RR_ROBOT_NONE;
259 for (i=0; i < solution->num_moves; i++) {
260 move = &solution->move[i];
261 if (move->robot == last_robot)
262 printf (", %s", rr_direction_str (move->dir));
264 printf ("\n Move #%d: %s %s",
265 i+1, rr_robot_str (move->robot), rr_direction_str (move->dir));
266 last_robot = move->robot;
272 rrs_state_get_from_board (rr_board_t *board)
280 for (i=0; i < RR_NUM_ROBOTS; i++) {
281 rr_board_find_robot (board, rr_robot_from_idx (i), &x, &y);
282 RRS_STATE_SET_ROBOT (state, i, x, y);
289 rrs_state_set_board (rrs_state_t state, rr_board_t *board)
294 for (i=0; i < RR_NUM_ROBOTS; i++) {
295 RRS_STATE_GET_ROBOT (state, i, x, y);
296 rr_board_position_robot (board, rr_robot_from_idx (i), x, y);
302 solve_board (rr_board_t *board, rrs_solution_t *solution)
306 rrs_state_t solution_state;
307 rrs_state_buf_t **states;
308 struct timeval tv_start, tv_end;
310 if (rr_board_solved (board)) {
311 printf ("Board is solved to begin with.\n");
312 return RR_STATUS_SUCCESS;
315 printf ("Now trying to solve:");
316 puts (rr_board_to_str (board));
318 gettimeofday (&tv_start, NULL);
320 status = find_solution_states (board, &states, &moves, &solution_state);
324 gettimeofday (&tv_end, NULL);
327 printf ("It seems impossible.\n");
329 printf ("Found solution of %d moves in %g seconds.\n",
331 tv_end.tv_sec - tv_start.tv_sec
332 + (tv_end.tv_usec - tv_start.tv_usec) / 1e6);
334 gettimeofday (&tv_start, NULL);
336 trace_solution (board, states, moves, solution_state, solution);
338 gettimeofday (&tv_end, NULL);
340 printf ("Traced solution in %g seconds.\n",
341 tv_end.tv_sec - tv_start.tv_sec
342 + (tv_end.tv_usec - tv_start.tv_usec) / 1e6);
345 for (i=0; i <= moves; i++) {
346 rrs_state_buf_destroy (states[i]);
352 return RR_STATUS_SUCCESS;
356 rrs_state_connected (rrs_state_t a, rrs_state_t b,
357 rr_robot_t *robot, rr_direction_t *dir)
361 rrs_state_t diff, mask;
367 for (i=0; i < 8; i++) {
380 *robot = RR_ROBOT_BLUE;
384 *robot = RR_ROBOT_GREEN;
388 *robot = RR_ROBOT_RED;
392 *robot = RR_ROBOT_YELLOW;
398 delta = ((b & mask) >> shift) - ((a & mask) >> shift);
402 *dir = RR_DIRECTION_WEST;
404 *dir = RR_DIRECTION_EAST;
407 *dir = RR_DIRECTION_NORTH;
409 *dir = RR_DIRECTION_SOUTH;
415 trace_solution (rr_board_t *board,
416 rrs_state_buf_t **states, int moves,
417 rrs_state_t solution_state,
418 rrs_solution_t *solution)
422 rrs_state_buf_t *buf;
424 rr_robot_t robot, robot_found, last_robot_found = RR_ROBOT_NONE;
425 rr_direction_t dir, dir_found;
426 rrs_state_t state_found;
428 for (i = moves-1; i >= 0; i--) {
430 for (j = 0; j < buf->num_states; j++) {
431 if (rrs_state_connected (buf->state[j], solution_state,
433 rrs_state_set_board (buf->state[j], board);
434 status = rr_board_move (board, robot, dir);
435 if (status == RR_STATUS_SUCCESS) {
436 if (rrs_state_get_from_board (board) == solution_state) {
440 state_found = buf->state[j];
441 if (robot_found == last_robot_found)
444 last_robot_found = robot_found;
451 rrs_solution_prepend (solution, robot_found, dir_found);
452 solution_state = state_found;
454 fprintf (stderr, "ERROR: Failed to trace solution backwards to 0x%x at move %d\n",
455 solution_state, i+1);
462 find_solution_states (rr_board_t *board,
463 rrs_state_buf_t ***solution_states,
465 rrs_state_t *final_state)
469 rrs_state_buf_t **states, **new_states;
471 states = malloc (sizeof (rrs_state_buf_t *));
473 return RR_STATUS_NO_MEMORY;
475 initial = rrs_state_get_from_board (board);
477 states[0] = rrs_state_buf_create (1);
478 if (states[0] == NULL)
479 return RR_STATUS_NO_MEMORY;
480 rrs_state_buf_add (states[0], initial);
486 new_states = realloc (states, (moves + 1) * sizeof (rrs_state_buf_t *));
487 if (new_states == NULL)
488 return RR_STATUS_NO_MEMORY;
491 states[moves] = rrs_state_buf_create (RRS_BRANCHING_FACTOR_ESTIMATE
492 * states[moves-1]->num_states);
493 if (states[moves] == NULL)
494 return RR_STATUS_NO_MEMORY;
496 solved = rrs_find_new_states (board, states, moves-1, states[moves], final_state);
501 if (states[moves]->num_states == 0) {
502 printf ("Can't make any further progress after %d moves.\n", moves);
506 printf ("Move #%d generated %d new states.\n",
507 moves, states[moves]->num_states);
511 *num_state_buf = moves;
512 *solution_states = states;
514 return RR_STATUS_SUCCESS;
518 state_has_been_seen (rrs_state_buf_t **previous,
524 for (i=0; i <= moves; i++)
525 if (rrs_state_buf_contains (previous[i], state))
531 rrs_find_new_states (rr_board_t *board,
532 rrs_state_buf_t **previous,
534 rrs_state_buf_t *new,
535 rrs_state_t *solution_state)
539 rrs_state_t state, new_state;
540 rrs_state_buf_t *initial;
544 initial = previous[moves];
546 for (i = 0; i < initial->num_states; i++) {
547 state = initial->state[i];
549 rrs_state_set_board (state, board);
550 for (ri = 0; ri < RR_NUM_ROBOTS; ri++) {
551 robot = rr_robot_from_idx (ri);
552 for (dir = RR_DIRECTION_NORTH; dir <= RR_DIRECTION_EAST; dir++) {
553 status = rr_board_move (board, robot, dir);
554 if (status == RR_STATUS_SUCCESS) {
555 new_state = rrs_state_get_from_board (board);
556 if (! state_has_been_seen (previous, moves, new_state)) {
557 rrs_state_buf_add_sorted (new, new_state);
558 if (rr_board_solved (board)) {
559 *solution_state = new_state;
563 rr_board_undo (board);