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 main (int argc, char *argv[])
84 args_parse (&args, argc, argv);
89 rrs_solution_t solution;
91 for (i = 0; args.files[i]; i++) {
92 board = rr_board_create_from_file (args.files[i]);
95 rrs_solution_init (&solution);
96 solve_board (board, &solution);
97 rrs_solution_print (&solution);
98 rrs_solution_fini (&solution);
99 rr_board_destroy (board);
102 client = rr_client_create (args.host, args.port, args.user);
103 if (client == NULL) {
104 fprintf (stderr, "Failed connecting to %s:%s as %s\n",
105 args.host, args.port, args.user);
109 status = rr_client_join (client, GAME);
110 if (status == RR_STATUS_NO_GAME)
111 status = rr_client_new (client, GAME);
113 fprintf (stderr, "Error joining or creating game: %s\n", rr_status_str (status));
117 handle_events (client);
119 rr_client_destroy (client);
126 handle_events (rr_client_t *client)
131 rrs_solution_t solution;
134 struct timespec move_delay = { 1, 200000000l };
136 /* XXX: This block of code can go away when add a NOTICE BOARD
137 for new users joining a game. */
139 status = rr_client_show (client, &diagram);
141 fprintf (stderr, "Error in rr_client_show: %s\n", rr_status_str (status));
144 board = rr_board_create_from_str (diagram);
148 /* XXX: This block of code can go away when we add a NOTICE TURN
149 for new users joining a game in progress. */
151 rrs_solution_init (&solution);
152 solve_board (board, &solution);
153 rr_client_bid (client, solution.num_moves);
157 status = rr_client_next_notice (client, ¬ice);
159 if (status == RR_STATUS_EOF)
160 fprintf (stderr, "Server has disconnected. Exiting.\n");
162 fprintf (stderr, "ERROR during rr_client_next_notice: %s\n",
163 rr_status_str (status));
167 fprintf (stderr, "No notice during rr_client_next_notice\n");
171 switch (notice->type) {
172 /* XXX: The processing needed for GAMEOVER, JOIN, and TURN
173 is a mess right now. There should be one NOTICE to say
174 the board has changed and one to say a TURN has started,
175 rather than the current mess. */
176 case RR_NOTICE_GAMEOVER:
177 status = rr_client_show (client, &diagram);
179 fprintf (stderr, "Error in rr_client_show: %s\n", rr_status_str (status));
182 rr_board_parse (board, diagram);
186 rr_board_set_goal_target (board, notice->u.target);
187 rrs_solution_init (&solution);
188 solve_board (board, &solution);
189 rr_client_bid (client, solution.num_moves);
191 case RR_NOTICE_ACTIVATE:
192 for (i = 0; i < solution.num_moves; i++) {
193 status = rr_client_move (client,
194 solution.move[i].robot,
195 solution.move[i].dir);
197 rr_client_message (client, "Drat, looks like I was wrong.");
198 rr_client_pass (client);
201 nanosleep (&move_delay, NULL);
203 rrs_solution_fini (&solution);
205 case RR_NOTICE_ABANDON:
206 rr_client_abandon (client);
208 case RR_NOTICE_NOBID:
209 rr_client_nobid (client);
211 case RR_NOTICE_POSITION:
212 rr_board_position_robot (board,
213 notice->u.position.robot,
214 notice->u.position.x,
215 notice->u.position.y);
217 case RR_NOTICE_GAMESTATE:
218 if (notice->u.gamestate == RR_GAMESTATE_SHOW) {
219 if (solution.num_moves) {
220 printf ("My solution (%d moves):", solution.num_moves);
221 rrs_solution_print (&solution);
229 case RR_NOTICE_DISPOSE:
230 case RR_NOTICE_MESSAGE:
231 case RR_NOTICE_WATCH:
234 case RR_NOTICE_REVOKE:
235 case RR_NOTICE_TIMER:
236 case RR_NOTICE_ACTIVE:
239 case RR_NOTICE_RESET:
240 case RR_NOTICE_SCORE:
241 /* Ignore these notices */
251 rr_board_destroy (board);
255 rrs_solution_print (rrs_solution_t *solution)
258 rr_robot_t last_robot;
261 last_robot = RR_ROBOT_NONE;
262 for (i=0; i < solution->num_moves; i++) {
263 move = &solution->move[i];
264 if (move->robot == last_robot)
265 printf (", %s", rr_direction_str (move->dir));
267 printf ("\n Move #%d: %s %s",
268 i+1, rr_robot_str (move->robot), rr_direction_str (move->dir));
269 last_robot = move->robot;
275 rrs_state_get_from_board (rr_board_t *board)
283 for (i=0; i < RR_NUM_ROBOTS; i++) {
284 rr_board_find_robot (board, rr_robot_from_idx (i), &x, &y);
285 RRS_STATE_SET_ROBOT (state, i, x, y);
292 rrs_state_set_board (rrs_state_t state, rr_board_t *board)
297 for (i=0; i < RR_NUM_ROBOTS; i++) {
298 RRS_STATE_GET_ROBOT (state, i, x, y);
299 rr_board_position_robot (board, rr_robot_from_idx (i), x, y);
305 solve_board (rr_board_t *board, rrs_solution_t *solution)
309 rrs_state_t solution_state;
310 rrs_state_buf_t **states;
311 struct timeval tv_start, tv_end;
313 if (rr_board_solved (board)) {
314 printf ("Board is solved to begin with.\n");
315 return RR_STATUS_SUCCESS;
318 printf ("Now trying to solve:");
319 puts (rr_board_to_str (board));
321 gettimeofday (&tv_start, NULL);
323 status = find_solution_states (board, &states, &moves, &solution_state);
327 gettimeofday (&tv_end, NULL);
330 printf ("It seems impossible.\n");
332 printf ("Found solution of %d moves in %g seconds.\n",
334 tv_end.tv_sec - tv_start.tv_sec
335 + (tv_end.tv_usec - tv_start.tv_usec) / 1e6);
337 gettimeofday (&tv_start, NULL);
339 trace_solution (board, states, moves, solution_state, solution);
341 gettimeofday (&tv_end, NULL);
343 printf ("Traced solution in %g seconds.\n",
344 tv_end.tv_sec - tv_start.tv_sec
345 + (tv_end.tv_usec - tv_start.tv_usec) / 1e6);
348 for (i=0; i <= moves; i++) {
349 rrs_state_buf_destroy (states[i]);
355 return RR_STATUS_SUCCESS;
359 rrs_state_connected (rrs_state_t a, rrs_state_t b,
360 rr_robot_t *robot, rr_direction_t *dir)
364 rrs_state_t diff, mask;
370 for (i=0; i < 8; i++) {
383 *robot = RR_ROBOT_BLUE;
387 *robot = RR_ROBOT_GREEN;
391 *robot = RR_ROBOT_RED;
395 *robot = RR_ROBOT_YELLOW;
401 delta = ((b & mask) >> shift) - ((a & mask) >> shift);
405 *dir = RR_DIRECTION_WEST;
407 *dir = RR_DIRECTION_EAST;
410 *dir = RR_DIRECTION_NORTH;
412 *dir = RR_DIRECTION_SOUTH;
418 trace_solution (rr_board_t *board,
419 rrs_state_buf_t **states, int moves,
420 rrs_state_t solution_state,
421 rrs_solution_t *solution)
425 rrs_state_buf_t *buf;
427 rr_robot_t robot, robot_found, last_robot_found = RR_ROBOT_NONE;
428 rr_direction_t dir, dir_found;
429 rrs_state_t state_found;
431 for (i = moves-1; i >= 0; i--) {
433 for (j = 0; j < buf->num_states; j++) {
434 if (rrs_state_connected (buf->state[j], solution_state,
436 rrs_state_set_board (buf->state[j], board);
437 status = rr_board_move (board, robot, dir);
438 if (status == RR_STATUS_SUCCESS) {
439 if (rrs_state_get_from_board (board) == solution_state) {
443 state_found = buf->state[j];
444 if (robot_found == last_robot_found)
447 last_robot_found = robot_found;
454 rrs_solution_prepend (solution, robot_found, dir_found);
455 solution_state = state_found;
457 fprintf (stderr, "ERROR: Failed to trace solution backwards to 0x%x at move %d\n",
458 solution_state, i+1);
465 find_solution_states (rr_board_t *board,
466 rrs_state_buf_t ***solution_states,
468 rrs_state_t *final_state)
472 rrs_state_buf_t **states, **new_states;
474 states = malloc (sizeof (rrs_state_buf_t *));
476 return RR_STATUS_NO_MEMORY;
478 initial = rrs_state_get_from_board (board);
480 states[0] = rrs_state_buf_create (1);
481 if (states[0] == NULL)
482 return RR_STATUS_NO_MEMORY;
483 rrs_state_buf_add (states[0], initial);
489 new_states = realloc (states, (moves + 1) * sizeof (rrs_state_buf_t *));
490 if (new_states == NULL)
491 return RR_STATUS_NO_MEMORY;
494 states[moves] = rrs_state_buf_create (RRS_BRANCHING_FACTOR_ESTIMATE
495 * states[moves-1]->num_states);
496 if (states[moves] == NULL)
497 return RR_STATUS_NO_MEMORY;
499 solved = rrs_find_new_states (board, states, moves-1, states[moves], final_state);
504 if (states[moves]->num_states == 0) {
505 printf ("Can't make any further progress after %d moves.\n", moves);
509 printf ("Move #%d generated %d new states.\n",
510 moves, states[moves]->num_states);
514 *num_state_buf = moves;
515 *solution_states = states;
517 return RR_STATUS_SUCCESS;
521 state_has_been_seen (rrs_state_buf_t **previous,
527 for (i=0; i <= moves; i++)
528 if (rrs_state_buf_contains (previous[i], state))
534 rrs_find_new_states (rr_board_t *board,
535 rrs_state_buf_t **previous,
537 rrs_state_buf_t *new,
538 rrs_state_t *solution_state)
542 rrs_state_t state, new_state;
543 rrs_state_buf_t *initial;
547 initial = previous[moves];
549 for (i = 0; i < initial->num_states; i++) {
550 state = initial->state[i];
552 rrs_state_set_board (state, board);
553 for (ri = 0; ri < RR_NUM_ROBOTS; ri++) {
554 robot = rr_robot_from_idx (ri);
555 for (dir = RR_DIRECTION_NORTH; dir <= RR_DIRECTION_EAST; dir++) {
556 status = rr_board_move (board, robot, dir);
557 if (status == RR_STATUS_SUCCESS) {
558 new_state = rrs_state_get_from_board (board);
559 if (! state_has_been_seen (previous, moves, new_state)) {
560 rrs_state_buf_add_sorted (new, new_state);
561 if (rr_board_solved (board)) {
562 *solution_state = new_state;
566 rr_board_undo (board);