]> git.cworth.org Git - loudgame/blobdiff - lg-loa.c
Use loa_move_t structure instead of two x,y pairs
[loudgame] / lg-loa.c
index 4bf623076442ea9d8c9d7e7369fb8a3226f3f355..88f6ffbf15dc11b8c6b8b39ba50d5ea6a37a1f32 100644 (file)
--- a/lg-loa.c
+++ b/lg-loa.c
@@ -37,20 +37,17 @@ typedef struct _loa_game {
 static void
 loa_game_new_game (loa_game_t *game)
 {
 static void
 loa_game_new_game (loa_game_t *game)
 {
-    loa_board_init (&game->board);
+    loa_board_reset (&game->board);
 }
 
 static loa_bool_t
 }
 
 static loa_bool_t
-loa_game_move (loa_game_t *game, const char * peer,
-              int x1, int y1, int x2, int y2)
+loa_game_move (loa_game_t *game, const char * peer, loa_move_t *move)
 {
     char *error;
 
 {
     char *error;
 
-    if (! loa_board_move (&game->board, x1, y1, x2, y2, &error)) {
-       loudgame_sendf (&game->lg, peer, "Illegal move: %c%d%c%d: %s",
-                       'a' + x1, LOA_BOARD_SIZE - y1,
-                       'a' + x2, LOA_BOARD_SIZE - y2,
-                       error);
+    if (! loa_board_move (&game->board, move, &error)) {
+       loudgame_sendf (&game->lg, peer, "Illegal move: %s: %s",
+                       loa_move_to_string (move), error);
        return FALSE;
     }
 
        return FALSE;
     }
 
@@ -114,35 +111,36 @@ loa_game_handle_show (loa_game_t *game,
 static void
 loa_game_handle_move (loa_game_t *game,
                      const char *peer,
 static void
 loa_game_handle_move (loa_game_t *game,
                      const char *peer,
-                     const char *move)
+                     const char *move_string)
 {
 {
-    char xc1, xc2;
-    int x1, y1, x2, y2;
-    int matched;
+    loa_move_t move;
 
 
-    matched = sscanf (move, " %c %d %c %d ", &xc1, &y1, &xc2, &y2);
-    if (matched != 4) {
+    if (! loa_move_init_from_string (&move, move_string)) {
        loudgame_sendf (&game->lg, peer,
        loudgame_sendf (&game->lg, peer,
-                       "Error: The 'move' command requires a move of the form 'b1d3'");
+                       "Error: The 'move' command requires a move of the form 'b1-d3'");
        return;
     }
 
        return;
     }
 
-    x1 = tolower (xc1) - 'a';
-    x2 = tolower (xc2) - 'a';
-    /* We use an upper-left origin internally. */
-    y1 = LOA_BOARD_SIZE - y1;
-    y2 = LOA_BOARD_SIZE - y2;
-    if (! loa_game_move (game, peer, x1, y1, x2, y2))
+    if (! loa_game_move (game, peer, &move))
        return;
 
        return;
 
-    loudgame_broadcastf (&game->lg, "%c%d%c%d",
-                        'a' + x1, LOA_BOARD_SIZE - y1,
-                        'a' + x2, LOA_BOARD_SIZE - y2);
+    loudgame_broadcastf (&game->lg, "%s", loa_move_to_string (&move));
 
 
-    if (loa_board_is_won (&game->board, x2, y2))
+    if (loa_board_is_won (&game->board, move.x2, move.y2))
        loudgame_broadcastf (&game->lg, "%s wins", peer);
 }
 
        loudgame_broadcastf (&game->lg, "%s wins", peer);
 }
 
+static void
+loa_game_handle_history (loa_game_t *game,
+                        const char *peer)
+{
+    int i;
+
+    for (i = 0; i < game->board.num_moves; i++)
+       loudgame_sendf (&game->lg, peer, "%s",
+                       loa_move_to_string (&game->board.moves[i]));
+}
+
 static void
 loa_game_handle_pass (loa_game_t *game, const char *peer)
 {
 static void
 loa_game_handle_pass (loa_game_t *game, const char *peer)
 {
@@ -168,6 +166,7 @@ loa_game_handle_help (loa_game_t *game, const char *peer)
                    "\tmove aNbN\tMove a piece, (eg. 'move b1d3')\n"
                    "\tpass     \t\tSkip a turn (only legal if no moves are possible)\n"
                    "\tnew      \t\tBegin a new game\n"
                    "\tmove aNbN\tMove a piece, (eg. 'move b1d3')\n"
                    "\tpass     \t\tSkip a turn (only legal if no moves are possible)\n"
                    "\tnew      \t\tBegin a new game\n"
+                   "\thistory  \t\tShow the move history of the game\n"
                    "\thelp     \t\tThis help message\n"
                    "\trules    \t\tA description of the Lines of Action rules\n"
                    "\n"
                    "\thelp     \t\tThis help message\n"
                    "\trules    \t\tA description of the Lines of Action rules\n"
                    "\n"
@@ -244,6 +243,8 @@ loa_game_handle_message (loudgame_t *lg,
        loa_game_handle_pass (game, peer);
     else if (strcmp (message, "new") == 0)
        loa_game_handle_new (game, peer);
        loa_game_handle_pass (game, peer);
     else if (strcmp (message, "new") == 0)
        loa_game_handle_new (game, peer);
+    else if (strcmp (message, "history") == 0)
+       loa_game_handle_history (game, peer);
     else if (strcmp (message, "help") == 0)
        loa_game_handle_help (game, peer);
     else if (strcmp (message, "rules") == 0)
     else if (strcmp (message, "help") == 0)
        loa_game_handle_help (game, peer);
     else if (strcmp (message, "rules") == 0)
@@ -261,6 +262,8 @@ loa_game_init (loa_game_t *game, int argc, char *argv[])
     if (err)
        return err;
 
     if (err)
        return err;
 
+    loa_board_init (&game->board);
+
     loa_game_new_game (game);
 
     return 0;
     loa_game_new_game (game);
 
     return 0;