X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=kub.c;h=326856bfc326d954394e55a27169b93728ef17c8;hb=da6cb72d21c540631238e0f3a974dc814aea8a9c;hp=69c3d033a9f0162245f3d6830da3275256837daa;hpb=092aee5ea3be9390f5cae49ebac86383218720bd;p=kub diff --git a/kub.c b/kub.c index 69c3d03..326856b 100644 --- a/kub.c +++ b/kub.c @@ -87,7 +87,7 @@ 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() ); + sizeof (card_t), card_compare); if (card_group->num_cards > 13 || card_group->num_cards < 3) { @@ -112,18 +112,25 @@ static int card_group_is_run_two(card_group_t *card_group) 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; } + /* 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; @@ -133,10 +140,31 @@ static int card_group_is_run_two(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; } @@ -275,7 +303,15 @@ static void game_init(game_t *game) 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); + + cairo_set_source_rgb (cr, 1, 0, 0); /* red */ + cairo_rectangle (cr, 86, 66, 74, 103); + cairo_fill (cr); + + cairo_destroy (cr); return TRUE; }