]> git.cworth.org Git - kub/blob - kub.c
Neatly rearrange tile group after selected.
[kub] / kub.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <gtk/gtk.h>
5 #include <librsvg/rsvg.h>
6 #include <librsvg/rsvg-cairo.h>
7
8 /*
9  *   TILE_NUMBER_X_OFFSET = 3
10  * //
11  * || TILE_NUMBER_WIDTH = 34
12  * ||/   /
13  * ||   |  TILE_WIDTH = 40
14  * ||   | /
15  * |     |
16  * +-----+ - TILE_NUMBER_Y_OFFSET = 3   -
17  * |+---+| -                            |
18  * ||   ||                              |
19  * ||   ||   TILE_NUMBER_HEIGHT = 24    |
20  * |+---+| -                            |
21  * |  _  | _                            |-TILE_HEIGHT = 50
22  * | / \ |                              |
23  * ||   ||   TILE_SUN_HEIGHT = 20       |
24  * | \_/ | _                            |
25  * |     |   TILE_SUN_Y_OFFSET = 3      |
26  * +-----+ -                            -
27  * ||   | TILE_SUN_WIDTH = 20
28  * ||
29  * || TILE_SUN_X_OFFSET = 10
30  */
31 #define TILE_WIDTH              40
32 #define TILE_HEIGHT             50
33
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
38
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
43
44 #define FATAL_ERROR(msg)                                                \
45     do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0)
46
47 char *colors[] = {"Black", "Blue", "Red", "Yellow"};
48
49 typedef enum {BLACK, BLUE, RED, YELLOW} color_t;
50
51 typedef struct tile {
52     color_t color;
53     int number;
54     int x;
55     int y;
56     int selected;
57     int owned;
58 } tile_t;
59
60 #define DECK_MAX_TILES 104
61
62 typedef struct deck {
63     tile_t tiles[DECK_MAX_TILES];
64     int num_tiles;
65 } deck_t;
66
67 #define TILE_GROUP_MAX_TILES DECK_MAX_TILES
68
69 typedef struct tile_group {
70     tile_t tiles[TILE_GROUP_MAX_TILES];
71     int num_tiles;
72 } tile_group_t;
73
74 #define BOARD_MAX_TILE_GROUPS (DECK_MAX_TILES / 3)
75
76 typedef struct board {
77     tile_group_t groups[BOARD_MAX_TILE_GROUPS];
78     int num_groups;
79 } board_t;
80
81 typedef struct player {
82     tile_group_t hand;
83 } player_t;
84
85 typedef struct selection_box {
86     int x1, x2, y1, y2, visible;
87 } selection_box_t;
88
89 #define GAME_MAX_PLAYERS 4
90 #define GAME_WINDOW_DEFAULT_WIDTH  800
91 #define GAME_WINDOW_DEFAULT_HEIGHT 600
92
93 typedef struct game {
94     player_t players[GAME_MAX_PLAYERS];
95     int num_players;
96     board_t board;
97     deck_t deck;
98     selection_box_t selection_box;
99     RsvgHandle *blanktile;
100     RsvgHandle *selectedtile;
101     RsvgHandle *ownedtile;
102
103     int current_tile;
104     int select_mode;
105     int diff_x, diff_y;
106     int click_x, click_y;
107     int release_x, release_y;    /*Currently unused*/
108 } game_t;
109
110 static void selection_box_init(selection_box_t *box)
111 {
112     box->x1 = 0;
113     box->x2 = 0;
114     box->y1 = 0;
115     box->y2 = 0;
116     box->visible = 0;
117 }
118
119 static void selection_box_draw(selection_box_t *box, cairo_t *cr)
120 {
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);
123     cairo_fill (cr);
124 }
125
126 static void tile_init (tile_t *tile, color_t color, int number)
127 {
128     tile->color = color;
129     tile->number = number;
130     tile->x = 0;
131     tile->y = 0;
132     tile->selected = 0;
133     tile->owned = 0;
134 }
135
136 static void tile_set_x_y (tile_t *tile, int x, int y)
137 {
138     tile->x = x;
139     tile->y = y;
140 }
141
142 static void tile_print(tile_t tile)
143 {
144     printf("%6s %2d\n", colors[tile.color], tile.number + 1);
145 }
146
147 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region)
148 {
149     char number_string[3];
150     int len;
151     GdkRectangle rectangle;
152
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)
158         return;
159
160     len = snprintf (number_string, 3, "%d", tile->number + 1);
161     if (len < 0 || len >= 3)
162         FATAL_ERROR ("snprintf failed");
163
164     cairo_save(cr);
165     cairo_translate(cr, tile->x, tile->y);
166
167     if (tile->selected)
168         rsvg_handle_render_cairo (game->selectedtile, cr);
169     if (tile->owned)
170         rsvg_handle_render_cairo (game->ownedtile, cr); 
171     else
172         rsvg_handle_render_cairo (game->blanktile, cr);
173
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);
184     else
185         cairo_move_to (cr, 10, 25);
186     cairo_set_font_size(cr, 25);
187     cairo_show_text (cr, number_string);
188
189     cairo_restore(cr);
190 }
191
192 static void tile_group_init(tile_group_t *tile_group)
193 {
194     tile_group->num_tiles = 0;
195 }
196
197 static void board_init(board_t *board)
198 {
199     int i;
200     board->num_groups = 0;
201
202     for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
203     {
204         tile_group_init(&board->groups[i]);
205     }
206 }
207
208 static void player_init(player_t *player)
209 {
210     tile_group_init(&player->hand);
211 }
212
213
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)
218 {
219     const tile_t *tile_one = one;
220     const tile_t *tile_two = two;
221     return tile_one->number - tile_two->number;
222 }
223
224 static int tile_group_is_run_one(tile_group_t *tile_group)
225 {
226     int i;
227     qsort (&tile_group->tiles[0], tile_group->num_tiles,
228            sizeof (tile_t), tile_compare);
229
230     if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
231     {
232         printf("fail run - invalid num tiles; ");
233         return 0;
234     }
235     for (i = 0; i < tile_group->num_tiles - 1; ++i)
236     {
237         if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
238         {
239             printf("fail run - colors don't match; ");
240             return 0;
241         }
242         if( tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1 &&
243             i+1 != tile_group->num_tiles)
244         {
245             printf("fail run - invalid number sequence; ");
246             return 0;
247         }
248     }
249     return 1;
250 }
251
252
253 static int tile_group_is_run_two(tile_group_t *tile_group)
254 {
255     int i;
256     int lowest = 14, highest = 0;
257     color_t run_color;
258
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).
263      */
264     if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
265     {
266         return 0;
267     }
268
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
271      * group. */
272     run_color = tile_group->tiles[0].color;
273
274     for (i = 0; i < tile_group->num_tiles; i++)
275     {
276         if (tile_group->tiles[i].color != run_color)
277             return 0;
278         if (tile_group->tiles[i].number > highest)
279         {
280             highest = tile_group->tiles[i].number;
281         }
282         if (tile_group->tiles[i].number < lowest)
283         {
284             lowest = tile_group->tiles[i].number;
285         }
286     }
287
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.
291      */
292     if (highest - lowest != tile_group->num_tiles - 1)
293     {
294         return 0;
295     }
296
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:
302      *
303      * This is a run:   1, 2, 3, 4
304      * But this is not: 1, 3, 4, 4
305      *
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
310      * exactly 1.
311      */
312     return 1;
313 }
314
315 static int tile_group_is_set(tile_group_t *tile_group)
316 {
317     int i;
318     color_t seen_color[4];
319     for (i = 0; i < 4; i++)
320         seen_color[i] = 0;
321
322     if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
323     {
324         printf("fail set - invalid num tiles; ");
325         return 0;
326     }
327     for (i = 0; i <= tile_group->num_tiles - 1; ++i)
328     {
329         if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number &&
330             i+1 != tile_group->num_tiles)
331         {
332             printf("fail set - numbers don't match; ");
333             return 0;
334         }
335         seen_color[tile_group->tiles[i].color] += 1;
336     }
337     for (i = 0; i < 4; i++)
338     {
339         if (seen_color[i] > 1)
340         {
341             printf("fail set - repeat color; ");
342             return 0;
343         }
344     }
345     return 1;
346 }
347
348 static void deck_deal(game_t *game, deck_t *deck)
349 {
350     tile_t temp;
351     int rand_tile;
352     int i, j, newline;
353
354     printf ("How many players(1-4) should I deal in? ");
355     game->num_players = getchar();
356     if (game->num_players == EOF)
357     {
358         printf ("\nGoodbye.\n");
359         exit (1);
360     }
361     newline = getchar();
362     game->num_players -= '0';
363
364     for (i = 0; i < game->num_players; ++i)
365     {
366         for (j = 0; j < 14; ++j)
367         {
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;
375         }
376     }
377     printf ("Game dealt for %d player(s)\n", game->num_players);
378 }
379
380 static void deck_init(deck_t *deck)
381 {
382     int h, i, j;
383     deck->num_tiles = 0;
384     for (h = 0; h <= 1; ++h)
385     {
386         for (i = 0; i <= 3; ++i)
387         {
388             for (j = 0; j <= 12; ++j)
389             {
390                 tile_init (&deck->tiles[deck->num_tiles++], i, j);
391                 printf ("There are %d tiles in the deck\n", deck->num_tiles);
392             }
393         }
394     }
395 }
396
397 static void deck_shuffle(deck_t *deck)
398 {
399     tile_t temp;
400     int rand_tile;
401     int last;
402     for (last = deck->num_tiles; last > 0; --last)
403     {
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;
408     }
409 }
410
411 static void deck_print(deck_t *deck)
412 {
413     int h, i, j;
414     for (h = 0; h < 2; ++h)
415     {
416         for (i = 0; i < 4; ++i)
417         {
418             for (j = 0; j < 13; ++j)
419             {
420                 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
421             }
422         }
423     }
424     printf ("There are %d tiles in the deck\n" , deck->num_tiles);
425 }
426
427 static void deck_spread(deck_t *deck)
428 {
429     int i, j;
430     for (i = 0; i < 8; i++)
431     {
432         for (j = 0; j < 13; j++)
433         {
434             deck->tiles[j + (i * 13)].x = j * 50;
435             deck->tiles[j + (i * 13)].y = i * 60;
436         }
437     }
438 }
439
440 static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
441 {
442     int i;
443     for (i = 0; i < game->deck.num_tiles; i++)
444     {
445         tile_draw(game, &game->deck.tiles[i], cr, region);
446     }
447 }
448
449 static void hand_print(game_t *game, int player)
450 {
451     int i;
452     for (i = 0; i < game->players[player].hand.num_tiles; i++)
453     {
454         tile_print(game->players[player].hand.tiles[i]);
455     }
456 }
457
458 static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
459 {
460     int i;
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++)
464     {
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) );
468     }
469     for (i = 0; i < game->players[player].hand.num_tiles; i++)
470     {
471         tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
472     }
473 }
474
475 static void game_init(game_t *game)
476 {
477     int i;
478     GError *error = NULL;
479
480     game->num_players = 0;
481
482     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
483     {
484         player_init(&game->players[i]);
485         game->num_players += 1;
486     }
487     
488     selection_box_init(&game->selection_box);
489     board_init(&game->board);
490     deck_init(&game->deck);
491     deck_shuffle(&game->deck);
492
493     game->selectedtile = rsvg_handle_new_from_file ("tiles/selected_tile.svg", &error);
494     if (error)
495         FATAL_ERROR (error->message);
496
497     game->ownedtile = rsvg_handle_new_from_file ("tiles/owned_tile.svg", &error);
498     if (error)
499         FATAL_ERROR (error->message);
500
501     game->blanktile = rsvg_handle_new_from_file ("tiles/blank_tile.svg", &error);
502     if (error)
503         FATAL_ERROR (error->message);
504
505     game->current_tile = game->deck.num_tiles - 1;
506     game->select_mode = 1;
507
508     game->diff_x = game->diff_y = 0;
509 }
510
511 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
512 {
513     cairo_t *cr;
514
515     cr = gdk_cairo_create (widget->window);
516
517     deck_draw(game, cr, event->region);
518
519     if (game->selection_box.visible)
520         selection_box_draw(&game->selection_box, cr);
521
522     hand_draw(game, 0, cr, event->region);
523
524     cairo_destroy (cr);
525
526     return TRUE;
527 }
528
529 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
530 {
531     printf ("You pressed key %d\n", event->keyval);
532
533     return TRUE;
534 }
535
536 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
537 {
538     int i, tile_x, tile_y;
539     tile_t *curr_tile = &game->deck.tiles[game->current_tile];    
540
541     for (i = 0; i < game->deck.num_tiles; i++)
542     {
543         curr_tile = &game->deck.tiles[i];
544         if (curr_tile->selected)
545         {
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);
548         }
549
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) )
554         {
555             game->select_mode = 0;
556  
557             game->current_tile = i;
558             curr_tile = &game->deck.tiles[game->current_tile];
559             if (!curr_tile->selected)
560                 curr_tile->selected = 1;
561             else
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);
564
565             game->diff_x = event->x - tile_x;
566             game->diff_y = event->y - tile_y;
567         }
568     }
569     if (game->select_mode)
570     {
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);
573
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;
583     }
584     return TRUE;
585 }
586
587 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
588 {
589     if (game->select_mode)
590     {
591         game->select_mode = 0;
592         selection_box_t *box;
593         box = &game->selection_box;
594
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);
601
602         box->visible = 0;       
603         gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
604
605 //      tile_group_t group;
606 //      group.num_tiles = 0;
607
608         tile_t* group[TILE_GROUP_MAX_TILES];
609         int num_tiles = 0;
610
611         int i, tile_x, tile_y, tile_x2, tile_y2;
612         for (i = 0; i < game->deck.num_tiles; i++)
613         {
614             tile_x = game->deck.tiles[i].x;
615             tile_y = game->deck.tiles[i].y;
616             tile_x2 = tile_x + TILE_WIDTH;
617             tile_y2 = tile_y + TILE_HEIGHT;
618             if (/*If top-left corner*/
619                 (tile_x >= x_min && tile_x <= x_max &&
620                  tile_y >= y_min && tile_y <= y_max) ||
621                 /*or bottom-right corner*/
622                 (tile_x2 >= x_min && tile_x2 <= x_max &&
623                  tile_y2 >= y_min && tile_y2 <= y_max) ||
624                 /*or bottom-left corner*/
625                 (tile_x >= x_min && tile_x <= x_max &&
626                  tile_y2 >= y_min && tile_y2 <= y_max) ||
627                 /*or top-right corner of tile selected*/
628                 (tile_x2 >= x_min && tile_x2 <= x_max &&
629                  tile_y >= y_min && tile_y <= y_max) ||
630                 /*or left edge*/
631                 (y_min >= tile_y && y_min <= tile_y2 &&
632                  x_min <= tile_x && x_max >= tile_x) ||
633                 /*or top edge*/
634                 (x_min >= tile_x && x_min <= tile_x2 &&
635                  y_min <= tile_y && y_max >= tile_y) ||
636                 /*or right edge*/
637                 (y_min >= tile_y && y_min <= tile_y2 &&
638                  x_min >= tile_x && x_min <= tile_x2) ||
639                 /*or bottom edge*/
640                 (x_min >= tile_x && x_min <= tile_x2 &&
641                  y_min >= tile_y && y_min <= tile_y) )
642             {           
643 //              group.tiles[group.num_tiles] = game->deck.tiles[i];
644 //              group.num_tiles++;
645
646                 group[num_tiles] = &game->deck.tiles[i];;
647                 num_tiles++;
648             }
649         }
650         //printf("is run %d\n", tile_group_is_run_one(&group) );
651         //printf("is set %d\n", tile_group_is_set(&group) );
652
653 //      int matching_y = y_min;
654         
655 //      for (i = 0; i < group.num_tiles; i++)
656         for (i = 0; i < num_tiles; i++)
657         {
658 //          tile_print(group.tiles[i]);
659             gtk_widget_queue_draw_area (widget, group[i]->x - 1 , group[i]->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
660             group[i]->x = x_min + (i * (TILE_WIDTH));
661             group[i]->y = y_min;
662             gtk_widget_queue_draw_area (widget, group[i]->x - 1 , group[i]->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
663         }    
664 /*      int j;
665         int new_x = 0;
666         for (i = 0; i < group.num_tiles; i++)
667         {
668             for (j = 0; j < game->deck.num_tiles; j++)
669             {
670                 if (group.tiles[i].x == game->deck.tiles[j].x &&
671                     group.tiles[i].y == game->deck.tiles[j].y)
672                 {
673                     new_x = x_min + (i * (TILE_WIDTH));
674                     gtk_widget_queue_draw_area (widget, game->deck.tiles[j].x - 1 , game->deck.tiles[j].y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
675                     game->deck.tiles[j].x = new_x;
676                     game->deck.tiles[j].y = matching_y;
677                     gtk_widget_queue_draw_area (widget, new_x - 1 , matching_y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
678                 }
679             }
680         }*/
681     }    
682     game->select_mode = 1;
683
684     return TRUE;
685 }
686
687 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
688                                         game_t *game, cairo_t *cr)
689 {
690     if (game->select_mode)
691     {
692         selection_box_t *box;
693         box = &game->selection_box;
694         box->visible = 1;
695
696         int x_min = MIN(box->x1, box->x2);
697         int x_max = MAX(box->x1, box->x2);
698         int y_min = MIN(box->y1, box->y2);
699         int y_max = MAX(box->y1, box->y2);
700         int width = abs(box->x2 - box->x1);
701         int height = abs(box->y2 - box->y1);
702
703         gtk_widget_queue_draw_area ( widget,  x_min, y_min, width, height );
704
705         box->x2 = event->x;
706         box->y2 = event->y;
707
708         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) );
709
710         int i, tile_x, tile_y, tile_x2, tile_y2;
711         tile_t *curr_tile;
712         for (i = 0; i < game->deck.num_tiles; i++)
713         {
714             curr_tile = &game->deck.tiles[i];
715             if (curr_tile->selected)
716             {
717                 curr_tile->selected = 0;
718                 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
719             }
720
721             tile_x = game->deck.tiles[i].x;
722             tile_y = game->deck.tiles[i].y;
723             tile_x2 = tile_x + TILE_WIDTH;
724             tile_y2 = tile_y + TILE_HEIGHT;
725             if (/*If top-left corner*/
726                 (tile_x >= x_min && tile_x <= x_max &&
727                  tile_y >= y_min && tile_y <= y_max) ||
728                 /*or bottom-right corner*/
729                 (tile_x2 >= x_min && tile_x2 <= x_max &&
730                  tile_y2 >= y_min && tile_y2 <= y_max) ||
731                 /*or bottom-left corner*/
732                 (tile_x >= x_min && tile_x <= x_max &&
733                  tile_y2 >= y_min && tile_y2 <= y_max) ||
734                 /*or top-right corner of tile selected*/
735                 (tile_x2 >= x_min && tile_x2 <= x_max &&
736                  tile_y >= y_min && tile_y <= y_max) ||
737                 /*or left edge*/
738                 (y_min >= tile_y && y_min <= tile_y2 &&
739                  x_min <= tile_x && x_max >= tile_x) ||
740                 /*or top edge*/
741                 (x_min >= tile_x && x_min <= tile_x2 &&
742                  y_min <= tile_y && y_max >= tile_y) ||
743                 /*or right edge*/
744                 (y_min >= tile_y && y_min <= tile_y2 &&
745                  x_min >= tile_x && x_min <= tile_x2) ||
746                 /*or bottom edge*/
747                 (x_min >= tile_x && x_min <= tile_x2 &&
748                  y_min >= tile_y && y_min <= tile_y) )
749             {
750                 curr_tile->selected = 1;
751                 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
752             }
753 /*
754             else
755             {
756                 curr_tile->selected = 0;
757                 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
758                 }*/
759         }
760     }
761     else
762     {
763         tile_t *tile;
764         tile = &game->deck.tiles[game->current_tile];
765
766         /* First, invalidate the region where the tile currently is. */
767         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
768
769     /* Then, move the tile */
770         tile->x = event->x - game->diff_x;
771         tile->y = event->y - game->diff_y;
772
773     /* Finally, invalidate the region where the tile is now. */
774         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
775     }
776     return TRUE;
777 }
778
779 int main(int argc, char *argv[])
780 {
781     GtkWidget *window;
782     game_t game;
783
784     srand(time(NULL));
785
786     gtk_init (&argc, &argv);
787
788     game_init(&game);
789     deck_print(&game.deck);
790     deck_spread(&game.deck);
791     deck_deal(&game, &game.deck);
792     //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
793     //deck_print(&game.deck);
794
795     /* Create a new window */
796     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
797     gtk_window_set_default_size (GTK_WINDOW (window),
798                                  GAME_WINDOW_DEFAULT_WIDTH,
799                                  GAME_WINDOW_DEFAULT_HEIGHT);
800
801     gtk_widget_set_events (window,
802                            GDK_EXPOSURE_MASK |
803                            GDK_KEY_PRESS_MASK |
804                            GDK_BUTTON_MOTION_MASK |
805                            GDK_BUTTON_PRESS_MASK |
806                            GDK_BUTTON_RELEASE_MASK);
807
808     g_signal_connect (G_OBJECT (window), "delete_event",
809                       G_CALLBACK (gtk_main_quit), NULL);
810     g_signal_connect (G_OBJECT (window), "expose_event",
811                       G_CALLBACK (on_expose_event), &game);
812     g_signal_connect (G_OBJECT (window), "key_press_event",
813                       G_CALLBACK (on_key_press_event), &game);
814     g_signal_connect (G_OBJECT (window), "button_press_event",
815                       G_CALLBACK (on_button_press_event), &game);
816     g_signal_connect (G_OBJECT (window), "button_release_event",
817                       G_CALLBACK (on_button_release_event), &game);
818     g_signal_connect (G_OBJECT (window), "motion_notify_event",
819                       G_CALLBACK (on_button_motion_event), &game);
820
821
822     gtk_widget_show_all (window);
823     gtk_main ();
824
825     return 0;
826
827 }