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