5 #include <librsvg/rsvg.h>
6 #include <librsvg/rsvg-cairo.h>
8 #define FATAL_ERROR(msg) \
9 do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0)
11 char *colors[] = {"Black", "Blue", "Red", "Yellow"};
13 typedef enum {BLACK, BLUE, RED, YELLOW} color_t;
20 #define DECK_MAX_CARDS 104
23 card_t cards[DECK_MAX_CARDS];
27 #define CARD_GROUP_MAX_CARDS DECK_MAX_CARDS
29 typedef struct card_group {
30 card_t cards[CARD_GROUP_MAX_CARDS];
34 #define BOARD_MAX_CARD_GROUPS (DECK_MAX_CARDS / 3)
36 typedef struct board {
37 card_group_t groups[BOARD_MAX_CARD_GROUPS];
41 typedef struct player {
45 #define GAME_MAX_PLAYERS 4
46 #define GAME_WINDOW_DEFAULT_WIDTH 800
47 #define GAME_WINDOW_DEFAULT_HEIGHT 600
50 player_t players[GAME_MAX_PLAYERS];
54 RsvgHandle *blanktile;
57 static void card_print(card_t card)
59 printf("%6s %2d\n", colors[card.color], card.number + 1);
62 static void card_group_init(card_group_t *card_group)
64 card_group->num_cards = 0;
67 static void board_init(board_t *board)
70 board->num_groups = 0;
72 for (i = 0; i <= BOARD_MAX_CARD_GROUPS; ++i)
74 card_group_init(&board->groups[i]);
78 static void player_init(player_t *player)
80 card_group_init(&player->hand);
84 /* If card_one < card_two, then return value will be negative
85 if they are equal, 0 will be returned,
86 if card_one > card_two, then return value will be positive */
87 static int card_compare(card_t *card_one, card_t *card_two)
89 return card_one->number - card_two->number;
92 static int card_group_is_run_one(card_group_t *card_group)
95 qsort (&card_group->cards[0], card_group->num_cards,
96 sizeof (card_t), card_compare);
98 if (card_group->num_cards > 13 || card_group->num_cards < 3)
102 for (i = 0; i < card_group->num_cards - 1; ++i)
103 if(card_group->cards[i].color != card_group->cards[i + 1].color)
107 if(card_group->cards[i].number != card_group->cards[i + 1].number -1)
115 static int card_group_is_run_two(card_group_t *card_group)
118 int lowest = 14, highest = 0;
121 /* By definition, a run must have at least 3 cards. Also, it's
122 * impossible for any group of cards with more than 13 cards to be
123 * a run, (there are only 13 unique numbers so a group with more
124 * than 13 cards must have some duplicates).
126 if (card_group->num_cards < 3 || card_group->num_cards > 13)
131 /* Loop through all cards in the group, ensuring that they are all
132 * the same color and finding the highest and lowest number in the
134 run_color = card_group->cards[0].color;
136 for (i = 0; i < card_group->num_cards; i++)
138 if (card_group->cards[i].color != run_color)
140 if (card_group->cards[i].number > highest)
142 highest = card_group->cards[i].number;
144 if (card_group->cards[i].number < lowest)
146 lowest = card_group->cards[i].number;
150 /* For a run, the difference between the highest and lowest cards
151 * will always be one less than the number of cards in the
152 * group. If not then we know it's not a run.
154 if (highest - lowest != card_group->num_cards - 1)
159 /* XXX: There's a bug here. We're guessing that at this point
160 * anything we're looking at must be a run. This would be correct
161 * if there were no duplicate cards, but since there are
162 * duplicates this us quite broken. For example consider two
163 * sequences of entirely red cards:
165 * This is a run: 1, 2, 3, 4
166 * But this is not: 1, 3, 4, 4
168 * As currently written, this function will consider both of these
169 * groups to be a run. One possible fix is to throw away the
170 * highest - lowest heuristic and instead simply sort the cards up
171 * front and ensure the difference between each adjacent pair is
177 static int card_group_is_set(card_group_t *card_group)
180 color_t seen_color[card_group->num_cards];
182 if (card_group->num_cards > 4 || card_group->num_cards < 3)
186 for (i = 0; i < card_group->num_cards - 1; ++i)
188 if (card_group->cards[i].number != card_group->cards[i + 1].number)
193 seen_color[i] = card_group->cards[i].color;
194 for (i = 0; i < card_group->num_cards; ++i)
196 seen_color[card_group->cards[i].color]++;
197 if (seen_color[card_group->cards[i].color] > 1)
205 static void deck_deal(game_t *game, deck_t *deck)
211 printf ("How many players(1-4) should I deal in? ");
212 game->num_players = getchar();
213 if (game->num_players == EOF)
215 printf ("\nGoodbye.\n");
219 game->num_players -= '0';
221 for (i = 0; i < game->num_players; ++i)
223 for (j = 0; j < 14; ++j)
225 rand_card = ((deck->num_cards + 1.0) * rand()) / (RAND_MAX + 1.0);
226 temp = deck->cards[rand_card];
227 deck->cards[rand_card] = deck->cards[deck->num_cards - 1];
228 game->players[i].hand.cards[j] = temp;
229 deck->num_cards -= 1;
230 game->players[i].hand.num_cards += 1;
233 printf ("Game dealt for %d player(s)\n", game->num_players);
236 static void deck_init(deck_t *deck)
240 for (h = 0; h <= 1; ++h)
242 for (i = 0; i <= 3; ++i)
244 for (j = 0; j <= 12; ++j)
246 deck->cards[j + (i * 13) + (h * 52)].color = i;
247 deck->cards[j + (i * 13) + (h * 52)].number = j;
248 deck->num_cards += 1;
249 printf ("There are %d tiles in the deck\n", deck->num_cards);
255 static void deck_shuffle(deck_t *deck)
260 for (last = deck->num_cards; last > 0; --last)
262 rand_card = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
263 temp = deck->cards[rand_card];
264 deck->cards[rand_card] = deck->cards[last - 1];
265 deck->cards[last - 1] = temp;
269 static void deck_print(deck_t *deck)
272 for (h = 0; h <= 1; ++h)
274 for (i = 0; i <= 3; ++i)
276 for (j = 0; j <= 12; ++j)
278 card_print(deck->cards[j + (i * 13) + (h * 52)]);
282 printf ("There are %d tiles in the deck\n" , deck->num_cards);
285 static void hand_print(game_t *game)
288 for (i = 0; i < game->players[0].hand.num_cards; ++i)
290 card_print(game->players[0].hand.cards[i]);
294 static void card_draw(game_t *game, card_t *card, cairo_t *cr, int x, int y)
296 char number_string[2];
297 number_string[0] = &card->number;
298 number_string[1] = '\0';
301 cairo_translate(cr, x, y);
302 rsvg_handle_render_cairo (game->blanktile, cr);
304 if (&card->color == BLACK)
305 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
306 if (&card->color == BLUE)
307 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
308 if (&card->color == RED)
309 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
310 if (&card->color == YELLOW)
311 cairo_set_source_rgb (cr, 1.0, 1.0, 0.0);
312 cairo_move_to (cr, 0, 25);
313 cairo_set_font_size(cr, 25);
314 cairo_show_text (cr, number_string[0]);
319 static void game_init(game_t *game)
322 GError *error = NULL;
324 game->num_players = 0;
326 for (i = 0; i < GAME_MAX_PLAYERS; ++i)
328 player_init(&game->players[i]);
329 game->num_players += 1;
332 board_init(&game->board);
333 deck_init(&game->deck);
334 deck_shuffle(&game->deck);
336 game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
338 FATAL_ERROR (error->message);
341 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
345 cr = gdk_cairo_create (widget->window);
347 rsvg_handle_render_cairo (game->blanktile, cr);
348 card_draw(game, game->deck.cards[0], cr, 320, 240);
355 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
357 printf ("You pressed key %d\n", event->keyval);
362 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
364 printf ("You pressed button %d\n", event->button);
369 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
371 printf ("You released button %d\n", event->button);
376 int main(int argc, char *argv[])
383 gtk_init (&argc, &argv);
386 deck_print(&game.deck);
387 //deck_deal(&game, &game.deck);
389 deck_print(&game.deck);
391 /* Create a new window */
392 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
393 gtk_window_set_default_size (GTK_WINDOW (window),
394 GAME_WINDOW_DEFAULT_WIDTH,
395 GAME_WINDOW_DEFAULT_HEIGHT);
397 gtk_widget_set_events (window,
400 GDK_BUTTON_PRESS_MASK |
401 GDK_BUTTON_RELEASE_MASK);
403 g_signal_connect (G_OBJECT (window), "delete_event",
404 G_CALLBACK (gtk_main_quit), NULL);
405 g_signal_connect (G_OBJECT (window), "expose_event",
406 G_CALLBACK (on_expose_event), &game);
407 g_signal_connect (G_OBJECT (window), "key_press_event",
408 G_CALLBACK (on_key_press_event), &game);
409 g_signal_connect (G_OBJECT (window), "button_press_event",
410 G_CALLBACK (on_button_press_event), &game);
411 g_signal_connect (G_OBJECT (window), "button_release_event",
412 G_CALLBACK (on_button_release_event), &game);
414 gtk_widget_show_all (window);