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 static void tile_init (tile_t *tile, color_t color, int number)
101 tile->number = number;
106 static void tile_set_x_y (tile_t *tile, int x, int y)
112 static void tile_print(tile_t tile)
114 printf("%6s %2d\n", colors[tile.color], tile.number + 1);
117 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region)
119 char number_string[3];
121 GdkRectangle rectangle;
123 rectangle.x = tile->x - 1;
124 rectangle.y = tile->y - 1;
125 rectangle.width = TILE_WIDTH + 2;
126 rectangle.height = TILE_HEIGHT + 2;
127 if (gdk_region_rect_in (region, &rectangle) == GDK_OVERLAP_RECTANGLE_OUT)
130 len = snprintf (number_string, 3, "%d", tile->number + 1);
131 if (len < 0 || len >= 3)
132 FATAL_ERROR ("snprintf failed");
135 cairo_translate(cr, tile->x, tile->y);
136 rsvg_handle_render_cairo (game->blanktile, cr);
138 if (tile->color == BLACK)
139 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
140 if (tile->color == BLUE)
141 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
142 if (tile->color == RED)
143 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
144 if (tile->color == YELLOW)
145 cairo_set_source_rgb (cr, 1.0, .843, 0.0);
146 if (tile->number + 1 > 9)
147 cairo_move_to (cr, 1, 25);
149 cairo_move_to (cr, 10, 25);
150 cairo_set_font_size(cr, 25);
151 cairo_show_text (cr, number_string);
156 static void tile_group_init(tile_group_t *tile_group)
158 tile_group->num_tiles = 0;
161 static void board_init(board_t *board)
164 board->num_groups = 0;
166 for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
168 tile_group_init(&board->groups[i]);
172 static void player_init(player_t *player)
174 tile_group_init(&player->hand);
178 /* If tile_one < tile_two, then return value will be negative
179 if they are equal, 0 will be returned,
180 if tile_one > tile_two, then return value will be positive */
181 static int tile_compare(const void *one, const void *two)
183 const tile_t *tile_one = one;
184 const tile_t *tile_two = two;
185 return tile_one->number - tile_two->number;
188 static int tile_group_is_run_one(tile_group_t *tile_group)
191 qsort (&tile_group->tiles[0], tile_group->num_tiles,
192 sizeof (tile_t), tile_compare);
194 if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
198 for (i = 0; i < tile_group->num_tiles - 1; ++i)
199 if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
203 if(tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1)
211 static int tile_group_is_run_two(tile_group_t *tile_group)
214 int lowest = 14, highest = 0;
217 /* By definition, a run must have at least 3 tiles. Also, it's
218 * impossible for any group of tiles with more than 13 tiles to be
219 * a run, (there are only 13 unique numbers so a group with more
220 * than 13 tiles must have some duplicates).
222 if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
227 /* Loop through all tiles in the group, ensuring that they are all
228 * the same color and finding the highest and lowest number in the
230 run_color = tile_group->tiles[0].color;
232 for (i = 0; i < tile_group->num_tiles; i++)
234 if (tile_group->tiles[i].color != run_color)
236 if (tile_group->tiles[i].number > highest)
238 highest = tile_group->tiles[i].number;
240 if (tile_group->tiles[i].number < lowest)
242 lowest = tile_group->tiles[i].number;
246 /* For a run, the difference between the highest and lowest tiles
247 * will always be one less than the number of tiles in the
248 * group. If not then we know it's not a run.
250 if (highest - lowest != tile_group->num_tiles - 1)
255 /* XXX: There's a bug here. We're guessing that at this point
256 * anything we're looking at must be a run. This would be correct
257 * if there were no duplicate tiles, but since there are
258 * duplicates this us quite broken. For example consider two
259 * sequences of entirely red tiles:
261 * This is a run: 1, 2, 3, 4
262 * But this is not: 1, 3, 4, 4
264 * As currently written, this function will consider both of these
265 * groups to be a run. One possible fix is to throw away the
266 * highest - lowest heuristic and instead simply sort the tiles up
267 * front and ensure the difference between each adjacent pair is
273 static int tile_group_is_set(tile_group_t *tile_group)
276 color_t seen_color[tile_group->num_tiles];
278 if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
282 for (i = 0; i < tile_group->num_tiles - 1; ++i)
284 if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number)
289 seen_color[i] = tile_group->tiles[i].color;
290 for (i = 0; i < tile_group->num_tiles; ++i)
292 seen_color[tile_group->tiles[i].color]++;
293 if (seen_color[tile_group->tiles[i].color] > 1)
301 static void deck_deal(game_t *game, deck_t *deck)
307 printf ("How many players(1-4) should I deal in? ");
308 game->num_players = getchar();
309 if (game->num_players == EOF)
311 printf ("\nGoodbye.\n");
315 game->num_players -= '0';
317 for (i = 0; i < game->num_players; ++i)
319 for (j = 0; j < 14; ++j)
321 rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
322 temp = deck->tiles[rand_tile];
323 deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
324 game->players[i].hand.tiles[j] = temp;
325 deck->num_tiles -= 1;
326 game->players[i].hand.num_tiles += 1;
329 printf ("Game dealt for %d player(s)\n", game->num_players);
332 static void deck_init(deck_t *deck)
336 for (h = 0; h <= 1; ++h)
338 for (i = 0; i <= 3; ++i)
340 for (j = 0; j <= 12; ++j)
342 tile_init (&deck->tiles[deck->num_tiles++], i, j);
343 printf ("There are %d tiles in the deck\n", deck->num_tiles);
349 static void deck_shuffle(deck_t *deck)
354 for (last = deck->num_tiles; last > 0; --last)
356 rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
357 temp = deck->tiles[rand_tile];
358 deck->tiles[rand_tile] = deck->tiles[last - 1];
359 deck->tiles[last - 1] = temp;
363 static void deck_print(deck_t *deck)
366 for (h = 0; h < 2; ++h)
368 for (i = 0; i < 4; ++i)
370 for (j = 0; j < 13; ++j)
372 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
376 printf ("There are %d tiles in the deck\n" , deck->num_tiles);
379 static void deck_spread(deck_t *deck)
382 for (i = 0; i < 8; i++)
384 for (j = 0; j < 13; j++)
386 deck->tiles[j + (i * 13)].x = j * 50;
387 deck->tiles[j + (i * 13)].y = i * 60;
392 static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
395 for (i = 0; i < game->deck.num_tiles; i++)
397 tile_draw(game, &game->deck.tiles[i], cr, region);
401 static void hand_print(game_t *game, int player)
404 for (i = 0; i < game->players[player].hand.num_tiles; i++)
406 tile_print(game->players[player].hand.tiles[i]);
410 static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
413 int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
414 int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
415 for (i = 0; i < game->players[player].hand.num_tiles; i++)
417 tile_set_x_y(&game->players[player].hand.tiles[i],
418 ((gwdw / game->players[player].hand.num_tiles)) * i,
419 (gwdh - TILE_HEIGHT - 6) );
421 for (i = 0; i < game->players[player].hand.num_tiles; i++)
423 tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
427 static void game_init(game_t *game)
430 GError *error = NULL;
432 game->num_players = 0;
434 for (i = 0; i < GAME_MAX_PLAYERS; ++i)
436 player_init(&game->players[i]);
437 game->num_players += 1;
440 board_init(&game->board);
441 deck_init(&game->deck);
442 deck_shuffle(&game->deck);
444 game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
446 FATAL_ERROR (error->message);
448 game->current_tile = game->deck.num_tiles - 1;
449 game->diff_x = game->diff_y = 0;
452 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
456 cr = gdk_cairo_create (widget->window);
458 deck_draw(game, cr, event->region);
460 hand_draw(game, 0, cr, event->region);
467 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
469 printf ("You pressed key %d\n", event->keyval);
474 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
476 int i, tile_x, tile_y;
478 for (i = 0; i < game->deck.num_tiles; i++)
480 tile_x = game->deck.tiles[i].x;
481 tile_y = game->deck.tiles[i].y;
482 if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
483 event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
485 game->current_tile = i;
486 game->diff_x = event->x - tile_x;
487 game->diff_y = event->y - tile_y;
494 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
496 game->current_tile = -1;
497 printf ("You released button %d\n", event->button);
502 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
503 game_t *game, cairo_t *cr)
507 tile = &game->deck.tiles[game->current_tile];
509 /* First, invalidate the region where the tile currently is. */
510 gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
512 /* Then, move the tile */
513 tile->x = event->x - game->diff_x;
514 tile->y = event->y - game->diff_y;
516 /* Finally, invalidate the region where the tile is now. */
517 gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
522 int main(int argc, char *argv[])
529 gtk_init (&argc, &argv);
532 deck_print(&game.deck);
533 deck_spread(&game.deck);
534 deck_deal(&game, &game.deck);
535 //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
536 //deck_print(&game.deck);
538 /* Create a new window */
539 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
540 gtk_window_set_default_size (GTK_WINDOW (window),
541 GAME_WINDOW_DEFAULT_WIDTH,
542 GAME_WINDOW_DEFAULT_HEIGHT);
544 gtk_widget_set_events (window,
547 GDK_BUTTON_MOTION_MASK |
548 GDK_BUTTON_PRESS_MASK |
549 GDK_BUTTON_RELEASE_MASK);
551 g_signal_connect (G_OBJECT (window), "delete_event",
552 G_CALLBACK (gtk_main_quit), NULL);
553 g_signal_connect (G_OBJECT (window), "expose_event",
554 G_CALLBACK (on_expose_event), &game);
555 g_signal_connect (G_OBJECT (window), "key_press_event",
556 G_CALLBACK (on_key_press_event), &game);
557 g_signal_connect (G_OBJECT (window), "button_press_event",
558 G_CALLBACK (on_button_press_event), &game);
559 g_signal_connect (G_OBJECT (window), "button_release_event",
560 G_CALLBACK (on_button_release_event), &game);
561 g_signal_connect (G_OBJECT (window), "motion_notify_event",
562 G_CALLBACK (on_button_motion_event), &game);
565 gtk_widget_show_all (window);