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;
60 #define DECK_MAX_TILES 104
63 tile_t tiles[DECK_MAX_TILES];
67 #define TILE_GROUP_MAX_TILES DECK_MAX_TILES
69 typedef struct tile_group {
70 tile_t tiles[TILE_GROUP_MAX_TILES];
74 #define BOARD_MAX_TILE_GROUPS (DECK_MAX_TILES / 3)
76 typedef struct board {
77 tile_group_t groups[BOARD_MAX_TILE_GROUPS];
81 typedef struct player {
85 typedef struct selection_box {
86 int x1, x2, y1, y2, visible;
89 #define GAME_MAX_PLAYERS 4
90 #define GAME_WINDOW_DEFAULT_WIDTH 800
91 #define GAME_WINDOW_DEFAULT_HEIGHT 600
94 player_t players[GAME_MAX_PLAYERS];
98 selection_box_t selection_box;
99 RsvgHandle *blanktile;
100 RsvgHandle *selectedtile;
101 RsvgHandle *ownedtile;
106 int click_x, click_y;
107 int release_x, release_y; /*Currently unused*/
110 static void selection_box_init(selection_box_t *box)
119 static void selection_box_draw(selection_box_t *box, cairo_t *cr)
121 cairo_rectangle (cr, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1);
122 cairo_set_source_rgba(cr, 0.0, 0.1, 0.2, 0.5);
126 static void tile_init (tile_t *tile, color_t color, int number)
129 tile->number = number;
136 static void tile_set_x_y (tile_t *tile, int x, int y)
142 static void tile_print(tile_t tile)
144 printf("%6s %2d\n", colors[tile.color], tile.number + 1);
147 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region)
149 char number_string[3];
151 GdkRectangle rectangle;
153 rectangle.x = tile->x - 1;
154 rectangle.y = tile->y - 1;
155 rectangle.width = TILE_WIDTH + 2;
156 rectangle.height = TILE_HEIGHT + 2;
157 if (gdk_region_rect_in (region, &rectangle) == GDK_OVERLAP_RECTANGLE_OUT)
160 len = snprintf (number_string, 3, "%d", tile->number + 1);
161 if (len < 0 || len >= 3)
162 FATAL_ERROR ("snprintf failed");
165 cairo_translate(cr, tile->x, tile->y);
168 rsvg_handle_render_cairo (game->selectedtile, cr);
170 rsvg_handle_render_cairo (game->ownedtile, cr);
172 rsvg_handle_render_cairo (game->blanktile, cr);
174 if (tile->color == BLACK)
175 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
176 if (tile->color == BLUE)
177 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
178 if (tile->color == RED)
179 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
180 if (tile->color == YELLOW)
181 cairo_set_source_rgb (cr, 1.0, .843, 0.0);
182 if (tile->number + 1 > 9)
183 cairo_move_to (cr, 1, 25);
185 cairo_move_to (cr, 10, 25);
186 cairo_set_font_size(cr, 25);
187 cairo_show_text (cr, number_string);
192 static void tile_group_init(tile_group_t *tile_group)
194 tile_group->num_tiles = 0;
197 static void board_init(board_t *board)
200 board->num_groups = 0;
202 for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
204 tile_group_init(&board->groups[i]);
208 static void player_init(player_t *player)
210 tile_group_init(&player->hand);
214 /* If tile_one < tile_two, then return value will be negative
215 if they are equal, 0 will be returned,
216 if tile_one > tile_two, then return value will be positive */
217 static int tile_compare(const void *one, const void *two)
219 const tile_t *tile_one = one;
220 const tile_t *tile_two = two;
221 return tile_one->number - tile_two->number;
224 static int tile_group_is_run_one(tile_group_t *tile_group)
227 qsort (&tile_group->tiles[0], tile_group->num_tiles,
228 sizeof (tile_t), tile_compare);
230 if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
232 printf("fail run - invalid num tiles; ");
235 for (i = 0; i < tile_group->num_tiles - 1; ++i)
237 if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
239 printf("fail run - colors don't match; ");
242 if( tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1 &&
243 i+1 != tile_group->num_tiles)
245 printf("fail run - invalid number sequence; ");
253 static int tile_group_is_run_two(tile_group_t *tile_group)
256 int lowest = 14, highest = 0;
259 /* By definition, a run must have at least 3 tiles. Also, it's
260 * impossible for any group of tiles with more than 13 tiles to be
261 * a run, (there are only 13 unique numbers so a group with more
262 * than 13 tiles must have some duplicates).
264 if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
269 /* Loop through all tiles in the group, ensuring that they are all
270 * the same color and finding the highest and lowest number in the
272 run_color = tile_group->tiles[0].color;
274 for (i = 0; i < tile_group->num_tiles; i++)
276 if (tile_group->tiles[i].color != run_color)
278 if (tile_group->tiles[i].number > highest)
280 highest = tile_group->tiles[i].number;
282 if (tile_group->tiles[i].number < lowest)
284 lowest = tile_group->tiles[i].number;
288 /* For a run, the difference between the highest and lowest tiles
289 * will always be one less than the number of tiles in the
290 * group. If not then we know it's not a run.
292 if (highest - lowest != tile_group->num_tiles - 1)
297 /* XXX: There's a bug here. We're guessing that at this point
298 * anything we're looking at must be a run. This would be correct
299 * if there were no duplicate tiles, but since there are
300 * duplicates this us quite broken. For example consider two
301 * sequences of entirely red tiles:
303 * This is a run: 1, 2, 3, 4
304 * But this is not: 1, 3, 4, 4
306 * As currently written, this function will consider both of these
307 * groups to be a run. One possible fix is to throw away the
308 * highest - lowest heuristic and instead simply sort the tiles up
309 * front and ensure the difference between each adjacent pair is
315 static int tile_group_is_set(tile_group_t *tile_group)
318 color_t seen_color[4];
319 for (i = 0; i < 4; i++)
322 if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
324 printf("fail set - invalid num tiles; ");
327 for (i = 0; i <= tile_group->num_tiles - 1; ++i)
329 if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number &&
330 i+1 != tile_group->num_tiles)
332 printf("fail set - numbers don't match; ");
335 seen_color[tile_group->tiles[i].color] += 1;
337 for (i = 0; i < 4; i++)
339 if (seen_color[i] > 1)
341 printf("fail set - repeat color; ");
348 static void deck_deal(game_t *game, deck_t *deck)
354 printf ("How many players(1-4) should I deal in? ");
355 game->num_players = getchar();
356 if (game->num_players == EOF)
358 printf ("\nGoodbye.\n");
362 game->num_players -= '0';
364 for (i = 0; i < game->num_players; ++i)
366 for (j = 0; j < 14; ++j)
368 rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
369 temp = deck->tiles[rand_tile];
370 deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
371 game->players[i].hand.tiles[j] = temp;
372 game->players[i].hand.tiles[j].owned = 1;
373 deck->num_tiles -= 1;
374 game->players[i].hand.num_tiles += 1;
377 printf ("Game dealt for %d player(s)\n", game->num_players);
380 static void deck_init(deck_t *deck)
384 for (h = 0; h <= 1; ++h)
386 for (i = 0; i <= 3; ++i)
388 for (j = 0; j <= 12; ++j)
390 tile_init (&deck->tiles[deck->num_tiles++], i, j);
391 printf ("There are %d tiles in the deck\n", deck->num_tiles);
397 static void deck_shuffle(deck_t *deck)
402 for (last = deck->num_tiles; last > 0; --last)
404 rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
405 temp = deck->tiles[rand_tile];
406 deck->tiles[rand_tile] = deck->tiles[last - 1];
407 deck->tiles[last - 1] = temp;
411 static void deck_print(deck_t *deck)
414 for (h = 0; h < 2; ++h)
416 for (i = 0; i < 4; ++i)
418 for (j = 0; j < 13; ++j)
420 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
424 printf ("There are %d tiles in the deck\n" , deck->num_tiles);
427 static void deck_spread(deck_t *deck)
430 for (i = 0; i < 8; i++)
432 for (j = 0; j < 13; j++)
434 deck->tiles[j + (i * 13)].x = j * 50;
435 deck->tiles[j + (i * 13)].y = i * 60;
440 static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
443 for (i = 0; i < game->deck.num_tiles; i++)
445 tile_draw(game, &game->deck.tiles[i], cr, region);
449 static void hand_print(game_t *game, int player)
452 for (i = 0; i < game->players[player].hand.num_tiles; i++)
454 tile_print(game->players[player].hand.tiles[i]);
458 static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
461 int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
462 int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
463 for (i = 0; i < game->players[player].hand.num_tiles; i++)
465 tile_set_x_y(&game->players[player].hand.tiles[i],
466 ((gwdw / game->players[player].hand.num_tiles)) * i,
467 (gwdh - TILE_HEIGHT - 6) );
469 for (i = 0; i < game->players[player].hand.num_tiles; i++)
471 tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
475 static void game_init(game_t *game)
478 GError *error = NULL;
480 game->num_players = 0;
482 for (i = 0; i < GAME_MAX_PLAYERS; ++i)
484 player_init(&game->players[i]);
485 game->num_players += 1;
488 selection_box_init(&game->selection_box);
489 board_init(&game->board);
490 deck_init(&game->deck);
491 deck_shuffle(&game->deck);
493 game->selectedtile = rsvg_handle_new_from_file ("tiles/selected_tile.svg", &error);
495 FATAL_ERROR (error->message);
497 game->ownedtile = rsvg_handle_new_from_file ("tiles/owned_tile.svg", &error);
499 FATAL_ERROR (error->message);
501 game->blanktile = rsvg_handle_new_from_file ("tiles/blank_tile.svg", &error);
503 FATAL_ERROR (error->message);
505 game->current_tile = game->deck.num_tiles - 1;
506 game->select_mode = 1;
508 game->diff_x = game->diff_y = 0;
511 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
515 cr = gdk_cairo_create (widget->window);
517 deck_draw(game, cr, event->region);
519 if (game->selection_box.visible)
520 selection_box_draw(&game->selection_box, cr);
522 hand_draw(game, 0, cr, event->region);
529 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
531 printf ("You pressed key %d\n", event->keyval);
536 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
538 int i, tile_x, tile_y;
539 tile_t *curr_tile = &game->deck.tiles[game->current_tile];
541 for (i = 0; i < game->deck.num_tiles; i++)
543 curr_tile = &game->deck.tiles[i];
544 if (curr_tile->selected)
546 curr_tile->selected = 0;
547 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
550 tile_x = game->deck.tiles[i].x;
551 tile_y = game->deck.tiles[i].y;
552 if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
553 event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
555 game->select_mode = 0;
557 game->current_tile = i;
558 curr_tile = &game->deck.tiles[game->current_tile];
559 if (!curr_tile->selected)
560 curr_tile->selected = 1;
562 curr_tile->selected = 0;
563 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
565 game->diff_x = event->x - tile_x;
566 game->diff_y = event->y - tile_y;
569 if (game->select_mode)
571 game->deck.tiles[game->current_tile].selected = 0;
572 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
574 game->selection_box.visible = 1;
575 /*These next two lines are likely to be replaced by...*/
576 game->click_x = event->x;
577 game->click_y = event->y;
578 /*...these two lines*/
579 game->selection_box.x1 = event->x;
580 game->selection_box.x2 = event->x;
581 game->selection_box.y1 = event->y;
582 game->selection_box.y2 = event->y;
587 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
589 if (game->select_mode)
591 game->select_mode = 0;
592 selection_box_t *box;
593 box = &game->selection_box;
595 int x_min = MIN(box->x1, box->x2);
596 int x_max = MAX(box->x1, box->x2);
597 int y_min = MIN(box->y1, box->y2);
598 int y_max = MAX(box->y1, box->y2);
599 int width = abs(box->x2 - box->x1);
600 int height = abs(box->y2 - box->y1);
603 gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
608 int i, tile_x, tile_y, tile_x2, tile_y2;
609 for (i = 0; i < game->deck.num_tiles; i++)
611 tile_x = game->deck.tiles[i].x;
612 tile_y = game->deck.tiles[i].y;
613 tile_x2 = tile_x + TILE_WIDTH;
614 tile_y2 = tile_y + TILE_HEIGHT;
615 if (/*If top-left corner*/
616 (tile_x >= x_min && tile_x <= x_max &&
617 tile_y >= y_min && tile_y <= y_max) ||
618 /*or bottom-right corner*/
619 (tile_x2 >= x_min && tile_x2 <= x_max &&
620 tile_y2 >= y_min && tile_y2 <= y_max) ||
621 /*or bottom-left corner*/
622 (tile_x >= x_min && tile_x <= x_max &&
623 tile_y2 >= y_min && tile_y2 <= y_max) ||
624 /*or top-right corner of tile selected*/
625 (tile_x2 >= x_min && tile_x2 <= x_max &&
626 tile_y >= y_min && tile_y <= y_max) ||
628 (y_min >= tile_y && y_min <= tile_y2 &&
629 x_min <= tile_x && x_max >= tile_x) ||
631 (x_min >= tile_x && x_min <= tile_x2 &&
632 y_min <= tile_y && y_max >= tile_y) ||
634 (y_min >= tile_y && y_min <= tile_y2 &&
635 x_min >= tile_x && x_min <= tile_x2) ||
637 (x_min >= tile_x && x_min <= tile_x2 &&
638 y_min >= tile_y && y_min <= tile_y) )
640 group.tiles[group.num_tiles] = game->deck.tiles[i];
644 printf("is run %d\n", tile_group_is_run_one(&group) );
645 printf("is set %d\n", tile_group_is_set(&group) );
646 for (i = 0; i < group.num_tiles; i++)
647 tile_print(group.tiles[i]);
649 game->select_mode = 1;
654 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
655 game_t *game, cairo_t *cr)
657 if (game->select_mode)
659 selection_box_t *box;
660 box = &game->selection_box;
663 int x_min = MIN(box->x1, box->x2);
664 int x_max = MAX(box->x1, box->x2);
665 int y_min = MIN(box->y1, box->y2);
666 int y_max = MAX(box->y1, box->y2);
667 int width = abs(box->x2 - box->x1);
668 int height = abs(box->y2 - box->y1);
670 gtk_widget_queue_draw_area ( widget, x_min, y_min, width, height );
675 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) );
677 int i, tile_x, tile_y, tile_x2, tile_y2;
679 for (i = 0; i < game->deck.num_tiles; i++)
681 curr_tile = &game->deck.tiles[i];
682 if (curr_tile->selected)
684 curr_tile->selected = 0;
685 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
688 tile_x = game->deck.tiles[i].x;
689 tile_y = game->deck.tiles[i].y;
690 tile_x2 = tile_x + TILE_WIDTH;
691 tile_y2 = tile_y + TILE_HEIGHT;
692 if (/*If top-left corner*/
693 (tile_x >= x_min && tile_x <= x_max &&
694 tile_y >= y_min && tile_y <= y_max) ||
695 /*or bottom-right corner*/
696 (tile_x2 >= x_min && tile_x2 <= x_max &&
697 tile_y2 >= y_min && tile_y2 <= y_max) ||
698 /*or bottom-left corner*/
699 (tile_x >= x_min && tile_x <= x_max &&
700 tile_y2 >= y_min && tile_y2 <= y_max) ||
701 /*or top-right corner of tile selected*/
702 (tile_x2 >= x_min && tile_x2 <= x_max &&
703 tile_y >= y_min && tile_y <= y_max) ||
705 (y_min >= tile_y && y_min <= tile_y2 &&
706 x_min <= tile_x && x_max >= tile_x) ||
708 (x_min >= tile_x && x_min <= tile_x2 &&
709 y_min <= tile_y && y_max >= tile_y) ||
711 (y_min >= tile_y && y_min <= tile_y2 &&
712 x_min >= tile_x && x_min <= tile_x2) ||
714 (x_min >= tile_x && x_min <= tile_x2 &&
715 y_min >= tile_y && y_min <= tile_y) )
717 curr_tile->selected = 1;
718 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
723 curr_tile->selected = 0;
724 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
731 tile = &game->deck.tiles[game->current_tile];
733 /* First, invalidate the region where the tile currently is. */
734 gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
736 /* Then, move the tile */
737 tile->x = event->x - game->diff_x;
738 tile->y = event->y - game->diff_y;
740 /* Finally, invalidate the region where the tile is now. */
741 gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
746 int main(int argc, char *argv[])
753 gtk_init (&argc, &argv);
756 deck_print(&game.deck);
757 deck_spread(&game.deck);
758 deck_deal(&game, &game.deck);
759 //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
760 //deck_print(&game.deck);
762 /* Create a new window */
763 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
764 gtk_window_set_default_size (GTK_WINDOW (window),
765 GAME_WINDOW_DEFAULT_WIDTH,
766 GAME_WINDOW_DEFAULT_HEIGHT);
768 gtk_widget_set_events (window,
771 GDK_BUTTON_MOTION_MASK |
772 GDK_BUTTON_PRESS_MASK |
773 GDK_BUTTON_RELEASE_MASK);
775 g_signal_connect (G_OBJECT (window), "delete_event",
776 G_CALLBACK (gtk_main_quit), NULL);
777 g_signal_connect (G_OBJECT (window), "expose_event",
778 G_CALLBACK (on_expose_event), &game);
779 g_signal_connect (G_OBJECT (window), "key_press_event",
780 G_CALLBACK (on_key_press_event), &game);
781 g_signal_connect (G_OBJECT (window), "button_press_event",
782 G_CALLBACK (on_button_press_event), &game);
783 g_signal_connect (G_OBJECT (window), "button_release_event",
784 G_CALLBACK (on_button_release_event), &game);
785 g_signal_connect (G_OBJECT (window), "motion_notify_event",
786 G_CALLBACK (on_button_motion_event), &game);
789 gtk_widget_show_all (window);