[Commit] librr/src rr.h, 1.15, 1.16 rr_board.c, 1.13,
1.14 rr_notice.c, 1.3, 1.4
Carl Worth
commit at keithp.com
Thu Jul 10 19:01:46 PDT 2003
Committed by: cworth
Update of /local/src/CVS/librr/src
In directory home.keithp.com:/tmp/cvs-serv11536/src
Modified Files:
rr.h rr_board.c rr_notice.c
Log Message:
Added RR_NOTICE_BOARD. API cleanup
Index: rr.h
===================================================================
RCS file: /local/src/CVS/librr/src/rr.h,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- rr.h 3 Jul 2003 14:07:13 -0000 1.15
+++ rr.h 11 Jul 2003 01:01:44 -0000 1.16
@@ -162,6 +162,7 @@
RR_NOTICE_DISPOSE,
RR_NOTICE_MESSAGE,
/* game notices */
+ RR_NOTICE_BOARD,
RR_NOTICE_GAMESTATE,
RR_NOTICE_TURN,
RR_NOTICE_GAMEOVER,
@@ -197,7 +198,7 @@
union {
/*
- * USER, QUIT, JOIN, WATCH, PART, REVOKE,
+ * USER, QUIT, BOARD, JOIN, WATCH, PART, REVOKE,
* ABANDON, NOBID, GAME, DISPOSE, GAMEOVER
*/
char *string;
@@ -261,15 +262,21 @@
rr_status_t
rr_board_add_wall (rr_board_t *board,
- int x, int y, rr_wall_t wall);
+ rr_wall_t wall,
+ int x, int y);
rr_status_t
-rr_board_add_robot (rr_board_t *board, int x, int y,
- rr_robot_t robot);
+rr_board_add_robot (rr_board_t *board,
+ rr_robot_t robot,
+ int x, int y);
rr_status_t
-rr_board_add_target (rr_board_t *board, int x, int y,
- rr_target_t target, int is_goal_target);
+rr_board_remove_robot (rr_board_t *board, rr_robot_t robot);
+
+rr_status_t
+rr_board_add_target (rr_board_t *board,
+ rr_target_t target, int is_goal_target,
+ int x, int y);
/* rr_board_find_robot: Find X/Y location for given robot.
@@ -311,10 +318,6 @@
rr_status_t
rr_board_reset (rr_board_t *board);
-
-/* rr_board_position: Reposition a robot */
-rr_status_t
-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.
Index: rr_board.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr_board.c,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- rr_board.c 3 Jul 2003 18:21:20 -0000 1.13
+++ rr_board.c 11 Jul 2003 01:01:44 -0000 1.14
@@ -95,6 +95,7 @@
diagram = _rr_board_read_diagram (file);
if (diagram == NULL) {
fprintf (stderr, "Did not find a valid board diagram in %s.\n", filename);
+ fclose (file);
return NULL;
}
fclose (file);
@@ -231,7 +232,8 @@
rr_status_t
rr_board_add_wall (rr_board_t *board,
- int x, int y, rr_wall_t wall)
+ rr_wall_t wall,
+ int x, int y)
{
rr_cell_t *cell = _rr_board_get_cell_ptr (board, x, y);
@@ -263,22 +265,45 @@
}
rr_status_t
-rr_board_add_robot (rr_board_t *board, int x, int y,
- rr_robot_t robot)
+rr_board_add_robot (rr_board_t *board,
+ rr_robot_t robot,
+ int x, int y)
{
- rr_cell_t *cell = _rr_board_get_cell_ptr (board, x, y);
+ rr_cell_t *cell;
+
+ cell = _rr_board_get_cell_ptr (board, x, y);
if (RR_CELL_GET_ROBOT (*cell))
return RR_STATUS_OCCUPIED;
+ rr_board_remove_robot (board, robot);
+
RR_CELL_ADD_ROBOT (*cell, robot);
return RR_STATUS_SUCCESS;
}
rr_status_t
-rr_board_add_target (rr_board_t *board, int x, int y,
- rr_target_t target, int is_goal_target)
+rr_board_remove_robot (rr_board_t *board, rr_robot_t robot)
+{
+ rr_status_t status;
+ rr_cell_t *cell;
+ int x, y;
+
+ status = rr_board_find_robot (board, robot, &x, &y);
+ if (status)
+ return RR_STATUS_SUCCESS;
+
+ cell = _rr_board_get_cell_ptr (board, x, y);
+ RR_CELL_REMOVE_ROBOT (*cell, robot);
+
+ return RR_STATUS_SUCCESS;
+}
+
+rr_status_t
+rr_board_add_target (rr_board_t *board,
+ rr_target_t target, int is_goal_target,
+ int x, int y)
{
rr_cell_t *cell = _rr_board_get_cell_ptr (board, x, y);
@@ -305,7 +330,7 @@
cell = board->cell;
for (j=0; j < board->height; j++) {
- for (i=0; i < board->height; i++) {
+ for (i=0; i < board->width; i++) {
if (RR_CELL_GET_ROBOT (*cell) & robot) {
*x = i;
*y = j;
@@ -461,25 +486,6 @@
}
}
-rr_status_t
-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) {
- 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_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)
@@ -576,23 +582,27 @@
rr_robot_t robot;
rr_target_t target;
-#define NEXT *s; if (*s == '\0') return RR_STATUS_PARSE_ERROR; s++
-#define EXPECT(e) c = NEXT; if (c != (e)) return RR_STATUS_PARSE_ERROR
+#define NEXT *s; if (*s == '\0') { fprintf (stderr, "Unexpected end of file\n"); return RR_STATUS_PARSE_ERROR; } s++
+#define EXPECT(ex) c = NEXT; if (c != (ex)) { fprintf (stderr, "Unexpected character: '%c' (expected '%c')\n", c, ex); return RR_STATUS_PARSE_ERROR; }
_rr_board_fini (board);
s = str;
- if (s == NULL)
+ if (s == NULL) {
+ fprintf (stderr, "Cannot parse a null string.\n");
return RR_STATUS_PARSE_ERROR;
+ }
/* Consume optional initial newline */
if (*s == '\n')
s++;
n = strchr (s, '\n');
- if (n == NULL)
+ if (n == NULL) {
+ fprintf (stderr, "String contains no newline characters.\n");
return RR_STATUS_PARSE_ERROR;
+ }
board->width = ((n - s) - 1) / 4;
board->height = ((strlen (s) + 1) / (board->width * 4 + 2) - 1) / 2;
_rr_board_init (board, board->width, board->height);
@@ -601,10 +611,12 @@
for (i=0; i < board->width; i++) {
EXPECT (' ');
c = NEXT;
- if (c == '=')
- rr_board_add_wall (board, i, j, RR_WALL_ABOVE);
- else if (c != ' ')
+ if (c == '=') {
+ rr_board_add_wall (board, RR_WALL_ABOVE, i, j);
+ } else if (c != ' ') {
+ fprintf (stderr, "Unexpected character: '%c' (expected '=' or ' )\n", c);
return RR_STATUS_PARSE_ERROR;
+ }
EXPECT (c);
EXPECT (c);
}
@@ -616,20 +628,22 @@
} else {
c = NEXT;
}
- if (c == '|')
- rr_board_add_wall (board, i, j, RR_WALL_LEFT);
- else if (c != ' ')
+ if (c == '|') {
+ rr_board_add_wall (board, RR_WALL_LEFT, i, j);
+ } else if (c != ' ') {
+ fprintf (stderr, "Unexpected character: '%c' (expected '|' or ' ')\n", c);
return RR_STATUS_PARSE_ERROR;
+ }
c = NEXT;
robot = _rr_robot_parse_symbol (c);
- rr_board_add_robot (board, i, j, robot);
+ rr_board_add_robot (board, robot, i, j);
c = NEXT;
d = NEXT;
target = _rr_target_parse_symbols (c, d);
- rr_board_add_target (board, i, j, target, isupper (c));
+ rr_board_add_target (board, target, isupper (c), i, j);
}
EXPECT ('|');
- rr_board_add_wall (board, board->width - 1, j, RR_WALL_RIGHT);
+ rr_board_add_wall (board, RR_WALL_RIGHT, board->width - 1, j);
EXPECT ('\n');
}
for (i=0; i < board->width; i++) {
@@ -637,7 +651,7 @@
EXPECT ('=');
EXPECT ('=');
EXPECT ('=');
- rr_board_add_wall (board, i, board->height - 1, RR_WALL_BELOW);
+ rr_board_add_wall (board, RR_WALL_BELOW, i, board->height - 1);
}
EXPECT(' ');
Index: rr_notice.c
===================================================================
RCS file: /local/src/CVS/librr/src/rr_notice.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- rr_notice.c 26 Jun 2003 01:18:34 -0000 1.3
+++ rr_notice.c 11 Jul 2003 01:01:44 -0000 1.4
@@ -172,6 +172,7 @@
{ "DISPOSE", RR_NOTICE_DISPOSE, _rr_notice_string },
{ "MESSAGE", RR_NOTICE_MESSAGE, _rr_notice_message },
/* game notices */
+ { "BOARD", RR_NOTICE_BOARD, _rr_notice_string },
{ "GAMESTATE", RR_NOTICE_GAMESTATE, _rr_notice_gamestate },
{ "TURN", RR_NOTICE_TURN, _rr_notice_target },
{ "GAMEOVER", RR_NOTICE_GAMEOVER, _rr_notice_void },
More information about the Commit
mailing list