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_TILES 104
23 tile_t tiles[DECK_MAX_TILES];
27 #define TILE_GROUP_MAX_TILES DECK_MAX_TILES
29 typedef struct tile_group {
30 tile_t tiles[TILE_GROUP_MAX_TILES];
34 #define BOARD_MAX_TILE_GROUPS (DECK_MAX_TILES / 3)
36 typedef struct board {
37 tile_group_t groups[BOARD_MAX_TILE_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 tile_print(tile_t tile)
59 printf("%6s %2d\n", colors[tile.color], tile.number + 1);
62 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, int x, int y)
64 char number_string[3];
67 len = snprintf (number_string, 3, "%d", tile->number + 1);
68 if (len < 0 || len >= 3)
69 FATAL_ERROR ("snprintf failed");
72 cairo_translate(cr, x, y);
73 rsvg_handle_render_cairo (game->blanktile, cr);
75 if (tile->color == BLACK)
76 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
77 if (tile->color == BLUE)
78 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
79 if (tile->color == RED)
80 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
81 if (tile->color == YELLOW)
82 cairo_set_source_rgb (cr, 1.0, .843, 0.0);
83 cairo_move_to (cr, 10, 25);
84 cairo_set_font_size(cr, 25);
85 cairo_show_text (cr, number_string);
90 static void tile_group_init(tile_group_t *tile_group)
92 tile_group->num_tiles = 0;
95 static void board_init(board_t *board)
98 board->num_groups = 0;
100 for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
102 tile_group_init(&board->groups[i]);
106 static void player_init(player_t *player)
108 tile_group_init(&player->hand);
112 /* If tile_one < tile_two, then return value will be negative
113 if they are equal, 0 will be returned,
114 if tile_one > tile_two, then return value will be positive */
115 static int tile_compare(const void *one, const void *two)
117 const tile_t *tile_one = one;
118 const tile_t *tile_two = two;
119 return tile_one->number - tile_two->number;
122 static int tile_group_is_run_one(tile_group_t *tile_group)
125 qsort (&tile_group->tiles[0], tile_group->num_tiles,
126 sizeof (tile_t), tile_compare);
128 if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
132 for (i = 0; i < tile_group->num_tiles - 1; ++i)
133 if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
137 if(tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1)
145 static int tile_group_is_run_two(tile_group_t *tile_group)
148 int lowest = 14, highest = 0;
151 /* By definition, a run must have at least 3 tiles. Also, it's
152 * impossible for any group of tiles with more than 13 tiles to be
153 * a run, (there are only 13 unique numbers so a group with more
154 * than 13 tiles must have some duplicates).
156 if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
161 /* Loop through all tiles in the group, ensuring that they are all
162 * the same color and finding the highest and lowest number in the
164 run_color = tile_group->tiles[0].color;
166 for (i = 0; i < tile_group->num_tiles; i++)
168 if (tile_group->tiles[i].color != run_color)
170 if (tile_group->tiles[i].number > highest)
172 highest = tile_group->tiles[i].number;
174 if (tile_group->tiles[i].number < lowest)
176 lowest = tile_group->tiles[i].number;
180 /* For a run, the difference between the highest and lowest tiles
181 * will always be one less than the number of tiles in the
182 * group. If not then we know it's not a run.
184 if (highest - lowest != tile_group->num_tiles - 1)
189 /* XXX: There's a bug here. We're guessing that at this point
190 * anything we're looking at must be a run. This would be correct
191 * if there were no duplicate tiles, but since there are
192 * duplicates this us quite broken. For example consider two
193 * sequences of entirely red tiles:
195 * This is a run: 1, 2, 3, 4
196 * But this is not: 1, 3, 4, 4
198 * As currently written, this function will consider both of these
199 * groups to be a run. One possible fix is to throw away the
200 * highest - lowest heuristic and instead simply sort the tiles up
201 * front and ensure the difference between each adjacent pair is
207 static int tile_group_is_set(tile_group_t *tile_group)
210 color_t seen_color[tile_group->num_tiles];
212 if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
216 for (i = 0; i < tile_group->num_tiles - 1; ++i)
218 if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number)
223 seen_color[i] = tile_group->tiles[i].color;
224 for (i = 0; i < tile_group->num_tiles; ++i)
226 seen_color[tile_group->tiles[i].color]++;
227 if (seen_color[tile_group->tiles[i].color] > 1)
235 static void deck_deal(game_t *game, deck_t *deck)
241 printf ("How many players(1-4) should I deal in? ");
242 game->num_players = getchar();
243 if (game->num_players == EOF)
245 printf ("\nGoodbye.\n");
249 game->num_players -= '0';
251 for (i = 0; i < game->num_players; ++i)
253 for (j = 0; j < 14; ++j)
255 rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
256 temp = deck->tiles[rand_tile];
257 deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
258 game->players[i].hand.tiles[j] = temp;
259 deck->num_tiles -= 1;
260 game->players[i].hand.num_tiles += 1;
263 printf ("Game dealt for %d player(s)\n", game->num_players);
266 static void deck_init(deck_t *deck)
270 for (h = 0; h <= 1; ++h)
272 for (i = 0; i <= 3; ++i)
274 for (j = 0; j <= 12; ++j)
276 deck->tiles[j + (i * 13) + (h * 52)].color = i;
277 deck->tiles[j + (i * 13) + (h * 52)].number = j;
278 deck->num_tiles += 1;
279 printf ("There are %d tiles in the deck\n", deck->num_tiles);
285 static void deck_shuffle(deck_t *deck)
290 for (last = deck->num_tiles; last > 0; --last)
292 rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
293 temp = deck->tiles[rand_tile];
294 deck->tiles[rand_tile] = deck->tiles[last - 1];
295 deck->tiles[last - 1] = temp;
299 static void deck_print(deck_t *deck)
302 for (h = 0; h < 2; ++h)
304 for (i = 0; i < 4; ++i)
306 for (j = 0; j < 13; ++j)
308 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
312 printf ("There are %d tiles in the deck\n" , deck->num_tiles);
315 static void deck_draw(game_t *game, cairo_t *cr)
319 for (i = 0; i < 8; ++i)
321 for (j = 0; j < 13; ++j)
323 tile_draw(game, &game->deck.tiles[j + (i * 13)],
329 static void hand_print(game_t *game)
332 for (i = 0; i < game->players[0].hand.num_tiles; ++i)
334 tile_print(game->players[0].hand.tiles[i]);
338 static void game_init(game_t *game)
341 GError *error = NULL;
343 game->num_players = 0;
345 for (i = 0; i < GAME_MAX_PLAYERS; ++i)
347 player_init(&game->players[i]);
348 game->num_players += 1;
351 board_init(&game->board);
352 deck_init(&game->deck);
353 deck_shuffle(&game->deck);
355 game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
357 FATAL_ERROR (error->message);
360 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
364 cr = gdk_cairo_create (widget->window);
373 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
375 printf ("You pressed key %d\n", event->keyval);
380 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
382 printf ("You pressed button %d\n", event->button);
387 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
389 printf ("You released button %d\n", event->button);
394 int main(int argc, char *argv[])
401 gtk_init (&argc, &argv);
404 deck_print(&game.deck);
405 //deck_deal(&game, &game.deck);
407 deck_print(&game.deck);
409 /* Create a new window */
410 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
411 gtk_window_set_default_size (GTK_WINDOW (window),
412 GAME_WINDOW_DEFAULT_WIDTH,
413 GAME_WINDOW_DEFAULT_HEIGHT);
415 gtk_widget_set_events (window,
418 GDK_BUTTON_PRESS_MASK |
419 GDK_BUTTON_RELEASE_MASK);
421 g_signal_connect (G_OBJECT (window), "delete_event",
422 G_CALLBACK (gtk_main_quit), NULL);
423 g_signal_connect (G_OBJECT (window), "expose_event",
424 G_CALLBACK (on_expose_event), &game);
425 g_signal_connect (G_OBJECT (window), "key_press_event",
426 G_CALLBACK (on_key_press_event), &game);
427 g_signal_connect (G_OBJECT (window), "button_press_event",
428 G_CALLBACK (on_button_press_event), &game);
429 g_signal_connect (G_OBJECT (window), "button_release_event",
430 G_CALLBACK (on_button_release_event), &game);
432 gtk_widget_show_all (window);