]> git.cworth.org Git - kub/blobdiff - kub.c
Realized value for TILE_SUN_Y_OFFSET was missing from last commit
[kub] / kub.c
diff --git a/kub.c b/kub.c
index aec86bee557348ae318c7614bf8a7fa6dc6a9889..0efcbae7428845b34d7cbd1e56d4b1d26ae7ad75 100644 (file)
--- a/kub.c
+++ b/kub.c
@@ -2,6 +2,11 @@
 #include <stdlib.h>
 #include <time.h>
 #include <gtk/gtk.h>
+#include <librsvg/rsvg.h>
+#include <librsvg/rsvg-cairo.h>
+
+#define FATAL_ERROR(msg) \
+    do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0)
 
 char *colors[] = {"Black", "Blue", "Red", "Yellow"};
 
@@ -46,6 +51,7 @@ typedef struct game {
     int num_players;
     board_t board;
     deck_t deck;
+    RsvgHandle *blanktile;
 } game_t;
 
 static void card_print(card_t card)
@@ -53,6 +59,34 @@ static void card_print(card_t card)
     printf("%6s %2d\n", colors[card.color], card.number + 1);
 }
 
+static void card_draw(game_t *game, card_t *card, cairo_t *cr, int x, int y)
+{
+    char number_string[3];
+    int len;
+
+    len = snprintf (number_string, 3, "%d", card->number + 1);
+    if (len < 0 || len >= 3)
+       FATAL_ERROR ("snprintf failed");
+
+    cairo_save(cr);
+    cairo_translate(cr, x, y);
+    rsvg_handle_render_cairo (game->blanktile, cr);
+    
+    if (card->color == BLACK)
+        cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
+    if (card->color == BLUE)
+        cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);      
+    if (card->color == RED)
+        cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
+    if (card->color == YELLOW)
+       cairo_set_source_rgb (cr, 1.0, .843, 0.0);
+    cairo_move_to (cr, 10, 25);
+    cairo_set_font_size(cr, 25);
+    cairo_show_text (cr, number_string);
+    
+    cairo_restore(cr);
+}
+
 static void card_group_init(card_group_t *card_group)
 {
     card_group->num_cards = 0;
@@ -75,29 +109,64 @@ static void player_init(player_t *player)
 }
 
 
-static int card_compare(card_t *card_one, card_t *card_two)
+/* If card_one < card_two, then return value will be negative
+   if they are equal, 0 will be returned,
+   if card_one > card_two, then return value will be positive */
+static int card_compare(const void *one, const void *two)
 {
+    const card_t *card_one = one;
+    const card_t *card_two = two;
     return card_one->number - card_two->number;
 }
 
-static int card_group_is_run(card_group_t *card_group)
+static int card_group_is_run_one(card_group_t *card_group)
+{
+    int i;
+    qsort (&card_group->cards[0], card_group->num_cards,
+          sizeof (card_t), card_compare);
+          
+    if (card_group->num_cards > 13 || card_group->num_cards < 3)
+    {
+       return 0;
+    }
+    for (i = 0; i < card_group->num_cards - 1; ++i)
+       if(card_group->cards[i].color != card_group->cards[i + 1].color)
+       {
+           return 0;
+       }
+       if(card_group->cards[i].number != card_group->cards[i + 1].number -1)
+       {
+           return 0;
+       }
+    return 1;
+}
+
+
+static int card_group_is_run_two(card_group_t *card_group)
 {
     int i;
     int lowest = 14, highest = 0;
     color_t run_color;
 
-    if (card_group->num_cards > 13 || card_group->num_cards < 3)
+    /* By definition, a run must have at least 3 cards. Also, it's
+     * impossible for any group of cards with more than 13 cards to be
+     * a run, (there are only 13 unique numbers so a group with more
+     * than 13 cards must have some duplicates).
+     */
+    if (card_group->num_cards < 3 || card_group->num_cards > 13)
     {
        return 0;
     }
 
-    run_color = card_group->cards[i].color;
+    /* Loop through all cards in the group, ensuring that they are all
+     * the same color and finding the highest and lowest number in the
+     * group. */
+    run_color = card_group->cards[0].color;
 
     for (i = 0; i < card_group->num_cards; i++)
     {
        if (card_group->cards[i].color != run_color)
            return 0;
-
        if (card_group->cards[i].number > highest)
        {
            highest = card_group->cards[i].number;
@@ -107,10 +176,31 @@ static int card_group_is_run(card_group_t *card_group)
            lowest = card_group->cards[i].number;
        }
     }
+
+    /* For a run, the difference between the highest and lowest cards
+     * will always be one less than the number of cards in the
+     * group. If not then we know it's not a run.
+     */
     if (highest - lowest != card_group->num_cards - 1)
     {
        return 0;
     }
+
+    /* XXX: There's a bug here. We're guessing that at this point
+     * anything we're looking at must be a run. This would be correct
+     * if there were no duplicate cards, but since there are
+     * duplicates this us quite broken. For example consider two
+     * sequences of entirely red cards:
+     *
+     * This is a run:   1, 2, 3, 4
+     * But this is not: 1, 3, 4, 4
+     *
+     * As currently written, this function will consider both of these
+     * groups to be a run. One possible fix is to throw away the
+     * highest - lowest heuristic and instead simply sort the cards up
+     * front and ensure the difference between each adjacent pair is
+     * exactly 1.
+     */
     return 1;
 }
 
@@ -209,11 +299,11 @@ static void deck_shuffle(deck_t *deck)
 static void deck_print(deck_t *deck)
 {
     int h, i, j;  
-    for (h = 0; h <= 1; ++h)
+    for (h = 0; h < 2; ++h)
     {
-       for (i = 0; i <= 3; ++i)
+       for (i = 0; i < 4; ++i)
        {
-           for (j = 0; j <= 12; ++j)
+           for (j = 0; j < 13; ++j)
            {
                card_print(deck->cards[j + (i * 13) + (h * 52)]);
            }
@@ -222,6 +312,20 @@ static void deck_print(deck_t *deck)
     printf ("There are %d tiles in the deck\n" , deck->num_cards);
 }
 
+static void deck_draw(game_t *game, cairo_t *cr)
+{
+    int i, j;
+    
+    for (i = 0; i < 8; ++i)
+    {
+       for (j = 0; j < 13; ++j)
+       {
+           card_draw(game, &game->deck.cards[j + (i * 13)],
+                     cr, 45 * j, 55 * i);
+       }
+    }
+}   
+
 static void hand_print(game_t *game)
 {
     int i;
@@ -234,6 +338,8 @@ static void hand_print(game_t *game)
 static void game_init(game_t *game)
 {
     int i;
+    GError *error = NULL;
+
     game->num_players = 0;
     
     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
@@ -245,11 +351,21 @@ static void game_init(game_t *game)
     board_init(&game->board);
     deck_init(&game->deck);
     deck_shuffle(&game->deck);
+
+    game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
+    if (error)
+       FATAL_ERROR (error->message);
 }
 
 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
 {
-    printf ("I should be drawing something here\n");
+    cairo_t *cr;
+
+    cr = gdk_cairo_create (widget->window);
+
+    deck_draw(game, cr);
+
+    cairo_destroy (cr);
 
     return TRUE;
 }
@@ -281,15 +397,15 @@ int main(int argc, char *argv[])
     game_t game;
     
     srand(time(NULL));
+
+    gtk_init (&argc, &argv);
     
     game_init(&game);
     deck_print(&game.deck);
-    deck_deal(&game, &game.deck);
+    //deck_deal(&game, &game.deck);
     hand_print(&game);
     deck_print(&game.deck);
 
-    gtk_init (&argc, &argv);
-
     /* Create a new window */
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     gtk_window_set_default_size (GTK_WINDOW (window),