]> git.cworth.org Git - loudgame/blobdiff - lg-loa.c
lg-loa: Move current player into board_t structure.
[loudgame] / lg-loa.c
index a1949d3b8af20b640328da449c486ed82f0dd58c..ca888e8e7338e1c77c7012a89d0a5321521cf0af 100644 (file)
--- a/lg-loa.c
+++ b/lg-loa.c
@@ -64,14 +64,24 @@ typedef struct {
     int col_pieces[BOARD_SIZE];
     int diag_grave_pieces[DIAG_ARRAY_SIZE];
     int diag_acute_pieces[DIAG_ARRAY_SIZE];
+
+    player_t player;
 } board_t;
 
 typedef struct _loa_game {
     loudgame_t lg;
     board_t board;
-    player_t current;
 } loa_game_t;
 
+static void
+board_next_player (board_t *board)
+{
+    if (board->player == PLAYER_BLACK)
+       board->player = PLAYER_WHITE;
+    else
+       board->player = PLAYER_BLACK;
+}
+
 static int
 board_group_size_recursive (board_t *board, int x, int y,
                            cell_t cell,
@@ -292,6 +302,8 @@ board_init (board_t *board)
        board_add_piece (board, 0, i, CELL_WHITE);
        board_add_piece (board, BOARD_SIZE - 1, i, CELL_WHITE);
     }
+
+    board->player = PLAYER_BLACK;
 }
 
 /* A few different ideas for displaying boards:
@@ -380,16 +392,6 @@ static void
 loa_game_new_game (loa_game_t *game)
 {
     board_init (&game->board);
-    game->current = PLAYER_BLACK;
-}
-
-static void
-loa_game_next_player (loa_game_t *game)
-{
-    if (game->current == PLAYER_BLACK)
-       game->current = PLAYER_WHITE;
-    else
-       game->current = PLAYER_BLACK;
 }
 
 static loa_bool_t
@@ -405,7 +407,7 @@ loa_game_move (loa_game_t *game, const char * peer,
        return FALSE;
     }
 
-    if (board->cells[x1][y1] != game->current) {
+    if (board->cells[x1][y1] != board->player) {
        loudgame_sendf (&game->lg, peer, "Cell at (%d,%d) does not belong to current player.",
                        x1, y1);
        return FALSE;
@@ -422,7 +424,7 @@ loa_game_move (loa_game_t *game, const char * peer,
     board_remove_piece (board, x2, y2);
     board_add_piece (board, x2, y2, cell);
 
-    loa_game_next_player (game);
+    board_next_player (board);
 
     return TRUE;
 }
@@ -448,7 +450,7 @@ loa_game_handle_show (loa_game_t *game,
                                   "xmlns",
                                   "http://www.w3.org/1999/xhtml");
 
-    if (game->current == PLAYER_BLACK)
+    if (game->board.player == PLAYER_BLACK)
        lm_message_node_add_child (body, "span", "Black to move:");
     else
        lm_message_node_add_child (body, "span", "White to move:");
@@ -518,7 +520,7 @@ loa_game_handle_pass (loa_game_t *game, const char *peer)
 {
     loudgame_broadcastf (&game->lg, "%s passes", peer);
 
-    loa_game_next_player (game);
+    board_next_player (&game->board);
 }
 
 static void