From f37ff234d97a4fb8532f8c9387fb7fff5780afaf Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Mon, 3 Mar 2008 11:51:24 -0800 Subject: [PATCH] Use loa_move_t structure instead of two x,y pairs --- lg-loa.c | 35 +++++++++++------------------------ loa-board.c | 31 ++++++++++++++----------------- loa-board.h | 3 +-- 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/lg-loa.c b/lg-loa.c index 1749143..88f6ffb 100644 --- a/lg-loa.c +++ b/lg-loa.c @@ -41,16 +41,13 @@ loa_game_new_game (loa_game_t *game) } 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; - 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; } @@ -114,32 +111,22 @@ loa_game_handle_show (loa_game_t *game, 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, - "Error: The 'move' command requires a move of the form 'b1d3'"); + "Error: The 'move' command requires a move of the form 'b1-d3'"); 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; - 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); } diff --git a/loa-board.c b/loa-board.c index 21f4302..9478731 100644 --- a/loa-board.c +++ b/loa-board.c @@ -71,9 +71,11 @@ 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) + 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); return FALSE; + } x1 = tolower (xc1) - 'a'; x2 = tolower (xc2) - 'a'; @@ -413,35 +415,30 @@ loa_board_add_move_to_history (loa_board_t *board, int loa_board_move (loa_board_t *board, - int x1, int y1, - int x2, int y2, + loa_move_t *move, char **error) { loa_cell_t cell; - loa_move_t move; - - move.x1 = x1; - move.y1 = y1; - move.x2 = x2; - move.y2 = y2; - if (! loa_board_move_legal (board, &move, error)) + if (! loa_board_move_legal (board, move, error)) return FALSE; - cell = loa_board_remove_piece (board, x1, y1); + cell = loa_board_remove_piece (board, move->x1, move->y1); assert (cell == board->player); - cell = loa_board_remove_piece (board, x2, y2); + cell = loa_board_remove_piece (board, move->x2, move->y2); if (cell == LOA_CELL_EMPTY) { - move.is_capture = FALSE; + move->is_capture = FALSE; } else { assert (cell != board->player); - move.is_capture = TRUE; + move->is_capture = TRUE; } - loa_board_add_piece (board, x2, y2, board->player); + loa_board_add_piece (board, + move->x2, move->y2, + board->player); - loa_board_add_move_to_history (board, &move); + loa_board_add_move_to_history (board, move); loa_board_next_player (board); diff --git a/loa-board.h b/loa-board.h index 4a4f690..da48c72 100644 --- a/loa-board.h +++ b/loa-board.h @@ -114,8 +114,7 @@ loa_board_is_won (loa_board_t *board, int x, int y); * set to a string describing why the move is illegal.*/ int loa_board_move (loa_board_t *board, - int x1, int y1, - int x2, int y2, + loa_move_t *move, char **error); /* Execute a 'pass'---changing the player-to-move from the current -- 2.43.0