]> git.cworth.org Git - loudgame/commitdiff
Implement simple show, hint, and shuffle commands for lg-set.
authorCarl Worth <cworth@cworth.org>
Tue, 15 Jan 2008 15:13:02 +0000 (07:13 -0800)
committerCarl Worth <cworth@cworth.org>
Tue, 15 Jan 2008 15:13:02 +0000 (07:13 -0800)
It's very close to playable now!

lg-set.c
loudgame.c
loudgame.h

index 145120092f20a91cb8d8aead43e9e1313861b5f0..af51bd864a3857365e12cc1a5db6bd229dedf4ad 100644 (file)
--- a/lg-set.c
+++ b/lg-set.c
@@ -17,6 +17,8 @@
  * Author: Carl Worth <cworth@cworth.org>
  */
 
+#include "loudgame.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -75,20 +77,20 @@ typedef struct board {
     slot_t slots[BOARD_MAX_SLOTS];
     int needs_deal;
     int sets_possible;
-    int display_sets_possible;
 } board_t;
 
-typedef struct game {
+typedef struct _set_game {
+    loudgame_t lg;
     deck_t deck;
     board_t board;
-} game_t;
-
-static void game_init (game_t *game);
-static void game_fini (game_t *game);
+} set_game_t;
 
 static void
 board_count_sets_possible (board_t *board);
 
+static int
+set_game_shuffle (set_game_t *game);
+
 static int
 attribute_all_same (card_t *cards, int num_cards, int attr)
 {
@@ -253,27 +255,6 @@ board_init (board_t *board)
     board_count_sets_possible (board);
 }
 
-static void
-board_print (board_t *board)
-{
-    int i, j;
-
-    printf ("Sets possible: %d\n", board->sets_possible);
-
-    for  (i = 0; i < board->num_slots; i++) {
-       if (! board->slots[i].has_card)
-           for (j = 0; j < NUM_ATTRIBUTES; j++)
-               printf (" ");
-       else
-           for (j = 0; j < NUM_ATTRIBUTES; j++)
-               printf ("%d", board->slots[i].card.attributes[j]);
-       if ((i + 1) % 3 == 0)
-           printf ("\n");
-       else
-           printf (" ");
-    }
-}
-
 static void
 deal (deck_t *deck, board_t *board)
 {
@@ -292,31 +273,91 @@ deal (deck_t *deck, board_t *board)
     board->needs_deal = 0;
 }
 
-/* Begin a new game */
 static void
-game_init (game_t *game)
+set_game_handle_show (set_game_t       *game,
+                     const char        *peer)
 {
-    deck_init (&game->deck);
-    board_init (&game->board);
-    deal (&game->deck, &game->board);
+    board_t *board = &game->board;
+    char board_str[BOARD_MAX_SLOTS * (NUM_ATTRIBUTES + 1)];
+    char *s;
+    int i, j;
+
+    s = board_str;
+    for (i = 0; i < board->num_slots; i++)
+       if (board->slots[i].has_card) {
+           for (j = 0; j < NUM_ATTRIBUTES; j++)
+               *s++ = '0' + board->slots[i].card.attributes[j];
+           *s++ = ' ';
+       }
+    *s = '\0';
+
+    loudgame_send (&game->lg, peer, board_str);
 }
 
 static void
-game_fini (game_t *game)
+set_game_handle_hint (set_game_t *game,
+                     const char *peer)
 {
-    /* Nothing to do. */
+    loudgame_sendf (&game->lg, peer, "Sets possible: %d",
+                   game->board.sets_possible);
+}
+
+static void
+set_game_handle_shuffle (set_game_t *game,
+                        const char *peer)
+{
+    if (game->board.sets_possible) {
+       loudgame_sendf (&game->lg, peer,
+                       "There are %d sets, refusing to shuffle.",
+                       game->board.sets_possible);
+       return;
+    }
+
+    set_game_shuffle (game);
+}
+
+static void
+set_game_handle_message (loudgame_t *lg,
+                        const char *peer,
+                        const char *message)
+{
+    set_game_t *game = (set_game_t*) lg;
+
+    if (strcmp (message, "show") == 0)
+       set_game_handle_show (game, peer);
+    else if (strcmp (message, "hint") == 0)
+       set_game_handle_hint (game, peer);
+    else if (strcmp (message, "shuffle") == 0)
+       set_game_handle_shuffle (game, peer);
+    else
+       loudgame_sendf (lg, peer, "Unknown command: '%s'", message);
+}
+
+/* Begin a new game */
+static int
+set_game_init (set_game_t *game, int argc, char *argv[])
+{
+    int err;
+
+    err = loudgame_init (&game->lg, argc, argv);
+    if (err)
+       return err;
+
+    deck_init (&game->deck);
+    board_init (&game->board);
+    deal (&game->deck, &game->board);
+
+    return 0;
 }
 
 /* Return the dealt cards to the deck, reshuffle, and deal again. */
 static int
-reshuffle (deck_t *deck, board_t *board)
+set_game_shuffle (set_game_t *game)
 {
+    board_t *board = &game->board;
+    deck_t *deck = &game->deck;
     int i;
 
-    if (board->sets_possible) {
-       return 0;
-    }
-
     for (i=0; i < board->num_slots; i++) {
        if (board->slots[i].has_card) {
            deck->cards[++deck->num_cards - 1] = board->slots[i].card;
@@ -332,17 +373,22 @@ reshuffle (deck_t *deck, board_t *board)
 }
 
 int
-main (void)
+main (int argc, char *argv[])
 {
-    game_t game;
+    set_game_t game;
+    int err;
 
     srand (time(0));
 
-    game_init (&game);
+    err = set_game_init (&game, argc, argv);
+    if (err)
+       return err;
 
-    board_print (&game.board);
+    game.lg.handle_message = set_game_handle_message;
 
-    game_fini (&game);
+    err = loudgame_run (&game.lg);
+    if (err)
+       return err;
 
     return 0;
 }
index e2f0af2d5b751968725dfdd427b2e08b93e8f059..2cb9a1869f62e16160909a4fad2add98524abb7e 100644 (file)
@@ -93,6 +93,36 @@ loudgame_send (loudgame_t    *lg,
     }
 }
 
+void
+loudgame_vsendf (loudgame_t    *lg,
+                const char     *peer,
+                const char     *format,
+                va_list         va)
+{
+    char *str;
+
+    str = g_strdup_vprintf (format, va);
+
+    loudgame_send (lg, peer, str);
+
+    free (str);
+}
+
+void
+loudgame_sendf (loudgame_t     *lg,
+               const char      *peer,
+               const char      *format,
+               ...)
+{
+    va_list va;
+
+    va_start (va, format);
+
+    loudgame_vsendf (lg, peer, format, va);
+
+    va_end (va);
+}
+
 static void
 handle_command (LmConnection   *connection,
                const char      *peer,
index a65f5dd6aa38fce3dc546bd3d3af0e6710edf355..11e74df5fde3456b23858b21229797de8aa2f94b 100644 (file)
@@ -20,6 +20,7 @@
 #ifndef __LOUDGAME_H__
 #define __LOUDGAME_H__
 
+#include <stdarg.h>
 #include <loudmouth/loudmouth.h>
 
 typedef struct _loudgame loudgame_t;
@@ -51,6 +52,18 @@ loudgame_send (loudgame_t    *lg,
               const char       *peer,
               const char       *message);
 
+void
+loudgame_sendf (loudgame_t     *lg,
+               const char      *peer,
+               const char      *format,
+               ...);
+
+void
+loudgame_vsendf (loudgame_t    *lg,
+                const char     *peer,
+                const char     *format,
+                va_list         va);
+
 void
 loudgame_quit (loudgame_t *lg, int return_value);