[Commit] librr/src rr.c,1.7,1.8 rr.h,1.11,1.12 rr_board.c,1.10,1.11 rr_history.c,1.1,1.2 rrint.h,1.6,1.7

Carl Worth commit at keithp.com
Wed Jun 25 04:48:05 PDT 2003


Committed by: cworth

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

Modified Files:
	rr.c rr.h rr_board.c rr_history.c rrint.h 
Log Message:
Implemented rr_history. Standardized enum/idx order.
Implmented a few functions needed by the solver (eg. rr_board_solved)

Index: rr.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- rr.c	23 Jun 2003 06:01:24 -0000	1.7
+++ rr.c	25 Jun 2003 10:48:02 -0000	1.8
@@ -121,10 +121,10 @@
     char	*str;
     rr_robot_t	robot;
 } robots[] = {
+    { "blue",   RR_ROBOT_BLUE },
+    { "green",  RR_ROBOT_GREEN },
     { "red",    RR_ROBOT_RED },
     { "yellow", RR_ROBOT_YELLOW },
-    { "green",  RR_ROBOT_GREEN },
-    { "blue",   RR_ROBOT_BLUE },
 };
 
 rr_robot_t
@@ -197,14 +197,23 @@
     return str[0];
 }
 
+int
+rr_robot_matches_target (rr_robot_t robot, rr_target_t target)
+{
+    if (target == RR_TARGET_WHIRL && robot)
+	return 1;
+    else
+	return target & (robot << 13);
+}
+
 static struct {
     char		*str;
     rr_target_color_t   color;
 } colors[] = {
+    { "blue",   RR_TARGET_COLOR_BLUE },
+    { "green",  RR_TARGET_COLOR_GREEN },
     { "red",    RR_TARGET_COLOR_RED },
     { "yellow", RR_TARGET_COLOR_YELLOW },
-    { "green",  RR_TARGET_COLOR_GREEN },
-    { "blue",   RR_TARGET_COLOR_BLUE },
     { "whirl",  RR_TARGET_COLOR_WHIRL },
 };
 #define RR_NUM_COLORS	(sizeof (colors) / sizeof (colors[0]))

Index: rr.h
===================================================================
RCS file: /local/src/CVS/librr/src/rr.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- rr.h	23 Jun 2003 06:01:24 -0000	1.11
+++ rr.h	25 Jun 2003 10:48:02 -0000	1.12
@@ -35,6 +35,7 @@
     RR_STATUS_SUCCESS = 0,
     RR_STATUS_NO_MEMORY,
     RR_STATUS_ROBOT_NOT_FOUND,
+    RR_STATUS_TARGET_NOT_FOUND,
     RR_STATUS_OCCUPIED,
     RR_STATUS_HISTORY_EMPTY,
     RR_STATUS_PARSE_ERROR,
@@ -146,11 +147,11 @@
     RR_DIRECTION_LEFT,
     RR_DIRECTION_WEST = RR_DIRECTION_LEFT,
 
+    RR_DIRECTION_DOWN,
+    RR_DIRECTION_SOUTH = RR_DIRECTION_DOWN,
+
     RR_DIRECTION_RIGHT,
     RR_DIRECTION_EAST = RR_DIRECTION_RIGHT,
-
-    RR_DIRECTION_DOWN,
-    RR_DIRECTION_SOUTH = RR_DIRECTION_DOWN
 } rr_direction_t;
 
 typedef enum {
@@ -235,7 +236,7 @@
 
     } u;
 } rr_notice_t;
-    
+
 /* rr_board.c */
 
 rr_board_t *
@@ -259,7 +260,7 @@
 
 rr_status_t
 rr_board_add_robot (rr_board_t *board, int x, int y,
-		    rr_robot_t robot, int is_goal_robot);
+		    rr_robot_t robot);
 
 rr_status_t
 rr_board_add_target (rr_board_t *board, int x, int y,
@@ -275,10 +276,8 @@
 		     rr_robot_t robot, int *x, int *y);
 
 rr_status_t
-rr_board_set_goal_robot (rr_board_t *board, rr_robot_t robot);
-
-rr_robot_t
-rr_board_get_goal_robot (rr_board_t *board);
+rr_board_find_target (rr_board_t *board,
+		      rr_target_t target, int *x, int *y);
 
 rr_status_t
 rr_board_set_goal_target (rr_board_t *board,
@@ -287,6 +286,9 @@
 rr_target_t
 rr_board_get_goal_target (rr_board_t *board);
 
+int
+rr_board_solved (rr_board_t *board);
+
 /* rr_board_move: Move the given robot the given direction.
 
    Possible errors:
@@ -307,7 +309,7 @@
 
 /* rr_board_position: Reposition a robot */
 rr_status_t
-rr_board_position (rr_board_t *board, rr_robot_t robot, int x, int y);
+rr_board_position_robot (rr_board_t *board, rr_robot_t robot, int x, int y);
 
 /* rr_board_to_str:  Return a string containing a diagram of the board.
    
@@ -473,6 +475,9 @@
 
 char *
 rr_robot_str (rr_robot_t robot);
+
+int
+rr_robot_matches_target (rr_robot_t robot, rr_target_t target);
 
 char *
 rr_target_color_str (rr_target_color_t color);

Index: rr_board.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr_board.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- rr_board.c	19 Jun 2003 11:39:15 -0000	1.10
+++ rr_board.c	25 Jun 2003 10:48:02 -0000	1.11
@@ -84,8 +84,9 @@
     board->width = width;
     board->height = height;
 
-    board->goal_robot = RR_ROBOT_NONE;
     board->goal_target = RR_TARGET_NONE;
+    board->goal_x = 0;
+    board->goal_y = 0;
 
     status = _rr_history_init (&board->history);
     if (status)
@@ -210,7 +211,7 @@
 
 rr_status_t
 rr_board_add_robot (rr_board_t *board, int x, int y,
-		    rr_robot_t robot, int is_goal_robot)
+		    rr_robot_t robot)
 {
     rr_cell_t *cell = _rr_board_get_cell_ptr (board, x, y);
 
@@ -219,9 +220,6 @@
 
     RR_CELL_ADD_ROBOT (*cell, robot);
 
-    if (is_goal_robot)
-	board->goal_robot = robot;
-
     return RR_STATUS_SUCCESS;
 }
 
@@ -236,8 +234,11 @@
 
     RR_CELL_ADD_TARGET (*cell, target);
 
-    if (is_goal_target)
+    if (is_goal_target) {
 	board->goal_target = target;
+	board->goal_x = x;
+	board->goal_y = y;
+    }
 
     return RR_STATUS_SUCCESS;
 }
@@ -252,7 +253,7 @@
     cell = board->cell;
     for (j=0; j < board->height; j++) {
 	for (i=0; i < board->height; i++) {
-	    if (RR_CELL_GET_ROBOT (*cell) == robot) {
+	    if (RR_CELL_GET_ROBOT (*cell) & robot) {
 		*x = i;
 		*y = j;
 		return RR_STATUS_SUCCESS;
@@ -265,16 +266,25 @@
 }
 
 rr_status_t
-rr_board_set_goal_robot (rr_board_t *board, rr_robot_t robot)
+rr_board_find_target (rr_board_t *board,
+		      rr_target_t target, int *x, int *y)
 {
-    board->goal_robot = robot;
-    return RR_STATUS_SUCCESS;
-}
+    rr_cell_t *cell;
+    int i, j;
 
-rr_robot_t
-rr_board_get_goal_robot (rr_board_t *board)
-{
-    return board->goal_robot;
+    cell = board->cell;
+    for (j=0; j < board->height; j++) {
+	for (i=0; i < board->height; i++) {
+	    if (RR_CELL_GET_TARGET (*cell) == target) {
+		*x = i;
+		*y = j;
+		return RR_STATUS_SUCCESS;
+	    }
+	    cell++;
+	}
+    }
+
+    return RR_STATUS_TARGET_NOT_FOUND;
 }
 
 rr_status_t
@@ -282,6 +292,8 @@
 			  rr_target_t target)
 {
     board->goal_target = target;
+    rr_board_find_target (board, target, &board->goal_x, &board->goal_y);
+
     return RR_STATUS_SUCCESS;
 }
 
@@ -291,6 +303,19 @@
     return board->goal_target;
 }
 
+int
+rr_board_solved (rr_board_t *board)
+{
+    rr_cell_t cell;
+    rr_robot_t robot;
+
+    cell = rr_board_get_cell (board, board->goal_x, board->goal_y);
+    
+    robot = RR_CELL_GET_ROBOT (cell);
+
+    return rr_robot_matches_target (robot, board->goal_target);
+}
+
 rr_status_t
 rr_board_move (rr_board_t *board, rr_robot_t robot, rr_direction_t dir)
 {
@@ -331,7 +356,7 @@
     if (status)
 	return status;
 
-    RR_CELL_REMOVE_ROBOT (*cell);
+    RR_CELL_REMOVE_ROBOT (*cell, robot);
     do
 	cell += delta;
     while (RR_CELL_HAS_WALL (*cell, wall) == 0
@@ -360,7 +385,7 @@
 	return status;
 
     cell = _rr_board_get_cell_ptr (board, x, y);
-    RR_CELL_REMOVE_ROBOT (*cell);
+    RR_CELL_REMOVE_ROBOT (*cell, robot);
     cell = _rr_board_get_cell_ptr (board, prev_x, prev_y);
     RR_CELL_ADD_ROBOT (*cell, robot);
 
@@ -384,22 +409,24 @@
 }
 
 rr_status_t
-rr_board_position (rr_board_t *board, rr_robot_t robot, int x, int y)
+rr_board_position_robot (rr_board_t *board, rr_robot_t robot, int x, int y)
 {
     rr_status_t	status;
     rr_cell_t	*ocell, *cell;
     int		ox, oy;
 
     status = rr_board_find_robot (board, robot, &ox, &oy);
-    if (status != RR_STATUS_SUCCESS)
-	return status;
-    ocell = _rr_board_get_cell_ptr (board, ox, oy);
+    if (status == RR_STATUS_SUCCESS) {
+	ocell = _rr_board_get_cell_ptr (board, ox, oy);
+	RR_CELL_REMOVE_ROBOT (*ocell, robot);
+    }
+
     cell = _rr_board_get_cell_ptr (board, x, y);
-    RR_CELL_REMOVE_ROBOT (*ocell);
     RR_CELL_ADD_ROBOT (*cell, robot);
+
     return RR_STATUS_SUCCESS;
 }
-		    
+
 static rr_status_t
 _rr_board_get_robot_char (rr_board_t *board, rr_cell_t cell,
 			  char *r)
@@ -409,7 +436,7 @@
     robot = RR_CELL_GET_ROBOT (cell);
     *r = _rr_robot_symbol (robot);
 
-    if (robot == board->goal_robot)
+    if (rr_robot_matches_target (robot, board->goal_target))
 	*r = toupper (*r);
 
     return RR_STATUS_SUCCESS;
@@ -536,7 +563,7 @@
 		return RR_STATUS_PARSE_ERROR;
 	    c = NEXT;
 	    robot = _rr_robot_parse_symbol (c);
-	    rr_board_add_robot (board, i, j, robot, isupper (c));
+	    rr_board_add_robot (board, i, j, robot);
 	    c = NEXT;
 	    d = NEXT;
 	    target = _rr_target_parse_symbols (c, d);

Index: rr_history.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr_history.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- rr_history.c	1 Jun 2003 04:27:55 -0000	1.1
+++ rr_history.c	25 Jun 2003 10:48:02 -0000	1.2
@@ -29,21 +29,45 @@
 rr_status_t
 _rr_history_init (rr_history_t *history)
 {
-    /* XXX: NYI */
+    history->position = NULL;
+    history->size = 0;
+    history->num_positions = 0;
+
     return RR_STATUS_SUCCESS;
 }
 
 void
 _rr_history_fini (rr_history_t *history)
 {
-    /* XXX: NYI */
+    free (history->position);
+    history->position = NULL;
+    history->size = 0;
+    history->num_positions = 0;
 }
 
+#define RR_HISTORY_GROWTH 5
 rr_status_t
 _rr_history_push (rr_history_t *history,
 		  rr_robot_t robot, int x, int y)
 {
-    /* XXX: NYI */
+    rr_position_t *position;
+
+    if (history->num_positions >= history->size) {
+	rr_position_t *new_position;
+	history->size += RR_HISTORY_GROWTH;
+	new_position = realloc (history->position, history->size * sizeof (rr_position_t));
+	if (new_position == NULL) {
+	    history->size -= RR_HISTORY_GROWTH;
+	    return RR_STATUS_NO_MEMORY;
+	}
+	history->position = new_position;
+    }
+
+    position = &history->position[history->num_positions++];
+    position->robot = robot;
+    position->x = x;
+    position->y = y;
+
     return RR_STATUS_SUCCESS;
 }
 
@@ -51,6 +75,16 @@
 _rr_history_pop (rr_history_t *history,
 		 rr_robot_t *robot, int *x, int *y)
 {
-    /* XXX: NYI */
+    rr_position_t *position;
+
+    if (history->num_positions < 1)
+	return RR_STATUS_HISTORY_EMPTY;
+
+    position = &history->position[--history->num_positions];
+
+    *robot = position->robot;
+    *x = position->x;
+    *y = position->y;
+
     return RR_STATUS_SUCCESS;
 }

Index: rrint.h
===================================================================
RCS file: /local/src/CVS/librr/src/rrint.h,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- rrint.h	19 Jun 2003 11:39:15 -0000	1.6
+++ rrint.h	25 Jun 2003 10:48:02 -0000	1.7
@@ -37,14 +37,22 @@
 
 #define RR_CELL_INIT(cell)			((cell) = 0)
 #define RR_CELL_ADD_ROBOT(cell, robot)		((cell) |= (robot))
-#define RR_CELL_REMOVE_ROBOT(cell)		((cell) &= ~RR_ROBOT_ANY)
+#define RR_CELL_REMOVE_ROBOT(cell,robot)	((cell) &= ~(robot))
 #define RR_CELL_ADD_TARGET(cell, target)	((cell) |= (target))
 #define RR_CELL_REMOVE_TARGET(cell)		((cell) &= ~RR_TARGET_ANY)
 #define RR_CELL_ADD_WALL(cell, wall)		((cell) |= (wall))
 #define RR_CELL_REMOVE_WALL(cell, wall)		((cell) &= ~(wall))
 
+typedef struct rr_position {
+    rr_robot_t robot;
+    int x, y;
+} rr_position_t;
+
 typedef struct rr_history {
-    /* XXX: NYI */
+    rr_position_t *position;
+    int num_positions;
+
+    int size;
 } rr_history_t;
 
 struct rr_board {
@@ -53,8 +61,8 @@
 
     rr_cell_t *cell;
 
-    rr_robot_t goal_robot;
     rr_target_t goal_target;
+    int goal_x, goal_y;
 
     rr_history_t history;
 




More information about the Commit mailing list