[Commit] rrsolve/src rrsolve.c,1.5,1.6

Carl Worth commit at keithp.com
Thu Jun 26 21:16:51 PDT 2003


Committed by: cworth

Update of /local/src/CVS/rrsolve/src
In directory home.keithp.com:/tmp/cvs-serv18057/src

Modified Files:
	rrsolve.c 
Log Message:
Slight tweak in solution selection. It now prefers long sequences of the same robot
moving, (searching from the last move backwards). This makes some solutions much easier
to follow.

Fixed handling of board changes in various conditions, (some of these hacks can go away with
the new NOTICE BOARD from the server).

Fixed some memory leaks.

Index: rrsolve.c
===================================================================
RCS file: /local/src/CVS/rrsolve/src/rrsolve.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- rrsolve.c	26 Jun 2003 11:17:49 -0000	1.5
+++ rrsolve.c	27 Jun 2003 03:16:49 -0000	1.6
@@ -173,15 +173,38 @@
     rr_status_t status;
     rr_notice_t *notice;
     rrs_solution_t solution;
-    rr_board_t *board = NULL;
+    rr_board_t *board;
     char *diagram;
     struct timespec move_delay = { 1, 200000000l };
 
+    /* XXX: This block of code can go away when add a NOTICE BOARD
+       for new users joining a game. */
+    {
+	status = rr_client_show (client, &diagram);
+	if (status) {
+	    fprintf (stderr, "Error in rr_client_show: %s\n", rr_status_str (status));
+	    goto DONE;
+	}
+	board = rr_board_create_from_str (diagram);
+	free (diagram);
+    }
+
+    /* XXX: This block of code can go away when we add a NOTICE TURN
+       for new users joining a game in progress. */
+    {
+	rrs_solution_init (&solution);
+	solve_board (board, &solution);
+	rr_client_bid (client, solution.num_moves);
+    }
+
     while (1) {
 	status = rr_client_next_notice (client, &notice);
 	if (status) {
-	    fprintf (stderr, "ERROR during rr_client_next_notice: %s\n",
-		     rr_status_str (status));
+	    if (status == RR_STATUS_EOF)
+		fprintf (stderr, "Server has disconnected. Exiting.\n");
+	    else
+		fprintf (stderr, "ERROR during rr_client_next_notice: %s\n",
+			 rr_status_str (status));
 	    return;
 	}
 	if (!notice) {
@@ -190,22 +213,18 @@
 	}
 
 	switch  (notice->type) {
+	/* XXX: The processing needed for GAMEOVER, JOIN, and TURN
+	   is a mess right now. There should be one NOTICE to say
+	   the board has changed and one to say a TURN has started,
+	   rather than the current mess. */
 	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);
+	    rr_board_parse (board, diagram);
+	    free (diagram);
 	    break;
 	case RR_NOTICE_TURN:
 	    rr_board_set_goal_target (board, notice->u.target);
@@ -249,6 +268,7 @@
 	    break;
 	case RR_NOTICE_GAME:
 	case RR_NOTICE_USER:
+	case RR_NOTICE_JOIN:
 	case RR_NOTICE_QUIT:
 	case RR_NOTICE_DISPOSE:
 	case RR_NOTICE_MESSAGE:
@@ -265,9 +285,12 @@
 	    /* Ignore these notices */
 	    break;
 	}
+	free (notice);
     }
 
   DONE:
+    if (notice)
+	free (notice);
     if (board)
 	rr_board_destroy (board);
 }
@@ -286,7 +309,7 @@
 	    printf (", %s", rr_direction_str (move->dir));
 	else
 	    printf ("\n Move #%d: %s %s",
-		    i, rr_robot_str (move->robot), rr_direction_str (move->dir));
+		    i+1, rr_robot_str (move->robot), rr_direction_str (move->dir));
 	last_robot = move->robot;
     }
     printf ("\n");
@@ -444,8 +467,10 @@
     rr_status_t status;
     int i, j;
     rrs_state_buf_t *buf;
-    rr_robot_t robot;
-    rr_direction_t dir;
+    int found_move;
+    rr_robot_t robot, robot_found, last_robot_found = RR_ROBOT_NONE;
+    rr_direction_t dir, dir_found;
+    rrs_state_t state_found;
 
     for (i = moves-1; i >= 0; i--) {
 	buf = states[i];
@@ -456,18 +481,27 @@
 		status = rr_board_move (board, robot, dir);
 		if (status == RR_STATUS_SUCCESS) {
 		    if (rrs_state_get_from_board (board) == solution_state) {
-			rrs_solution_prepend (solution, robot, dir);
-			solution_state = buf->state[j];
-			goto NEXT_MOVE;
+			found_move = 1;
+			robot_found = robot;
+			dir_found = dir;
+			state_found = buf->state[j];
+			if (robot_found == last_robot_found)
+			    goto NEXT_MOVE;
+			else
+			    last_robot_found = robot_found;
 		    }
 		}
 	    }
 	}
-	fprintf (stderr, "ERROR: Failed to trace solution backwards to 0x%x at move %d\n",
-		 solution_state, i+1);
-	break;
       NEXT_MOVE:
-	;
+	if (found_move) {
+	    rrs_solution_prepend (solution, robot_found, dir_found);
+	    solution_state = state_found;
+	} else {
+	    fprintf (stderr, "ERROR: Failed to trace solution backwards to 0x%x at move %d\n",
+		     solution_state, i+1);
+	    break;
+	}
     }
 }
 




More information about the Commit mailing list