From 9637de044433983a74201dc9526ab6a3db39725b Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 26 Jun 2003 01:20:36 +0000 Subject: [PATCH] Added bidding and solution demonstration. --- src/rrsolve.c | 190 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 171 insertions(+), 19 deletions(-) diff --git a/src/rrsolve.c b/src/rrsolve.c index 80f17b1..af6d086 100644 --- a/src/rrsolve.c +++ b/src/rrsolve.c @@ -43,6 +43,9 @@ #define RRS_STATE_SET_ROBOT(s, ri, x, y) ((s) |= (((y) << ((ri)<<3)) | ((x) << (((ri)<<3) + 4)))) #define RRS_STATE_GET_ROBOT(s, ri, x, y) { (y) = ((s) >> ((ri)<<3)) & 0xf; (x) = ((s) >> (((ri)<<3) + 4)) & 0xf; } +static void +handle_events (rr_client_t *client); + static rrs_state_t rrs_state_get_from_board (rr_board_t *board); @@ -71,15 +74,73 @@ trace_solution (rr_board_t *board, rrs_state_t solution_state, rrs_solution_t *solution); +char TOUGH[] = "\n" +" === === === === === === === === === === === === === === === === \n" +"|... ... ... ...|r.. ... ... ... ... ... ... ...|... ... ... ...|\n" +" === \n" +"|... ... ... ... ... ... ... ... ... ... ... ... ...|.rs Y.. ...|\n" +" \n" +"|... ... ... ... ... .bo|... ... ... .bt|... ... ... ... ... ...|\n" +" === === \n" +"|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n" +" === \n" +"|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n" +" === \n" +"|... ... .gc|... ... ... ...|.rt ... ... ... ... ... ...|.go ...|\n" +" === === === === \n" +"|... ... ... ... ... ... ... ... ... ... ... .yc|... ... ... ...|\n" +" === === === \n" +"|...|.YS ... ... ... ... ...|... ...|... ... ... ... ... ... ...|\N" +" === \n" +"|... ... ... ... ... ... ...|... ...|... ... ...|.ww ... ... ...|\n" +" === === === === \n" +"|... .yo|... ... ...|bbs ... ... ... ...|.bc ... ... ... ... ...|\n" +" === \n" +"|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...|\n" +" === === \n" +"|... ... ... ... ... ... ... ... ... .yt|... ... ... ... ... ...|\n" +" === === \n" +"|... ... ... ... ... ... .rc|... ... ... ... ... ... ... .gs|...|\n" +" === \n" +"|... ... ... ... ... ... ... ... ... ... ... ... ... ... ... g..|\n" +" === \n" +"|... ...|.gt ... ... ... ... ... ... ... ... ... ...|.ro ... ...|\n" +" === \n" +"|... ... ... ... ... ...|... ... ... ... ...|... ... ... ... ...|\n" +" === === === === === === === === === === === === === === === === " +/* +Move #1 generated 11 new states. +Move #2 generated 59 new states. +Move #3 generated 216 new states. +Move #4 generated 640 new states. +Move #5 generated 1701 new states. +Move #6 generated 4239 new states. +Move #7 generated 10041 new states. +Move #8 generated 22678 new states. +Move #9 generated 49103 new states. +Move #10 generated 102154 new states. +Move #11 generated 204086 new states. +Move #12 generated 391534 new states. +Move #13 generated 722808 new states. +Move #14 generated 1285932 new states. +Move #15 generated 2204971 new states. +Found solution of 16 moves in 3694.8 seconds. +Traced solution in 0.052438 seconds. +Solution (16 moves): + Move #0: yellow east, south, west +Move #3: green south, west, north +Move #6: blue east, north, west, south +Move #10: yellow south, east, south +Move #13: green west +Move #14: yellow north +*/ + int main (int argc, char *argv[]) { args_t args; rr_status_t status; rr_client_t *client; - rr_board_t *board; - rrs_solution_t solution; - char *diagram; args_parse (&args, argc, argv); @@ -98,28 +159,116 @@ main (int argc, char *argv[]) return 1; } - status = rr_client_show (client, &diagram); - if (status) { - fprintf (stderr, "Error in rr_client_new: %s\n", rr_status_str (status)); - return 1; - } - board = rr_board_create_from_str (diagram); + handle_events (client); - puts (rr_board_to_str (board)); + rr_client_destroy (client); - rrs_solution_init (&solution); - solve_board (board, &solution); - printf ("Solution (%d moves):", solution.num_moves); - rrs_solution_print (&solution); - rrs_solution_fini (&solution); + return 0; +} - free (diagram); +static void +handle_events (rr_client_t *client) +{ + int i; + rr_status_t status; + rr_notice_t *notice; + rrs_solution_t solution; + rr_board_t *board = NULL; + char *diagram; + struct timespec move_delay = { 1, 200000000l }; - rr_board_destroy (board); + while (1) { + status = rr_client_next_notice (client, ¬ice); + if (status) { + fprintf (stderr, "ERROR during rr_client_next_notice: %s\n", + rr_status_str (status)); + return; + } - rr_client_destroy (client); + printf ("Received notice of type %d\n", notice->type); - return 0; + switch (notice->type) { + case RR_NOTICE_GAMEOVER: + case RR_NOTICE_JOIN: + status = rr_client_show (client, &diagram); + if (status) { + fprintf (stderr, "Error in rr_client_show: %s\n", rr_status_str (status)); + goto DONE; + } + if (board == NULL) + board = rr_board_create_from_str (diagram); + else + rr_board_parse (board, diagram); + + /* XXX: Fixing the server to send a NOTICE TURN here would let me drop this code */ + rrs_solution_init (&solution); + solve_board (board, &solution); + rr_client_bid (client, solution.num_moves); + break; + case RR_NOTICE_TURN: + rr_board_set_goal_target (board, notice->u.target); + rrs_solution_init (&solution); + solve_board (board, &solution); + rr_client_bid (client, solution.num_moves); + break; + case RR_NOTICE_ACTIVATE: + for (i = 0; i < solution.num_moves; i++) { + status = rr_client_move (client, + solution.move[i].robot, + solution.move[i].dir); + if (status) { + rr_client_message (client, "Drat, looks like I was wrong."); + rr_client_pass (client); + break; + } + nanosleep (&move_delay, NULL); + } + rrs_solution_fini (&solution); + return; + break; + case RR_NOTICE_ABANDON: + rr_client_abandon (client); + break; + case RR_NOTICE_NOBID: + rr_client_nobid (client); + break; + case RR_NOTICE_POSITION: + rr_board_position_robot (board, + notice->u.position.robot, + notice->u.position.x, + notice->u.position.y); + break; + case RR_NOTICE_GAMESTATE: + if (notice->u.gamestate == RR_GAMESTATE_SHOW) { + if (solution.num_moves) { + printf ("My solution (%d moves):", solution.num_moves); + rrs_solution_print (&solution); + } + } + break; + case RR_NOTICE_GAME: + case RR_NOTICE_USER: + case RR_NOTICE_QUIT: + case RR_NOTICE_DISPOSE: + case RR_NOTICE_MESSAGE: + case RR_NOTICE_WATCH: + case RR_NOTICE_PART: + case RR_NOTICE_BID: + case RR_NOTICE_REVOKE: + case RR_NOTICE_TIMER: + case RR_NOTICE_ACTIVE: + case RR_NOTICE_MOVE: + case RR_NOTICE_UNDO: + case RR_NOTICE_RESET: + case RR_NOTICE_SCORE: + /* Ignore these notices */ + break; + } + } + + DONE: + if (board) + rr_board_destroy (board); } static void @@ -185,6 +334,9 @@ solve_board (rr_board_t *board, rrs_solution_t *solution) printf ("Board is solved to begin with.\n"); return RR_STATUS_SUCCESS; } + + printf ("Now trying to solve:"); + puts (rr_board_to_str (board)); gettimeofday (&tv_start, NULL); -- 2.43.0