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