]> git.cworth.org Git - loudgame/commitdiff
Export and document loa_board_move_is_legal
authorCarl Worth <cworth@cworth.org>
Mon, 3 Mar 2008 19:52:31 +0000 (11:52 -0800)
committerCarl Worth <cworth@cworth.org>
Mon, 3 Mar 2008 19:52:31 +0000 (11:52 -0800)
loa-board.c
loa-board.h

index 9478731e29cbb9455dd9da7f492239e5e9f052f5..e630519cbfd81d7406c94e24892e019f0f62e8d7 100644 (file)
@@ -71,11 +71,9 @@ loa_move_init_from_string (loa_move_t *move, const char *string)
     move->y2 = 0;
     move->is_capture = 0;
 
-    matched = sscanf (string, " %c%d%c%c%d", &xc1, &y1, &sep, &xc2, &y2);
-    if (matched != 5) {
-       printf ("Matched only %d fields of %s\n", matched, string);
+    matched = sscanf (string, "%c%d%c%c%d", &xc1, &y1, &sep, &xc2, &y2);
+    if (matched != 5)
        return FALSE;
-    }
 
     x1 = tolower (xc1) - 'a';
     x2 = tolower (xc2) - 'a';
@@ -276,10 +274,10 @@ loa_board_is_won (loa_board_t *board, int x, int y)
     return 0;
 }
 
-static loa_bool_t
-loa_board_move_legal (loa_board_t       *board,
-                     const loa_move_t   *move,
-                     char              **error)
+loa_bool_t
+loa_board_move_is_legal (loa_board_t    *board,
+                        loa_move_t      *move,
+                        char           **error)
 {
     int x, y;
     int x1, y1, x2, y2;
@@ -312,6 +310,11 @@ loa_board_move_legal (loa_board_t   *board,
        return FALSE;
     }
 
+    if (board->cells[x2][y2] == LOA_CELL_EMPTY)
+       move->is_capture = FALSE;
+    else
+       move->is_capture = TRUE;
+
     dx = x2 - x1;
     dy = y2 - y1;
 
@@ -420,19 +423,15 @@ loa_board_move (loa_board_t *board,
 {
     loa_cell_t cell;
 
-    if (! loa_board_move_legal (board, move, error))
+    if (! loa_board_move_is_legal (board, move, error))
        return FALSE;
 
     cell = loa_board_remove_piece (board, move->x1, move->y1);
     assert (cell == board->player);
 
     cell = loa_board_remove_piece (board, move->x2, move->y2);
-    if (cell == LOA_CELL_EMPTY) {
-       move->is_capture = FALSE;
-    } else {
-       assert (cell != board->player);
-       move->is_capture = TRUE;
-    }
+    assert (cell == LOA_CELL_EMPTY ||
+           (move->is_capture && cell != board->player));
 
     loa_board_add_piece (board,
                         move->x2, move->y2,
index da48c7238e72cbf90ed1cfb7617ceeeaa35bd362..9c3c466a0ef321685eaf470f7c4d8ae21ea0e592 100644 (file)
@@ -107,16 +107,31 @@ loa_board_reset (loa_board_t *board);
 int
 loa_board_is_won (loa_board_t *board, int x, int y);
 
-/* Move a piece from (x1,y1) to (x2,y2) where (0,0) is at the
- * upper-left corner of the board. Returns TRUE if the move is legal
- * and is performed. If the move is not legal this function returns
- * FALSE, no change will be performed on the board, and *error will be
- * set to a string describing why the move is illegal.*/
-int
+/* Move a piece from (move->x1,move->y1) to (move->x2,move->y2) where
+ * (0,0) is at the upper-left corner of the board. Returns TRUE if the
+ * move is legal and is performed. If the move is not legal this
+ * function returns FALSE, no change will be performed on the board,
+ * and *error will be set to a string describing why the move is
+ * illegal.
+ *
+ * After this function returns, the move->is_capture field will be set
+ * as appropriate whether the move is a capture or not.
+ */
+loa_bool_t
 loa_board_move (loa_board_t *board,
                loa_move_t *move,
                char **error);
 
+/* Check whether a move is legal, but without performing the move.
+ *
+ * After this function returns, the move->is_capture field will be set
+ * as appropriate whether the move is a capture or not.
+ */
+loa_bool_t
+loa_board_move_is_legal (loa_board_t    *board,
+                        loa_move_t      *move,
+                        char           **error);
+
 /* Execute a 'pass'---changing the player-to-move from the current
  * player to the opponent. Returns TRUE if the pass is executed.  Will
  * eventually return FALSE if the current player has a legal move that