]> git.cworth.org Git - kub/blob - kub.c
Save state and Restore state
[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     tile_t *current_tile;
104 //    int current_tile;
105     int select_mode;
106     int drag_group_mode;
107     int diff_x, diff_y;
108     int click_x, click_y;
109     int release_x, release_y;    /*Currently unused*/
110 } game_t;
111
112 static void selection_box_init(selection_box_t *box)
113 {
114     box->x1 = 0;
115     box->x2 = 0;
116     box->y1 = 0;
117     box->y2 = 0;
118     box->visible = 0;
119 }
120
121 static void selection_box_draw(selection_box_t *box, cairo_t *cr)
122 {
123     cairo_rectangle (cr, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1);
124     cairo_set_source_rgba(cr, 0.0, 0.1, 0.2, 0.5);
125     cairo_fill (cr);
126 }
127
128 static void tile_init (tile_t *tile, color_t color, int number)
129 {
130     tile->color = color;
131     tile->number = number;
132     tile->x = 0;
133     tile->y = 0;
134     tile->selected = 0;
135     tile->owned = 0;
136 }
137
138 static void tile_set_x_y (tile_t *tile, int x, int y)
139 {
140     tile->x = x;
141     tile->y = y;
142 }
143
144 static void tile_print(tile_t tile)
145 {
146     printf("%6s %2d\n", colors[tile.color], tile.number + 1);
147 }
148
149 static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region)
150 {
151     char number_string[3];
152     int len;
153     GdkRectangle rectangle;
154
155     rectangle.x = tile->x - 1;
156     rectangle.y = tile->y - 1;
157     rectangle.width = TILE_WIDTH + 2;
158     rectangle.height = TILE_HEIGHT + 2;
159     if (gdk_region_rect_in (region, &rectangle) == GDK_OVERLAP_RECTANGLE_OUT)
160         return;
161
162     len = snprintf (number_string, 3, "%d", tile->number + 1);
163     if (len < 0 || len >= 3)
164         FATAL_ERROR ("snprintf failed");
165
166     cairo_save(cr);
167     cairo_translate(cr, tile->x, tile->y);
168
169     if (tile->selected)
170         rsvg_handle_render_cairo (game->selectedtile, cr);
171     if (tile->owned)
172         rsvg_handle_render_cairo (game->ownedtile, cr); 
173     else
174         rsvg_handle_render_cairo (game->blanktile, cr);
175
176     if (tile->color == BLACK)
177         cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
178     if (tile->color == BLUE)
179         cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
180     if (tile->color == RED)
181         cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
182     if (tile->color == YELLOW)
183         cairo_set_source_rgb (cr, 1.0, .843, 0.0);
184     if (tile->number + 1 > 9)
185         cairo_move_to (cr, 1, 25);
186     else
187         cairo_move_to (cr, 10, 25);
188     cairo_set_font_size(cr, 25);
189     cairo_show_text (cr, number_string);
190
191     cairo_restore(cr);
192 }
193
194 static void tile_group_init(tile_group_t *tile_group)
195 {
196     tile_group->num_tiles = 0;
197 }
198
199 static void board_init(board_t *board)
200 {
201     int i;
202     board->num_groups = 0;
203
204     for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
205     {
206         tile_group_init(&board->groups[i]);
207     }
208 }
209
210 static void player_init(player_t *player)
211 {
212     tile_group_init(&player->hand);
213 }
214
215
216 /* If tile_one < tile_two, then return value will be negative
217    if they are equal, 0 will be returned,
218    if tile_one > tile_two, then return value will be positive */
219 static int tile_compare(const void *one, const void *two)
220 {
221     const tile_t *tile_one = one;
222     const tile_t *tile_two = two;
223     return tile_one->number - tile_two->number;
224 }
225
226 static int tile_group_is_run_one(tile_group_t *tile_group)
227 {
228     int i;
229     qsort (&tile_group->tiles[0], tile_group->num_tiles,
230            sizeof (tile_t), tile_compare);
231
232     if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
233     {
234         printf("fail run - invalid num tiles; ");
235         return 0;
236     }
237     for (i = 0; i < tile_group->num_tiles - 1; ++i)
238     {
239         if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
240         {
241             printf("fail run - colors don't match; ");
242             return 0;
243         }
244         if( tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1 &&
245             i+1 != tile_group->num_tiles)
246         {
247             printf("fail run - invalid number sequence; ");
248             return 0;
249         }
250     }
251     return 1;
252 }
253
254
255 static int tile_group_is_run_two(tile_group_t *tile_group)
256 {
257     int i;
258     int lowest = 14, highest = 0;
259     color_t run_color;
260
261     /* By definition, a run must have at least 3 tiles. Also, it's
262      * impossible for any group of tiles with more than 13 tiles to be
263      * a run, (there are only 13 unique numbers so a group with more
264      * than 13 tiles must have some duplicates).
265      */
266     if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13)
267     {
268         return 0;
269     }
270
271     /* Loop through all tiles in the group, ensuring that they are all
272      * the same color and finding the highest and lowest number in the
273      * group. */
274     run_color = tile_group->tiles[0].color;
275
276     for (i = 0; i < tile_group->num_tiles; i++)
277     {
278         if (tile_group->tiles[i].color != run_color)
279             return 0;
280         if (tile_group->tiles[i].number > highest)
281         {
282             highest = tile_group->tiles[i].number;
283         }
284         if (tile_group->tiles[i].number < lowest)
285         {
286             lowest = tile_group->tiles[i].number;
287         }
288     }
289
290     /* For a run, the difference between the highest and lowest tiles
291      * will always be one less than the number of tiles in the
292      * group. If not then we know it's not a run.
293      */
294     if (highest - lowest != tile_group->num_tiles - 1)
295     {
296         return 0;
297     }
298
299     /* XXX: There's a bug here. We're guessing that at this point
300      * anything we're looking at must be a run. This would be correct
301      * if there were no duplicate tiles, but since there are
302      * duplicates this us quite broken. For example consider two
303      * sequences of entirely red tiles:
304      *
305      * This is a run:   1, 2, 3, 4
306      * But this is not: 1, 3, 4, 4
307      *
308      * As currently written, this function will consider both of these
309      * groups to be a run. One possible fix is to throw away the
310      * highest - lowest heuristic and instead simply sort the tiles up
311      * front and ensure the difference between each adjacent pair is
312      * exactly 1.
313      */
314     return 1;
315 }
316
317 static int tile_group_is_set(tile_group_t *tile_group)
318 {
319     int i;
320     color_t seen_color[4];
321     for (i = 0; i < 4; i++)
322         seen_color[i] = 0;
323
324     if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
325     {
326         printf("fail set - invalid num tiles; ");
327         return 0;
328     }
329     for (i = 0; i <= tile_group->num_tiles - 1; ++i)
330     {
331         if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number &&
332             i+1 != tile_group->num_tiles)
333         {
334             printf("fail set - numbers don't match; ");
335             return 0;
336         }
337         seen_color[tile_group->tiles[i].color] += 1;
338     }
339     for (i = 0; i < 4; i++)
340     {
341         if (seen_color[i] > 1)
342         {
343             printf("fail set - repeat color; ");
344             return 0;
345         }
346     }
347     return 1;
348 }
349
350 static void deck_deal(game_t *game, deck_t *deck)
351 {
352     tile_t temp;
353     int rand_tile;
354     int i, j, newline;
355
356     printf ("How many players(1-4) should I deal in? ");
357     game->num_players = getchar();
358     if (game->num_players == EOF)
359     {
360         printf ("\nGoodbye.\n");
361         exit (1);
362     }
363     newline = getchar();
364     game->num_players -= '0';
365
366     for (i = 0; i < game->num_players; ++i)
367     {
368         for (j = 0; j < 14; ++j)
369         {
370             rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0);
371             temp = deck->tiles[rand_tile];
372             deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
373             game->players[i].hand.tiles[j] = temp;
374             game->players[i].hand.tiles[j].owned = 1;
375             deck->num_tiles -= 1;
376             game->players[i].hand.num_tiles += 1;
377         }
378     }
379     printf ("Game dealt for %d player(s)\n", game->num_players);
380 }
381
382 static void deck_init(deck_t *deck)
383 {
384     int h, i, j;
385     deck->num_tiles = 0;
386     for (h = 0; h <= 1; ++h)
387     {
388         for (i = 0; i <= 3; ++i)
389         {
390             for (j = 0; j <= 12; ++j)
391             {
392                 tile_init (&deck->tiles[deck->num_tiles++], i, j);
393                 printf ("There are %d tiles in the deck\n", deck->num_tiles);
394             }
395         }
396     }
397 }
398
399 static void deck_shuffle(deck_t *deck)
400 {
401     tile_t temp;
402     int rand_tile;
403     int last;
404     for (last = deck->num_tiles; last > 0; --last)
405     {
406         rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0);
407         temp = deck->tiles[rand_tile];
408         deck->tiles[rand_tile] = deck->tiles[last - 1];
409         deck->tiles[last - 1] = temp;
410     }
411 }
412
413 static void deck_print(deck_t *deck)
414 {
415     int h, i, j;
416     for (h = 0; h < 2; ++h)
417     {
418         for (i = 0; i < 4; ++i)
419         {
420             for (j = 0; j < 13; ++j)
421             {
422                 tile_print(deck->tiles[j + (i * 13) + (h * 52)]);
423             }
424         }
425     }
426     printf ("There are %d tiles in the deck\n" , deck->num_tiles);
427 }
428
429 static void deck_spread(deck_t *deck)
430 {
431     int i, j;
432     for (i = 0; i < 8; i++)
433     {
434         for (j = 0; j < 13; j++)
435         {
436             deck->tiles[j + (i * 13)].x = j * 50;
437             deck->tiles[j + (i * 13)].y = i * 60;
438         }
439     }
440 }
441
442 static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
443 {
444     int i;
445     for (i = 0; i < game->deck.num_tiles; i++)
446     {
447         tile_draw(game, &game->deck.tiles[i], cr, region);
448     }
449 }
450
451 static void hand_print(game_t *game, int player)
452 {
453     int i;
454     for (i = 0; i < game->players[player].hand.num_tiles; i++)
455     {
456         tile_print(game->players[player].hand.tiles[i]);
457     }
458 }
459
460 static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
461 {
462     int i;
463     int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
464     int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
465     for (i = 0; i < game->players[player].hand.num_tiles; i++)
466     {
467         tile_set_x_y(&game->players[player].hand.tiles[i],
468                      ((gwdw / game->players[player].hand.num_tiles)) * i,
469                      (gwdh - TILE_HEIGHT - 6) );
470     }
471     for (i = 0; i < game->players[player].hand.num_tiles; i++)
472     {
473         tile_draw(game, &game->players[player].hand.tiles[i], cr, region);
474     }
475 }
476
477 static void game_init(game_t *game)
478 {
479     int i;
480     GError *error = NULL;
481
482     game->num_players = 0;
483
484     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
485     {
486         player_init(&game->players[i]);
487         game->num_players += 1;
488     }
489     
490     selection_box_init(&game->selection_box);
491     board_init(&game->board);
492     deck_init(&game->deck);
493     deck_shuffle(&game->deck);
494
495     game->selectedtile = rsvg_handle_new_from_file ("tiles/selected_tile.svg", &error);
496     if (error)
497         FATAL_ERROR (error->message);
498
499     game->ownedtile = rsvg_handle_new_from_file ("tiles/owned_tile.svg", &error);
500     if (error)
501         FATAL_ERROR (error->message);
502
503     game->blanktile = rsvg_handle_new_from_file ("tiles/blank_tile.svg", &error);
504     if (error)
505         FATAL_ERROR (error->message);
506
507 //    game->current_tile = game->deck.num_tiles - 1;
508     game->current_tile = &game->deck.tiles[0];
509     game->select_mode = 1;
510     game->drag_group_mode = 0;
511
512     game->diff_x = game->diff_y = 0;
513 }
514
515 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
516 {
517     cairo_t *cr;
518
519     cr = gdk_cairo_create (widget->window);
520
521     deck_draw(game, cr, event->region);
522
523     if (game->selection_box.visible)
524         selection_box_draw(&game->selection_box, cr);
525
526     hand_draw(game, 0, cr, event->region);
527
528     cairo_destroy (cr);
529
530     return TRUE;
531 }
532
533 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
534 {
535     printf ("You pressed key %d\n", event->keyval);
536     if (event->keyval == 65505 || event->keyval == 65506)
537         game->drag_group_mode = 1;
538
539     return TRUE;
540 }
541
542 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
543 {
544     int i, tile_x, tile_y;
545     tile_t *curr_tile;
546     player_t *curr_player = &game->players[0];
547
548     /*Handle tiles in player's hand */
549     for (i = 0; i < curr_player->hand.num_tiles; i++)
550     {
551         curr_tile = &curr_player->hand.tiles[i];
552         if (curr_tile->selected)
553         {
554             curr_tile->selected = 0;
555             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
556         }
557         tile_x = curr_player->hand.tiles[i].x;
558         tile_y = curr_player->hand.tiles[i].y;
559         if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
560             event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
561         {
562             game->select_mode = 0;
563
564             game->current_tile = curr_tile;
565
566             if (!curr_tile->selected)
567                 curr_tile->selected = 1;
568             else
569                 curr_tile->selected = 0;
570             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
571
572             game->diff_x = event->x - tile_x;
573             game->diff_y = event->y - tile_y;
574         }
575     }
576
577     /*Handle tiles in deck */
578     for (i = 0; i < game->deck.num_tiles; i++)
579     {
580         curr_tile = &game->deck.tiles[i];
581         if (curr_tile->selected)
582         {
583             curr_tile->selected = 0;
584             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
585         }
586
587         tile_x = game->deck.tiles[i].x;
588         tile_y = game->deck.tiles[i].y;
589         if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
590             event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
591         {
592             game->select_mode = 0;
593  
594 //          game->current_tile = i;
595             game->current_tile = curr_tile;
596
597 //delete_this?      curr_tile = &game->deck.tiles[game->current_tile];
598             if (!curr_tile->selected)
599                 curr_tile->selected = 1;
600             else
601                 curr_tile->selected = 0;
602             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
603
604             game->diff_x = event->x - tile_x;
605             game->diff_y = event->y - tile_y;
606         }
607     }
608     if (game->select_mode)
609     {
610 //      game->deck.tiles[game->current_tile].selected = 0;
611         game->current_tile->selected = 0;
612         gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
613
614         game->selection_box.visible = 1;
615         /*These next two lines appear to be dead
616         game->click_x = event->x;
617         game->click_y = event->y;*/
618
619         game->selection_box.x1 = event->x;
620         game->selection_box.x2 = event->x;
621         game->selection_box.y1 = event->y;
622         game->selection_box.y2 = event->y;
623     }
624     return TRUE;
625 }
626
627 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
628 {
629     if (game->select_mode)
630     {
631         game->select_mode = 0;
632         selection_box_t *box;
633         box = &game->selection_box;
634
635         int x_min = MIN(box->x1, box->x2);
636         int x_max = MAX(box->x1, box->x2);
637         int y_min = MIN(box->y1, box->y2);
638         int y_max = MAX(box->y1, box->y2);
639         int width = abs(box->x2 - box->x1);
640         int height = abs(box->y2 - box->y1);
641
642         box->visible = 0;       
643         gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
644
645 //      tile_group_t group;
646 //      group.num_tiles = 0;
647
648         tile_t* group[TILE_GROUP_MAX_TILES];
649         int num_tiles = 0;
650
651         int i, tile_x, tile_y, tile_x2, tile_y2;
652         for (i = 0; i < game->deck.num_tiles; i++)
653         {
654             tile_x = game->deck.tiles[i].x;
655             tile_y = game->deck.tiles[i].y;
656             tile_x2 = tile_x + TILE_WIDTH;
657             tile_y2 = tile_y + TILE_HEIGHT;
658             if (/*If top-left corner*/
659                 (tile_x >= x_min && tile_x <= x_max &&
660                  tile_y >= y_min && tile_y <= y_max) ||
661                 /*or bottom-right corner*/
662                 (tile_x2 >= x_min && tile_x2 <= x_max &&
663                  tile_y2 >= y_min && tile_y2 <= y_max) ||
664                 /*or bottom-left corner*/
665                 (tile_x >= x_min && tile_x <= x_max &&
666                  tile_y2 >= y_min && tile_y2 <= y_max) ||
667                 /*or top-right corner of tile selected*/
668                 (tile_x2 >= x_min && tile_x2 <= x_max &&
669                  tile_y >= y_min && tile_y <= y_max) ||
670                 /*or left edge*/
671                 (y_min >= tile_y && y_min <= tile_y2 &&
672                  x_min <= tile_x && x_max >= tile_x) ||
673                 /*or top edge*/
674                 (x_min >= tile_x && x_min <= tile_x2 &&
675                  y_min <= tile_y && y_max >= tile_y) ||
676                 /*or right edge*/
677                 (y_min >= tile_y && y_min <= tile_y2 &&
678                  x_min >= tile_x && x_min <= tile_x2) ||
679                 /*or bottom edge*/
680                 (x_min >= tile_x && x_min <= tile_x2 &&
681                  y_min >= tile_y && y_min <= tile_y) )
682             {           
683 //              group.tiles[group.num_tiles] = game->deck.tiles[i];
684 //              group.num_tiles++;
685
686                 group[num_tiles] = &game->deck.tiles[i];;
687                 num_tiles++;
688             }
689         }
690         //printf("is run %d\n", tile_group_is_run_one(&group) );
691         //printf("is set %d\n", tile_group_is_set(&group) );
692
693 //      int matching_y = y_min;
694         
695 //      for (i = 0; i < group.num_tiles; i++)
696         for (i = 0; i < num_tiles; i++)
697         {
698 //          tile_print(group.tiles[i]);
699             gtk_widget_queue_draw_area (widget, group[i]->x - 1 , group[i]->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
700             group[i]->x = x_min + (i * (TILE_WIDTH));
701             group[i]->y = y_min;
702             gtk_widget_queue_draw_area (widget, group[i]->x - 1 , group[i]->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
703         }    
704 /*      int j;
705         int new_x = 0;
706         for (i = 0; i < group.num_tiles; i++)
707         {
708             for (j = 0; j < game->deck.num_tiles; j++)
709             {
710                 if (group.tiles[i].x == game->deck.tiles[j].x &&
711                     group.tiles[i].y == game->deck.tiles[j].y)
712                 {
713                     new_x = x_min + (i * (TILE_WIDTH));
714                     gtk_widget_queue_draw_area (widget, game->deck.tiles[j].x - 1 , game->deck.tiles[j].y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
715                     game->deck.tiles[j].x = new_x;
716                     game->deck.tiles[j].y = matching_y;
717                     gtk_widget_queue_draw_area (widget, new_x - 1 , matching_y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
718                 }
719             }
720         }*/
721     }    
722     game->select_mode = 1;
723
724     return TRUE;
725 }
726
727 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
728                                         game_t *game, cairo_t *cr)
729 {
730     if (game->select_mode)
731     {
732         selection_box_t *box;
733         box = &game->selection_box;
734         box->visible = 1;
735
736         int x_min = MIN(box->x1, box->x2);
737         int x_max = MAX(box->x1, box->x2);
738         int y_min = MIN(box->y1, box->y2);
739         int y_max = MAX(box->y1, box->y2);
740         int width = abs(box->x2 - box->x1);
741         int height = abs(box->y2 - box->y1);
742
743         gtk_widget_queue_draw_area ( widget,  x_min, y_min, width, height );
744
745         box->x2 = event->x;
746         box->y2 = event->y;
747
748         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) );
749
750         int i, tile_x, tile_y, tile_x2, tile_y2;
751         tile_t *curr_tile;
752         for (i = 0; i < game->deck.num_tiles; i++)
753         {
754             curr_tile = &game->deck.tiles[i];
755             if (curr_tile->selected)
756             {
757                 curr_tile->selected = 0;
758                 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
759             }
760
761             tile_x = game->deck.tiles[i].x;
762             tile_y = game->deck.tiles[i].y;
763             tile_x2 = tile_x + TILE_WIDTH;
764             tile_y2 = tile_y + TILE_HEIGHT;
765             if (/*If top-left corner*/
766                 (tile_x >= x_min && tile_x <= x_max &&
767                  tile_y >= y_min && tile_y <= y_max) ||
768                 /*or bottom-right corner*/
769                 (tile_x2 >= x_min && tile_x2 <= x_max &&
770                  tile_y2 >= y_min && tile_y2 <= y_max) ||
771                 /*or bottom-left corner*/
772                 (tile_x >= x_min && tile_x <= x_max &&
773                  tile_y2 >= y_min && tile_y2 <= y_max) ||
774                 /*or top-right corner of tile selected*/
775                 (tile_x2 >= x_min && tile_x2 <= x_max &&
776                  tile_y >= y_min && tile_y <= y_max) ||
777                 /*or left edge*/
778                 (y_min >= tile_y && y_min <= tile_y2 &&
779                  x_min <= tile_x && x_max >= tile_x) ||
780                 /*or top edge*/
781                 (x_min >= tile_x && x_min <= tile_x2 &&
782                  y_min <= tile_y && y_max >= tile_y) ||
783                 /*or right edge*/
784                 (y_min >= tile_y && y_min <= tile_y2 &&
785                  x_min >= tile_x && x_min <= tile_x2) ||
786                 /*or bottom edge*/
787                 (x_min >= tile_x && x_min <= tile_x2 &&
788                  y_min >= tile_y && y_min <= tile_y) )
789             {
790                 curr_tile->selected = 1;
791                 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
792             }
793 /*
794             else
795             {
796                 curr_tile->selected = 0;
797                 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
798                 }*/
799         }
800     }
801     else
802     {
803         if (game->drag_group_mode)
804         {
805
806         }
807
808         tile_t *tile;
809 //      tile = &game->deck.tiles[game->current_tile];
810         tile = game->current_tile;
811
812         /* First, invalidate the region where the tile currently is. */
813         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
814
815     /* Then, move the tile */
816         tile->x = event->x - game->diff_x;
817         tile->y = event->y - game->diff_y;
818
819     /* Finally, invalidate the region where the tile is now. */
820         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
821     }
822     return TRUE;
823 }
824
825 int main(int argc, char *argv[])
826 {
827     GtkWidget *window;
828     game_t game;
829
830     srand(time(NULL));
831
832     gtk_init (&argc, &argv);
833
834     game_init(&game);
835     deck_print(&game.deck);
836     deck_spread(&game.deck);
837     deck_deal(&game, &game.deck);
838     //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
839     //deck_print(&game.deck);
840
841     /* Create a new window */
842     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
843     gtk_window_set_default_size (GTK_WINDOW (window),
844                                  GAME_WINDOW_DEFAULT_WIDTH,
845                                  GAME_WINDOW_DEFAULT_HEIGHT);
846
847     gtk_widget_set_events (window,
848                            GDK_EXPOSURE_MASK |
849                            GDK_KEY_PRESS_MASK |
850                            GDK_BUTTON_MOTION_MASK |
851                            GDK_BUTTON_PRESS_MASK |
852                            GDK_BUTTON_RELEASE_MASK);
853
854     g_signal_connect (G_OBJECT (window), "delete_event",
855                       G_CALLBACK (gtk_main_quit), NULL);
856     g_signal_connect (G_OBJECT (window), "expose_event",
857                       G_CALLBACK (on_expose_event), &game);
858     g_signal_connect (G_OBJECT (window), "key_press_event",
859                       G_CALLBACK (on_key_press_event), &game);
860     g_signal_connect (G_OBJECT (window), "button_press_event",
861                       G_CALLBACK (on_button_press_event), &game);
862     g_signal_connect (G_OBJECT (window), "button_release_event",
863                       G_CALLBACK (on_button_release_event), &game);
864     g_signal_connect (G_OBJECT (window), "motion_notify_event",
865                       G_CALLBACK (on_button_motion_event), &game);
866
867
868     gtk_widget_show_all (window);
869     gtk_main ();
870
871     return 0;
872
873 }