5 #include <librsvg/rsvg.h>
6 #include <librsvg/rsvg-cairo.h>
9 * TILE_NUMBER_X_OFFSET = 3
11 * || TILE_NUMBER_WIDTH = 34
13 * || | TILE_WIDTH = 40
16 * +-----+ - TILE_NUMBER_Y_OFFSET = 3 -
19 * || || TILE_NUMBER_HEIGHT = 24 |
21 * | _ | _ |-TILE_HEIGHT = 50
23 * || || TILE_SUN_HEIGHT = 20 |
25 * | | TILE_SUN_Y_OFFSET = 3 |
27 * || | TILE_SUN_WIDTH = 20
29 * || TILE_SUN_X_OFFSET = 10
32 #define TILE_HEIGHT 50
34 #define TILE_NUMBER_X_OFFSET 3
35 #define TILE_NUMBER_Y_OFFSET 3
36 #define TILE_NUMBER_WIDTH 34
37 #define TILE_NUMBER_HEIGHT 24
39 #define TILE_SUN_X_OFFSET 10
40 #define TILE_SUN_Y_OFFSET 3
41 #define TILE_SUN_WIDTH 20
42 #define TILE_SUN_HEIGHT 20
44 #define FATAL_ERROR(msg) \
45 do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0)
47 char *colors[] = {"Black", "Blue", "Red", "Yellow"};
49 typedef enum {BLACK, BLUE, RED, YELLOW} color_t;
58 #define DECK_MAX_TILES 104
61 tile_t tiles[DECK_MAX_TILES];
65 #define TILE_GROUP_MAX_TILES DECK_MAX_TILES
67 typedef struct tile_group {
68 tile_t tiles[TILE_GROUP_MAX_TILES];
72 #define BOARD_MAX_TILE_GROUPS (DECK_MAX_TILES / 3)
74 typedef struct board {
75 tile_group_t groups[BOARD_MAX_TILE_GROUPS];
79 typedef struct player {
83 #define GAME_MAX_PLAYERS 4
84 #define GAME_WINDOW_DEFAULT_WIDTH 800
85 #define GAME_WINDOW_DEFAULT_HEIGHT 600
88 player_t players[GAME_MAX_PLAYERS];
92 RsvgHandle *blanktile;
98 tile_init (tile_t *tile, color_t color, int number)
101 tile->number = number;
106 static void tile_print(tile_t tile)
108 printf("%6s %2d\n", colors[tile.color], tile.number + 1);
111 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr)
113 char number_string[3];
116 len = snprintf (number_string, 3, "%d", tile->number + 1);
117 if (len < 0 || len >= 3)
118 FATAL_ERROR ("snprintf failed");
121 cairo_translate(cr, tile->x, tile->y);
122 rsvg_handle_render_cairo (game->blanktile, cr);
124 if (tile->color == BLACK)
125 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
126 if (tile->color == BLUE)
127 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
128 if (tile->color == RED)
129 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
130 if (tile->color == YELLOW)
131 cairo_set_source_rgb (cr, 1.0, .843, 0.0);
132 cairo_move_to (cr, 10, 25);
133 cairo_set_font_size(cr, 25);
134 cairo_show_text (cr, number_string);
139 static void tile_group_init(tile_group_t *tile_group)
141 tile_group->num_tiles = 0;
144 static void board_init(board_t *board)
147 board->num_groups = 0;
149 for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
151 tile_group_init(&board->groups[i]);
155 static void player_init(player_t *player)
157 tile_group_init(&player->hand);
161 /* If tile_one < tile_two, then return value will be negative
162 if they are equal, 0 will be returned,
163 if tile_one > tile_two, then return value will be positive */
164 static int tile_compare(const void *one, const void *two)
166 const tile_t *tile_one = one;
167 const tile_t *tile_two = two;
168 return tile_one->number - tile_two->number;
171 static int tile_group_is_run_one(tile_group_t *tile_group)
174 qsort (&tile_group->tiles[0], tile_group->num_tiles,
175 sizeof (tile_t), tile_compare);
177 if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
181 for (i = 0; i < tile_group->num_tiles - 1; ++i)
182 if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
186 if(tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1)
194 static int tile_group_is_run_two(tile_group_t *tile_group)
197 int lowest = 14, highest = 0;
200 /* By definition, a run must have at least 3 tiles. Also, it's
201 * impossible for any group of tiles with more than 13 tiles to be
202 * a run, (there are only 13 unique numbers so a group with more
203 * than 13 tiles must have some duplicates).
205 if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
210 /* Loop through all tiles in the group, ensuring that they are all
211 * the same color and finding the highest and lowest number in the
213 run_color = tile_group->tiles[0].color;
215 for (i = 0; i < tile_group->num_tiles; i++)
217 if (tile_group->tiles[i].color != run_color)
219 if (tile_group->tiles[i].number > highest)
221 highest = tile_group->tiles[i].number;
223 if (tile_group->tiles[i].number < lowest)
225 lowest = tile_group->tiles[i].number;
229 /* For a run, the difference between the highest and lowest tiles
230 * will always be one less than the number of tiles in the
231 * group. If not then we know it's not a run.
233 if (highest - lowest != tile_group->num_tiles - 1)
238 /* XXX: There's a bug here. We're guessing that at this point
239 * anything we're looking at must be a run. This would be correct
240 * if there were no duplicate tiles, but since there are
241 * duplicates this us quite broken. For example consider two
242 * sequences of entirely red tiles:
244 * This is a run: 1, 2, 3, 4
245 * But this is not: 1, 3, 4, 4
247 * As currently written, this function will consider both of these
248 * groups to be a run. One possible fix is to throw away the
249 * highest - lowest heuristic and instead simply sort the tiles up
250 * front and ensure the difference between each adjacent pair is
256 static int tile_group_is_set(tile_group_t *tile_group)
259 color_t seen_color[tile_group->num_tiles];
261 if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
265 for (i = 0; i < tile_group->num_tiles - 1; ++i)
267 if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number)
272 seen_color[i] = tile_group->tiles[i].color;
273 for (i = 0; i < tile_group->num_tiles; ++i)
275 seen_color[tile_group->tiles[i].color]++;
276 if (seen_color[tile_group->tiles[i].color] > 1)
284 static void deck_deal(game_t *game, deck_t *deck)
290 printf ("How many players(1-4) should I deal in? ");
291 game->num_players = getchar();
292 if (game->num_players == EOF)
294 printf ("\nGoodbye.\n");
298 game->num_players -= '0';
300 for (i = 0; i < game->num_players; ++i)
302 for (j = 0; j < 14; ++j)
304 rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
305 temp = deck->tiles[rand_tile];
306 deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
307 game->players[i].hand.tiles[j] = temp;
308 deck->num_tiles -= 1;
309 game->players[i].hand.num_tiles += 1;
312 printf ("Game dealt for %d player(s)\n", game->num_players);
315 static void deck_init(deck_t *deck)
319 for (h = 0; h <= 1; ++h)
321 for (i = 0; i <= 3; ++i)
323 for (j = 0; j <= 12; ++j)
325 tile_init (&deck->tiles[deck->num_tiles++], i, j);
326 printf ("There are %d tiles in the deck\n", deck->num_tiles);
332 static void deck_shuffle(deck_t *deck)
337 for (last = deck->num_tiles; last > 0; --last)
339 rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
340 temp = deck->tiles[rand_tile];
341 deck->tiles[rand_tile] = deck->tiles[last - 1];
342 deck->tiles[last - 1] = temp;
346 static void deck_print(deck_t *deck)
349 for (h = 0; h < 2; ++h)
351 for (i = 0; i < 4; ++i)
353 for (j = 0; j < 13; ++j)
355 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
359 printf ("There are %d tiles in the deck\n" , deck->num_tiles);
362 static void deck_spread(deck_t *deck)
365 for (i = 0; i < 8; i++)
367 for (j = 0; j < 13; j++)
369 deck->tiles[j + (i * 13)].x = j * 50;
370 deck->tiles[j + (i * 13)].y = i * 60;
375 static void deck_draw(game_t *game, cairo_t *cr)
378 for (i = 0; i < game->deck.num_tiles; i++)
380 tile_draw(game, &game->deck.tiles[i], cr);
384 static void hand_print(game_t *game)
387 for (i = 0; i < game->players[0].hand.num_tiles; ++i)
389 tile_print(game->players[0].hand.tiles[i]);
393 static void game_init(game_t *game)
396 GError *error = NULL;
398 game->num_players = 0;
400 for (i = 0; i < GAME_MAX_PLAYERS; ++i)
402 player_init(&game->players[i]);
403 game->num_players += 1;
406 board_init(&game->board);
407 deck_init(&game->deck);
408 deck_shuffle(&game->deck);
410 game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
412 FATAL_ERROR (error->message);
414 game->current_tile = game->deck.num_tiles - 1;
417 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
421 cr = gdk_cairo_create (widget->window);
430 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
432 printf ("You pressed key %d\n", event->keyval);
437 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
439 int i, tile_x, tile_y;
441 for (i = 0; i < game->deck.num_tiles; i++)
443 tile_x = game->deck.tiles[i].x;
444 tile_y = game->deck.tiles[i].y;
445 if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
446 event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
448 game->current_tile = i;
453 tile = &game->deck.tiles[game->current_tile];
455 printf ("Placing tile #%d\n", game->current_tile);
457 /* First, invalidate the region where the tile currently is. */
458 gtk_widget_queue_draw_area (widget, tile->x, tile->y, TILE_WIDTH, TILE_HEIGHT);
460 /* Then, move the tile */
464 /* Finally, invalidate the region where the tile is now. */
465 gtk_widget_queue_draw_area (widget, tile->x, tile->y, TILE_WIDTH, TILE_HEIGHT);
467 game->current_tile--;
468 if (game->current_tile < 0)
469 game->current_tile = game->deck.num_tiles - 1;
474 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
476 printf ("You released button %d\n", event->button);
481 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
482 game_t *game, cairo_t *cr)
484 game->deck.tiles[game->current_tile].x = event->x;
485 game->deck.tiles[game->current_tile].y = event->y;
487 tile_draw(game, &game->deck.tiles[game->current_tile], cr);
492 int main(int argc, char *argv[])
499 gtk_init (&argc, &argv);
502 deck_print(&game.deck);
503 deck_spread(&game.deck);
504 //deck_deal(&game, &game.deck);
506 //deck_print(&game.deck);
508 /* Create a new window */
509 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
510 gtk_window_set_default_size (GTK_WINDOW (window),
511 GAME_WINDOW_DEFAULT_WIDTH,
512 GAME_WINDOW_DEFAULT_HEIGHT);
514 gtk_widget_set_events (window,
517 GDK_BUTTON_MOTION_MASK |
518 GDK_BUTTON_PRESS_MASK |
519 GDK_BUTTON_RELEASE_MASK);
521 g_signal_connect (G_OBJECT (window), "delete_event",
522 G_CALLBACK (gtk_main_quit), NULL);
523 g_signal_connect (G_OBJECT (window), "expose_event",
524 G_CALLBACK (on_expose_event), &game);
525 g_signal_connect (G_OBJECT (window), "key_press_event",
526 G_CALLBACK (on_key_press_event), &game);
527 g_signal_connect (G_OBJECT (window), "button_press_event",
528 G_CALLBACK (on_button_press_event), &game);
529 g_signal_connect (G_OBJECT (window), "button_release_event",
530 G_CALLBACK (on_button_release_event), &game);
531 g_signal_connect (G_OBJECT (window), "motion_notify_event",
532 G_CALLBACK (on_button_motion_event), &game);
535 gtk_widget_show_all (window);