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 typedef struct selection_box {
84 int x1, x2, y1, y2, visible;
87 #define GAME_MAX_PLAYERS 4
88 #define GAME_WINDOW_DEFAULT_WIDTH 800
89 #define GAME_WINDOW_DEFAULT_HEIGHT 600
92 player_t players[GAME_MAX_PLAYERS];
96 selection_box_t selection_box;
97 RsvgHandle *blanktile;
102 int click_x, click_y;
103 int release_x, release_y; /*Currently unused*/
106 static void selection_box_init(selection_box_t *box)
115 static void selection_box_draw(selection_box_t *box, cairo_t *cr)
117 cairo_rectangle (cr, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1);
118 cairo_set_source_rgba(cr, 0.0, 0.1, 0.2, 0.5);
122 static void tile_init (tile_t *tile, color_t color, int number)
125 tile->number = number;
130 static void tile_set_x_y (tile_t *tile, int x, int y)
136 static void tile_print(tile_t tile)
138 printf("%6s %2d\n", colors[tile.color], tile.number + 1);
141 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region)
143 char number_string[3];
145 GdkRectangle rectangle;
147 rectangle.x = tile->x - 1;
148 rectangle.y = tile->y - 1;
149 rectangle.width = TILE_WIDTH + 2;
150 rectangle.height = TILE_HEIGHT + 2;
151 if (gdk_region_rect_in (region, &rectangle) == GDK_OVERLAP_RECTANGLE_OUT)
154 len = snprintf (number_string, 3, "%d", tile->number + 1);
155 if (len < 0 || len >= 3)
156 FATAL_ERROR ("snprintf failed");
159 cairo_translate(cr, tile->x, tile->y);
160 rsvg_handle_render_cairo (game->blanktile, cr);
162 if (tile->color == BLACK)
163 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
164 if (tile->color == BLUE)
165 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
166 if (tile->color == RED)
167 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
168 if (tile->color == YELLOW)
169 cairo_set_source_rgb (cr, 1.0, .843, 0.0);
170 if (tile->number + 1 > 9)
171 cairo_move_to (cr, 1, 25);
173 cairo_move_to (cr, 10, 25);
174 cairo_set_font_size(cr, 25);
175 cairo_show_text (cr, number_string);
180 static void tile_group_init(tile_group_t *tile_group)
182 tile_group->num_tiles = 0;
185 static void board_init(board_t *board)
188 board->num_groups = 0;
190 for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
192 tile_group_init(&board->groups[i]);
196 static void player_init(player_t *player)
198 tile_group_init(&player->hand);
202 /* If tile_one < tile_two, then return value will be negative
203 if they are equal, 0 will be returned,
204 if tile_one > tile_two, then return value will be positive */
205 static int tile_compare(const void *one, const void *two)
207 const tile_t *tile_one = one;
208 const tile_t *tile_two = two;
209 return tile_one->number - tile_two->number;
212 static int tile_group_is_run_one(tile_group_t *tile_group)
215 qsort (&tile_group->tiles[0], tile_group->num_tiles,
216 sizeof (tile_t), tile_compare);
218 if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
220 printf("fail run - invalid num tiles; ");
223 for (i = 0; i < tile_group->num_tiles - 1; ++i)
225 if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
227 printf("fail run - colors don't match; ");
230 if( tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1 &&
231 i+1 != tile_group->num_tiles)
233 printf("fail run - invalid number sequence; ");
241 static int tile_group_is_run_two(tile_group_t *tile_group)
244 int lowest = 14, highest = 0;
247 /* By definition, a run must have at least 3 tiles. Also, it's
248 * impossible for any group of tiles with more than 13 tiles to be
249 * a run, (there are only 13 unique numbers so a group with more
250 * than 13 tiles must have some duplicates).
252 if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
257 /* Loop through all tiles in the group, ensuring that they are all
258 * the same color and finding the highest and lowest number in the
260 run_color = tile_group->tiles[0].color;
262 for (i = 0; i < tile_group->num_tiles; i++)
264 if (tile_group->tiles[i].color != run_color)
266 if (tile_group->tiles[i].number > highest)
268 highest = tile_group->tiles[i].number;
270 if (tile_group->tiles[i].number < lowest)
272 lowest = tile_group->tiles[i].number;
276 /* For a run, the difference between the highest and lowest tiles
277 * will always be one less than the number of tiles in the
278 * group. If not then we know it's not a run.
280 if (highest - lowest != tile_group->num_tiles - 1)
285 /* XXX: There's a bug here. We're guessing that at this point
286 * anything we're looking at must be a run. This would be correct
287 * if there were no duplicate tiles, but since there are
288 * duplicates this us quite broken. For example consider two
289 * sequences of entirely red tiles:
291 * This is a run: 1, 2, 3, 4
292 * But this is not: 1, 3, 4, 4
294 * As currently written, this function will consider both of these
295 * groups to be a run. One possible fix is to throw away the
296 * highest - lowest heuristic and instead simply sort the tiles up
297 * front and ensure the difference between each adjacent pair is
303 static int tile_group_is_set(tile_group_t *tile_group)
306 color_t seen_color[4];
307 for (i = 0; i < 4; i++)
310 if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
312 printf("fail set - invalid num tiles; ");
315 for (i = 0; i <= tile_group->num_tiles - 1; ++i)
317 if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number &&
318 i+1 != tile_group->num_tiles)
320 printf("fail set - numbers don't match; ");
323 seen_color[tile_group->tiles[i].color] += 1;
325 for (i = 0; i < 4; i++)
327 if (seen_color[i] > 1)
329 printf("fail set - repeat color; ");
336 static void deck_deal(game_t *game, deck_t *deck)
342 printf ("How many players(1-4) should I deal in? ");
343 game->num_players = getchar();
344 if (game->num_players == EOF)
346 printf ("\nGoodbye.\n");
350 game->num_players -= '0';
352 for (i = 0; i < game->num_players; ++i)
354 for (j = 0; j < 14; ++j)
356 rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
357 temp = deck->tiles[rand_tile];
358 deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
359 game->players[i].hand.tiles[j] = temp;
360 deck->num_tiles -= 1;
361 game->players[i].hand.num_tiles += 1;
364 printf ("Game dealt for %d player(s)\n", game->num_players);
367 static void deck_init(deck_t *deck)
371 for (h = 0; h <= 1; ++h)
373 for (i = 0; i <= 3; ++i)
375 for (j = 0; j <= 12; ++j)
377 tile_init (&deck->tiles[deck->num_tiles++], i, j);
378 printf ("There are %d tiles in the deck\n", deck->num_tiles);
384 static void deck_shuffle(deck_t *deck)
389 for (last = deck->num_tiles; last > 0; --last)
391 rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
392 temp = deck->tiles[rand_tile];
393 deck->tiles[rand_tile] = deck->tiles[last - 1];
394 deck->tiles[last - 1] = temp;
398 static void deck_print(deck_t *deck)
401 for (h = 0; h < 2; ++h)
403 for (i = 0; i < 4; ++i)
405 for (j = 0; j < 13; ++j)
407 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
411 printf ("There are %d tiles in the deck\n" , deck->num_tiles);
414 static void deck_spread(deck_t *deck)
417 for (i = 0; i < 8; i++)
419 for (j = 0; j < 13; j++)
421 deck->tiles[j + (i * 13)].x = j * 50;
422 deck->tiles[j + (i * 13)].y = i * 60;
427 static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
430 for (i = 0; i < game->deck.num_tiles; i++)
432 tile_draw(game, &game->deck.tiles[i], cr, region);
436 static void hand_print(game_t *game, int player)
439 for (i = 0; i < game->players[player].hand.num_tiles; i++)
441 tile_print(game->players[player].hand.tiles[i]);
445 static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
448 int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
449 int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
450 for (i = 0; i < game->players[player].hand.num_tiles; i++)
452 tile_set_x_y(&game->players[player].hand.tiles[i],
453 ((gwdw / game->players[player].hand.num_tiles)) * i,
454 (gwdh - TILE_HEIGHT - 6) );
456 for (i = 0; i < game->players[player].hand.num_tiles; i++)
458 tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
462 static void game_init(game_t *game)
465 GError *error = NULL;
467 game->num_players = 0;
469 for (i = 0; i < GAME_MAX_PLAYERS; ++i)
471 player_init(&game->players[i]);
472 game->num_players += 1;
475 selection_box_init(&game->selection_box);
476 board_init(&game->board);
477 deck_init(&game->deck);
478 deck_shuffle(&game->deck);
480 game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
482 FATAL_ERROR (error->message);
484 game->current_tile = game->deck.num_tiles - 1;
485 game->select_mode = 1;
487 game->diff_x = game->diff_y = 0;
490 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
494 cr = gdk_cairo_create (widget->window);
496 deck_draw(game, cr, event->region);
498 if (game->selection_box.visible)
499 selection_box_draw(&game->selection_box, cr);
501 hand_draw(game, 0, cr, event->region);
508 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
510 printf ("You pressed key %d\n", event->keyval);
515 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
517 int i, tile_x, tile_y;
519 for (i = 0; i < game->deck.num_tiles; i++)
521 tile_x = game->deck.tiles[i].x;
522 tile_y = game->deck.tiles[i].y;
523 if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
524 event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
526 game->current_tile = i;
527 game->diff_x = event->x - tile_x;
528 game->diff_y = event->y - tile_y;
531 if (game->select_mode)
533 game->selection_box.visible = 1;
534 /*These next two lines are likely to be replaced by...*/
535 game->click_x = event->x;
536 game->click_y = event->y;
537 /*...these two lines*/
538 game->selection_box.x1 = event->x;
539 game->selection_box.y1 = event->y;
544 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
546 if (game->select_mode)
548 game->select_mode = 0;
549 selection_box_t *box;
550 box = &game->selection_box;
552 int x = MIN(box->x1, box->x2);
553 int y = MIN(box->y1, box->y2);
554 int width = abs(box->x2 - box->x1);
555 int height = abs(box->y2 - box->y1);
558 gtk_widget_queue_draw_area (widget, x, y, width, height);
563 int i, tile_x, tile_y;
564 for (i = 0; i < game->deck.num_tiles; i++)
566 tile_x = game->deck.tiles[i].x;
567 tile_y = game->deck.tiles[i].y;
568 if ( (event->x >= tile_x && game->click_x <= tile_x &&
569 event->y >= tile_y && game->click_y <= tile_y) ||
570 (event->x >= tile_x && game->click_x <= tile_x &&
571 event->y <= (tile_y + TILE_HEIGHT) && game->click_y >= tile_y) )
573 group.tiles[group.num_tiles] = game->deck.tiles[i];
577 printf("is run %d\n", tile_group_is_run_one(&group) );
578 printf("is set %d\n", tile_group_is_set(&group) );
579 for (i = 0; i < group.num_tiles; i++)
580 tile_print(group.tiles[i]);
583 game->select_mode = 1;
588 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
589 game_t *game, cairo_t *cr)
591 if (game->select_mode)
593 selection_box_t *box;
594 box = &game->selection_box;
597 gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2), MIN(box->y1, box->y2), abs(box->x2 - box->x1), abs(box->y2 - box->y1) );
602 gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2), MIN(box->y1, box->y2), abs(box->x2 - box->x1), abs(box->y2 - box->y1) );
607 tile = &game->deck.tiles[game->current_tile];
609 /* First, invalidate the region where the tile currently is. */
610 gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
612 /* Then, move the tile */
613 tile->x = event->x - game->diff_x;
614 tile->y = event->y - game->diff_y;
616 /* Finally, invalidate the region where the tile is now. */
617 gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
622 int main(int argc, char *argv[])
629 gtk_init (&argc, &argv);
632 deck_print(&game.deck);
633 deck_spread(&game.deck);
634 deck_deal(&game, &game.deck);
635 //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
636 //deck_print(&game.deck);
638 /* Create a new window */
639 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
640 gtk_window_set_default_size (GTK_WINDOW (window),
641 GAME_WINDOW_DEFAULT_WIDTH,
642 GAME_WINDOW_DEFAULT_HEIGHT);
644 gtk_widget_set_events (window,
647 GDK_BUTTON_MOTION_MASK |
648 GDK_BUTTON_PRESS_MASK |
649 GDK_BUTTON_RELEASE_MASK);
651 g_signal_connect (G_OBJECT (window), "delete_event",
652 G_CALLBACK (gtk_main_quit), NULL);
653 g_signal_connect (G_OBJECT (window), "expose_event",
654 G_CALLBACK (on_expose_event), &game);
655 g_signal_connect (G_OBJECT (window), "key_press_event",
656 G_CALLBACK (on_key_press_event), &game);
657 g_signal_connect (G_OBJECT (window), "button_press_event",
658 G_CALLBACK (on_button_press_event), &game);
659 g_signal_connect (G_OBJECT (window), "button_release_event",
660 G_CALLBACK (on_button_release_event), &game);
661 g_signal_connect (G_OBJECT (window), "motion_notify_event",
662 G_CALLBACK (on_button_motion_event), &game);
665 gtk_widget_show_all (window);