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;
59 #define DECK_MAX_TILES 104
62 tile_t tiles[DECK_MAX_TILES];
66 #define TILE_GROUP_MAX_TILES DECK_MAX_TILES
68 typedef struct tile_group {
69 tile_t tiles[TILE_GROUP_MAX_TILES];
73 #define BOARD_MAX_TILE_GROUPS (DECK_MAX_TILES / 3)
75 typedef struct board {
76 tile_group_t groups[BOARD_MAX_TILE_GROUPS];
80 typedef struct player {
84 typedef struct selection_box {
85 int x1, x2, y1, y2, visible;
88 #define GAME_MAX_PLAYERS 4
89 #define GAME_WINDOW_DEFAULT_WIDTH 800
90 #define GAME_WINDOW_DEFAULT_HEIGHT 600
93 player_t players[GAME_MAX_PLAYERS];
97 selection_box_t selection_box;
98 RsvgHandle *blanktile;
103 int click_x, click_y;
104 int release_x, release_y; /*Currently unused*/
107 static void selection_box_init(selection_box_t *box)
116 static void selection_box_draw(selection_box_t *box, cairo_t *cr)
118 cairo_rectangle (cr, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1);
119 cairo_set_source_rgba(cr, 0.0, 0.1, 0.2, 0.5);
123 static void tile_init (tile_t *tile, color_t color, int number)
126 tile->number = number;
131 static void tile_set_x_y (tile_t *tile, int x, int y)
137 static void tile_print(tile_t tile)
139 printf("%6s %2d\n", colors[tile.color], tile.number + 1);
142 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region)
144 char number_string[3];
146 GdkRectangle rectangle;
148 rectangle.x = tile->x - 1;
149 rectangle.y = tile->y - 1;
150 rectangle.width = TILE_WIDTH + 2;
151 rectangle.height = TILE_HEIGHT + 2;
152 if (gdk_region_rect_in (region, &rectangle) == GDK_OVERLAP_RECTANGLE_OUT)
155 len = snprintf (number_string, 3, "%d", tile->number + 1);
156 if (len < 0 || len >= 3)
157 FATAL_ERROR ("snprintf failed");
160 cairo_translate(cr, tile->x, tile->y);
161 rsvg_handle_render_cairo (game->blanktile, cr);
163 if (tile->color == BLACK)
164 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
165 if (tile->color == BLUE)
166 cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
167 if (tile->color == RED)
168 cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
169 if (tile->color == YELLOW)
170 cairo_set_source_rgb (cr, 1.0, .843, 0.0);
171 if (tile->number + 1 > 9)
172 cairo_move_to (cr, 1, 25);
174 cairo_move_to (cr, 10, 25);
175 cairo_set_font_size(cr, 25);
176 cairo_show_text (cr, number_string);
181 static void tile_group_init(tile_group_t *tile_group)
183 tile_group->num_tiles = 0;
186 static void board_init(board_t *board)
189 board->num_groups = 0;
191 for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
193 tile_group_init(&board->groups[i]);
197 static void player_init(player_t *player)
199 tile_group_init(&player->hand);
203 /* If tile_one < tile_two, then return value will be negative
204 if they are equal, 0 will be returned,
205 if tile_one > tile_two, then return value will be positive */
206 static int tile_compare(const void *one, const void *two)
208 const tile_t *tile_one = one;
209 const tile_t *tile_two = two;
210 return tile_one->number - tile_two->number;
213 static int tile_group_is_run_one(tile_group_t *tile_group)
216 qsort (&tile_group->tiles[0], tile_group->num_tiles,
217 sizeof (tile_t), tile_compare);
219 if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
221 printf("fail run - invalid num tiles; ");
224 for (i = 0; i < tile_group->num_tiles - 1; ++i)
226 if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
228 printf("fail run - colors don't match; ");
231 if( tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1 &&
232 i+1 != tile_group->num_tiles)
234 printf("fail run - invalid number sequence; ");
242 static int tile_group_is_run_two(tile_group_t *tile_group)
245 int lowest = 14, highest = 0;
248 /* By definition, a run must have at least 3 tiles. Also, it's
249 * impossible for any group of tiles with more than 13 tiles to be
250 * a run, (there are only 13 unique numbers so a group with more
251 * than 13 tiles must have some duplicates).
253 if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
258 /* Loop through all tiles in the group, ensuring that they are all
259 * the same color and finding the highest and lowest number in the
261 run_color = tile_group->tiles[0].color;
263 for (i = 0; i < tile_group->num_tiles; i++)
265 if (tile_group->tiles[i].color != run_color)
267 if (tile_group->tiles[i].number > highest)
269 highest = tile_group->tiles[i].number;
271 if (tile_group->tiles[i].number < lowest)
273 lowest = tile_group->tiles[i].number;
277 /* For a run, the difference between the highest and lowest tiles
278 * will always be one less than the number of tiles in the
279 * group. If not then we know it's not a run.
281 if (highest - lowest != tile_group->num_tiles - 1)
286 /* XXX: There's a bug here. We're guessing that at this point
287 * anything we're looking at must be a run. This would be correct
288 * if there were no duplicate tiles, but since there are
289 * duplicates this us quite broken. For example consider two
290 * sequences of entirely red tiles:
292 * This is a run: 1, 2, 3, 4
293 * But this is not: 1, 3, 4, 4
295 * As currently written, this function will consider both of these
296 * groups to be a run. One possible fix is to throw away the
297 * highest - lowest heuristic and instead simply sort the tiles up
298 * front and ensure the difference between each adjacent pair is
304 static int tile_group_is_set(tile_group_t *tile_group)
307 color_t seen_color[4];
308 for (i = 0; i < 4; i++)
311 if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
313 printf("fail set - invalid num tiles; ");
316 for (i = 0; i <= tile_group->num_tiles - 1; ++i)
318 if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number &&
319 i+1 != tile_group->num_tiles)
321 printf("fail set - numbers don't match; ");
324 seen_color[tile_group->tiles[i].color] += 1;
326 for (i = 0; i < 4; i++)
328 if (seen_color[i] > 1)
330 printf("fail set - repeat color; ");
337 static void deck_deal(game_t *game, deck_t *deck)
343 printf ("How many players(1-4) should I deal in? ");
344 game->num_players = getchar();
345 if (game->num_players == EOF)
347 printf ("\nGoodbye.\n");
351 game->num_players -= '0';
353 for (i = 0; i < game->num_players; ++i)
355 for (j = 0; j < 14; ++j)
357 rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
358 temp = deck->tiles[rand_tile];
359 deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
360 game->players[i].hand.tiles[j] = temp;
361 deck->num_tiles -= 1;
362 game->players[i].hand.num_tiles += 1;
365 printf ("Game dealt for %d player(s)\n", game->num_players);
368 static void deck_init(deck_t *deck)
372 for (h = 0; h <= 1; ++h)
374 for (i = 0; i <= 3; ++i)
376 for (j = 0; j <= 12; ++j)
378 tile_init (&deck->tiles[deck->num_tiles++], i, j);
379 printf ("There are %d tiles in the deck\n", deck->num_tiles);
385 static void deck_shuffle(deck_t *deck)
390 for (last = deck->num_tiles; last > 0; --last)
392 rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
393 temp = deck->tiles[rand_tile];
394 deck->tiles[rand_tile] = deck->tiles[last - 1];
395 deck->tiles[last - 1] = temp;
399 static void deck_print(deck_t *deck)
402 for (h = 0; h < 2; ++h)
404 for (i = 0; i < 4; ++i)
406 for (j = 0; j < 13; ++j)
408 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
412 printf ("There are %d tiles in the deck\n" , deck->num_tiles);
415 static void deck_spread(deck_t *deck)
418 for (i = 0; i < 8; i++)
420 for (j = 0; j < 13; j++)
422 deck->tiles[j + (i * 13)].x = j * 50;
423 deck->tiles[j + (i * 13)].y = i * 60;
428 static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
431 for (i = 0; i < game->deck.num_tiles; i++)
433 tile_draw(game, &game->deck.tiles[i], cr, region);
437 static void hand_print(game_t *game, int player)
440 for (i = 0; i < game->players[player].hand.num_tiles; i++)
442 tile_print(game->players[player].hand.tiles[i]);
446 static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
449 int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
450 int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
451 for (i = 0; i < game->players[player].hand.num_tiles; i++)
453 tile_set_x_y(&game->players[player].hand.tiles[i],
454 ((gwdw / game->players[player].hand.num_tiles)) * i,
455 (gwdh - TILE_HEIGHT - 6) );
457 for (i = 0; i < game->players[player].hand.num_tiles; i++)
459 tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
463 static void game_init(game_t *game)
466 GError *error = NULL;
468 game->num_players = 0;
470 for (i = 0; i < GAME_MAX_PLAYERS; ++i)
472 player_init(&game->players[i]);
473 game->num_players += 1;
476 selection_box_init(&game->selection_box);
477 board_init(&game->board);
478 deck_init(&game->deck);
479 deck_shuffle(&game->deck);
481 game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
483 FATAL_ERROR (error->message);
485 game->current_tile = game->deck.num_tiles - 1;
486 game->select_mode = 1;
488 game->diff_x = game->diff_y = 0;
491 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
495 cr = gdk_cairo_create (widget->window);
497 deck_draw(game, cr, event->region);
499 if (game->selection_box.visible)
500 selection_box_draw(&game->selection_box, cr);
502 hand_draw(game, 0, cr, event->region);
509 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
511 printf ("You pressed key %d\n", event->keyval);
516 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
518 int i, tile_x, tile_y;
520 for (i = 0; i < game->deck.num_tiles; i++)
522 tile_x = game->deck.tiles[i].x;
523 tile_y = game->deck.tiles[i].y;
524 if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
525 event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
527 game->select_mode = 0;
528 game->current_tile = i;
529 game->diff_x = event->x - tile_x;
530 game->diff_y = event->y - tile_y;
533 if (game->select_mode)
535 game->selection_box.visible = 1;
536 /*These next two lines are likely to be replaced by...*/
537 game->click_x = event->x;
538 game->click_y = event->y;
539 /*...these two lines*/
540 game->selection_box.x1 = event->x;
541 game->selection_box.y1 = event->y;
546 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
548 if (game->select_mode)
550 game->select_mode = 0;
551 selection_box_t *box;
552 box = &game->selection_box;
554 int x_min = MIN(box->x1, box->x2);
555 int x_max = MAX(box->x1, box->x2);
556 int y_min = MIN(box->y1, box->y2);
557 int y_max = MAX(box->y1, box->y2);
558 int width = abs(box->x2 - box->x1);
559 int height = abs(box->y2 - box->y1);
562 gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
567 int i, tile_x1, tile_y1, tile_x2, tile_y2;
568 for (i = 0; i < game->deck.num_tiles; i++)
570 tile_x1 = game->deck.tiles[i].x;
571 tile_y1 = game->deck.tiles[i].y;
572 tile_x2 = tile_x + TILE_WIDTH;
573 tile_y2 = tile_y + TILE_HEIGHT;
574 if (/*If top-left corner*/
575 (tile_x1 >= x_min && tile_x1 <= x_max &&
576 tile_y1 >= y_min && tile_y1 <= y_max) ||
577 /*or bottom-right corner*/
578 (tile_x2 >= x_min && tile_x2 <= x_max &&
579 tile_y2 >= y_min && tile_y2 <= y_max) ||
580 /*or bottom-left corner*/
581 (tile_x1 >= x_min && tile_x1 <= x_max &&
582 tile_y2 >= y_min && tile_y2 <= y_max) ||
583 /*or top-right corner of tile selected*/
584 (tile_x2 >= x_min && tile_x2 <= x_max &&
585 tile_y1 >= y_min && tile_y1 <= y_max) )
587 group.tiles[group.num_tiles] = game->deck.tiles[i];
591 printf("is run %d\n", tile_group_is_run_one(&group) );
592 printf("is set %d\n", tile_group_is_set(&group) );
593 for (i = 0; i < group.num_tiles; i++)
594 tile_print(group.tiles[i]);
597 game->select_mode = 1;
602 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
603 game_t *game, cairo_t *cr)
605 if (game->select_mode)
607 selection_box_t *box;
608 box = &game->selection_box;
611 gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2),
612 MIN(box->y1, box->y2), abs(box->x2 - box->x1),
613 abs(box->y2 - box->y1) );
618 gtk_widget_queue_draw_area ( widget, MIN(box->x1, box->x2),
619 MIN(box->y1, box->y2), abs(box->x2 - box->x1),
620 abs(box->y2 - box->y1) );
625 tile = &game->deck.tiles[game->current_tile];
627 /* First, invalidate the region where the tile currently is. */
628 gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH +
631 /* Then, move the tile */
632 tile->x = event->x - game->diff_x;
633 tile->y = event->y - game->diff_y;
635 /* Finally, invalidate the region where the tile is now. */
636 gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
641 int main(int argc, char *argv[])
648 gtk_init (&argc, &argv);
651 deck_print(&game.deck);
652 deck_spread(&game.deck);
653 deck_deal(&game, &game.deck);
654 //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
655 //deck_print(&game.deck);
657 /* Create a new window */
658 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
659 gtk_window_set_default_size (GTK_WINDOW (window),
660 GAME_WINDOW_DEFAULT_WIDTH,
661 GAME_WINDOW_DEFAULT_HEIGHT);
663 gtk_widget_set_events (window,
666 GDK_BUTTON_MOTION_MASK |
667 GDK_BUTTON_PRESS_MASK |
668 GDK_BUTTON_RELEASE_MASK);
670 g_signal_connect (G_OBJECT (window), "delete_event",
671 G_CALLBACK (gtk_main_quit), NULL);
672 g_signal_connect (G_OBJECT (window), "expose_event",
673 G_CALLBACK (on_expose_event), &game);
674 g_signal_connect (G_OBJECT (window), "key_press_event",
675 G_CALLBACK (on_key_press_event), &game);
676 g_signal_connect (G_OBJECT (window), "button_press_event",
677 G_CALLBACK (on_button_press_event), &game);
678 g_signal_connect (G_OBJECT (window), "button_release_event",
679 G_CALLBACK (on_button_release_event), &game);
680 g_signal_connect (G_OBJECT (window), "motion_notify_event",
681 G_CALLBACK (on_button_motion_event), &game);
684 gtk_widget_show_all (window);