]> git.cworth.org Git - kub/blob - kub.c
Put tiles back in player's hand(in_hand = 1) when turn is finished(press enter)
[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     printf("State saved\n");
550     game->state.board = game->board;
551     game->state.deck = game->deck;
552     int i;
553     for (i=0; i < game->num_players; i++)
554         game->state.players[i] = game->players[i];
555 }
556
557 static void restore_state(game_t *game)
558 {
559     game->board = game->state.board;
560     game->deck = game->state.deck;
561     int i;
562     for (i=0; i < game->num_players; i++)
563         game->players[i] = game->state.players[i];
564 }
565
566 static void game_init(game_t *game)
567 {
568     int i;
569     GError *error = NULL;
570
571     game->num_players = 0;
572
573     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
574     {
575         player_init(&game->players[i]);
576         game->num_players += 1;
577     }
578     game->current_player = 0;
579     
580     selection_box_init(&game->selection_box);
581     board_init(&game->board);
582     deck_init(&game->deck);
583     deck_shuffle(&game->deck);
584
585     game->selectedtile = rsvg_handle_new_from_file ("tiles/selected_tile.svg", &error);
586     if (error)
587         FATAL_ERROR (error->message);
588
589     game->ownedtile = rsvg_handle_new_from_file ("tiles/owned_tile.svg", &error);
590     if (error)
591         FATAL_ERROR (error->message);
592
593     game->blanktile = rsvg_handle_new_from_file ("tiles/blank_tile.svg", &error);
594     if (error)
595         FATAL_ERROR (error->message);
596
597 //    game->current_tile = game->deck.num_tiles - 1;
598     game->current_tile = &game->deck.tiles[0];
599     game->select_mode = 1;
600     game->drag_group_mode = 0;
601
602     game->diff_x = game->diff_y = 0;
603 }
604
605 static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game)
606 {
607     cairo_t *cr;
608
609     cr = gdk_cairo_create (widget->window);
610
611     deck_draw(game, cr, event->region);
612     board_draw(game, cr, event->region);
613
614     if (game->selection_box.visible)
615         selection_box_draw(&game->selection_box, cr);
616
617     hand_draw(game, game->current_player, cr, event->region, widget);
618     //hand_draw(game, 0, cr, event->region);
619
620     cairo_destroy (cr);
621
622     return TRUE;
623 }
624 /*
625 static gboolean on_configure_event (GtkWidget *widget, GdkEventConfigure *event, game_t *game)
626 {
627     cairo_t *cr;
628
629     cr = gdk_cairo_create (event->window);
630
631     hand_draw(game, game->current_player, cr, event->window, widget);
632 //    gtk_widget_queue_draw(widget);
633 }
634 */
635 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
636 {
637     player_t *player = &game->players[game->current_player];
638     cairo_t *cr;
639     cr = gdk_cairo_create (widget->window);
640     printf ("You pressed key %d\n", event->keyval);
641
642     if (event->keyval == 100)           //HIT "d"
643     {
644         //Draw(take) top tile from deck and put in hand
645         if (game->deck.num_tiles > 0)
646         {
647             tile_t top_tile = game->deck.tiles[game->deck.num_tiles-1];
648             game->deck.num_tiles--;
649             player->hand.tiles[player->hand.num_tiles] = top_tile;
650             player->hand.tiles[player->hand.num_tiles].owned=1;
651             player->hand.num_tiles++;
652             printf("Tile added to hand\n");
653             gtk_widget_queue_draw(widget);
654         }
655     }
656     else if (event->keyval == 115)              //HIT "s"
657         save_state(game);
658     else if (event->keyval == 65293)            //HIT ENTER
659     {
660         player_t *player = &game->players[game->current_player];
661         int i;
662         for (i = 0; i < player->hand.num_tiles; i++)
663             player->hand.tiles[i].in_hand = 1;
664         save_state(game);
665         printf ("\tEnd of player %d's turn\n", game->current_player+1);
666         if (game->current_player == game->num_players-1)
667             game->current_player = 0;
668         else
669             game->current_player += 1;
670         gtk_widget_queue_draw(widget);
671     }
672     else if (event->keyval == 65307)            //HIT ESCAPE
673     {
674         restore_state(game);
675         printf ("\tChanges Reverted\n");
676         gtk_widget_queue_draw(widget);
677     }
678     else if (event->keyval == 65474)            //HIT "F5"
679     {
680         gtk_widget_queue_draw(widget);
681     }
682     else if (event->keyval == 65505 || event->keyval == 65506)
683         game->drag_group_mode = 1;
684     return TRUE;
685 }
686
687 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
688 {
689     int i, tile_x, tile_y;
690     tile_t *curr_tile;
691     player_t *curr_player = &game->players[game->current_player];
692
693     /*Handle tiles in player's hand */
694     for (i = 0; i < curr_player->hand.num_tiles; i++)
695     {
696         curr_tile = &curr_player->hand.tiles[i];
697         if (curr_tile->selected)
698         {
699             curr_tile->selected = 0;
700             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
701         }
702         tile_x = curr_player->hand.tiles[i].x;
703         tile_y = curr_player->hand.tiles[i].y;
704         if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
705             event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
706         {
707             game->select_mode = 0;
708             game->current_tile = curr_tile;
709             curr_tile->in_hand = 0;
710             if (!curr_tile->selected)
711                 curr_tile->selected = 1;
712             else
713                 curr_tile->selected = 0;
714             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
715             game->diff_x = event->x - tile_x;
716             game->diff_y = event->y - tile_y;
717         }
718     }
719
720     /*Handle tiles in deck */
721     for (i = 0; i < game->deck.num_tiles; i++)
722     {
723         curr_tile = &game->deck.tiles[i];
724         if (curr_tile->selected)
725         {
726             curr_tile->selected = 0;
727             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
728         }
729
730         tile_x = game->deck.tiles[i].x;
731         tile_y = game->deck.tiles[i].y;
732         if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
733             event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
734         {
735             game->select_mode = 0;
736  
737 //          game->current_tile = i;
738             game->current_tile = curr_tile;
739
740 //delete_this?      curr_tile = &game->deck.tiles[game->current_tile];
741             if (!curr_tile->selected)
742                 curr_tile->selected = 1;
743             else
744                 curr_tile->selected = 0;
745             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
746
747             game->diff_x = event->x - tile_x;
748             game->diff_y = event->y - tile_y;
749         }
750     }
751     if (game->select_mode)
752     {
753 //      game->deck.tiles[game->current_tile].selected = 0;
754         game->current_tile->selected = 0;
755         gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
756
757         game->selection_box.visible = 1;
758         /*These next two lines appear to be dead
759         game->click_x = event->x;
760         game->click_y = event->y;*/
761
762         game->selection_box.x1 = event->x;
763         game->selection_box.x2 = event->x;
764         game->selection_box.y1 = event->y;
765         game->selection_box.y2 = event->y;
766     }
767     return TRUE;
768 }
769
770 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
771 {
772     if (game->select_mode)
773     {
774         game->select_mode = 0;
775         selection_box_t *box;
776         box = &game->selection_box;
777
778         int x_min = MIN(box->x1, box->x2);
779         int x_max = MAX(box->x1, box->x2);
780         int y_min = MIN(box->y1, box->y2);
781         int y_max = MAX(box->y1, box->y2);
782         int width = abs(box->x2 - box->x1);
783         int height = abs(box->y2 - box->y1);
784
785         box->visible = 0;       
786         gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
787
788         tile_group_t group;
789         group.num_tiles = 0;
790
791         int i, tile_x, tile_y, tile_x2, tile_y2;
792         int tiles_to_remove[game->deck.num_tiles];
793         for (i = 0; i < game->deck.num_tiles; i++)
794         {
795             tile_x = game->deck.tiles[i].x;
796             tile_y = game->deck.tiles[i].y;
797             tile_x2 = tile_x + TILE_WIDTH;
798             tile_y2 = tile_y + TILE_HEIGHT;
799             if (/*If top-left corner*/
800                 (tile_x >= x_min && tile_x <= x_max &&
801                  tile_y >= y_min && tile_y <= y_max) ||
802                 /*or bottom-right corner*/
803                 (tile_x2 >= x_min && tile_x2 <= x_max &&
804                  tile_y2 >= y_min && tile_y2 <= y_max) ||
805                 /*or bottom-left corner*/
806                 (tile_x >= x_min && tile_x <= x_max &&
807                  tile_y2 >= y_min && tile_y2 <= y_max) ||
808                 /*or top-right corner*/
809                 (tile_x2 >= x_min && tile_x2 <= x_max &&
810                  tile_y >= y_min && tile_y <= y_max) ||
811                 /*or left edge*/
812                 (y_min >= tile_y && y_min <= tile_y2 &&
813                  x_min <= tile_x && x_max >= tile_x) ||
814                 /*or top edge*/
815                 (x_min >= tile_x && x_min <= tile_x2 &&
816                  y_min <= tile_y && y_max >= tile_y) ||
817                 /*or right edge*/
818                 (y_min >= tile_y && y_min <= tile_y2 &&
819                  x_min >= tile_x && x_min <= tile_x2) ||
820                 /*or bottom edge of tile selected*/
821                 (x_min >= tile_x && x_min <= tile_x2 &&
822                  y_min >= tile_y && y_min <= tile_y) )
823             {
824                 tiles_to_remove[group.num_tiles] = i;
825
826                 group.tiles[group.num_tiles] = game->deck.tiles[i];
827                 group.num_tiles++;
828             }
829         }
830         printf("is run %d\n", tile_group_is_run_one(&group) );
831         printf("is set %d\n", tile_group_is_set(&group) );
832
833         qsort (tiles_to_remove, group.num_tiles, sizeof (int), int_compare);
834
835         for (i = 0; i < group.num_tiles; i++)
836         {
837             tile_print(group.tiles[i]);
838             gtk_widget_queue_draw_area (widget, group.tiles[i].x - 1,
839                                         group.tiles[i].y - 1, TILE_WIDTH + 1,
840                                         TILE_HEIGHT + 2);
841
842             group.tiles[i].x = x_min + (i * (TILE_WIDTH));
843             group.tiles[i].y = y_min;
844
845             gtk_widget_queue_draw_area (widget, group.tiles[i].x - 1,
846                                         group.tiles[i].y - 1, TILE_WIDTH + 1,
847                                         TILE_HEIGHT + 2);
848
849             //Remove tile from deck
850             if (tiles_to_remove[i] != game->deck.num_tiles - 1)
851                 game->deck.tiles[tiles_to_remove[i]] = game->deck.tiles[game->deck.num_tiles-1];
852             game->deck.num_tiles--;
853         }    
854
855         if (group.num_tiles > 0)
856         {
857             game->board.groups[game->board.num_groups] = group;
858             game->board.num_groups++;
859         }
860         board_print(game);
861         printf("\nBut is the board valid?\t\t%s\n", board_valid(&game->board) ? "yes" : "no");
862     }    
863     game->select_mode = 1;
864
865     return TRUE;
866 }
867
868 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
869                                         game_t *game, cairo_t *cr)
870 {
871     if (game->select_mode)
872     {
873         selection_box_t *box;
874         box = &game->selection_box;
875         box->visible = 1;
876
877         int x_min = MIN(box->x1, box->x2);
878         int x_max = MAX(box->x1, box->x2);
879         int y_min = MIN(box->y1, box->y2);
880         int y_max = MAX(box->y1, box->y2);
881         int width = abs(box->x2 - box->x1);
882         int height = abs(box->y2 - box->y1);
883
884         gtk_widget_queue_draw_area ( widget,  x_min, y_min, width, height );
885
886         box->x2 = event->x;
887         box->y2 = event->y;
888
889         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) );
890
891         int i, tile_x, tile_y, tile_x2, tile_y2;
892         tile_t *curr_tile;
893         for (i = 0; i < game->deck.num_tiles; i++)
894         {
895             curr_tile = &game->deck.tiles[i];
896
897             tile_x = game->deck.tiles[i].x;
898             tile_y = game->deck.tiles[i].y;
899             tile_x2 = tile_x + TILE_WIDTH;
900             tile_y2 = tile_y + TILE_HEIGHT;
901             if (/*If top-left corner*/
902                 (tile_x >= x_min && tile_x <= x_max &&
903                  tile_y >= y_min && tile_y <= y_max) ||
904                 /*or bottom-right corner*/
905                 (tile_x2 >= x_min && tile_x2 <= x_max &&
906                  tile_y2 >= y_min && tile_y2 <= y_max) ||
907                 /*or bottom-left corner*/
908                 (tile_x >= x_min && tile_x <= x_max &&
909                  tile_y2 >= y_min && tile_y2 <= y_max) ||
910                 /*or top-right corner*/
911                 (tile_x2 >= x_min && tile_x2 <= x_max &&
912                  tile_y >= y_min && tile_y <= y_max) ||
913                 /*or left edge*/
914                 (y_min >= tile_y && y_min <= tile_y2 &&
915                  x_min <= tile_x && x_max >= tile_x) ||
916                 /*or top edge*/
917                 (x_min >= tile_x && x_min <= tile_x2 &&
918                  y_min <= tile_y && y_max >= tile_y) ||
919                 /*or right edge*/
920                 (y_min >= tile_y && y_min <= tile_y2 &&
921                  x_min >= tile_x && x_min <= tile_x2) ||
922                 /*or bottom edge of tile selected*/
923                 (x_min >= tile_x && x_min <= tile_x2 &&
924                  y_min >= tile_y && y_min <= tile_y) )
925             {
926                 curr_tile->selected = 1;
927                 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
928             }
929
930             else
931             {
932                 if (curr_tile->selected)
933                 {
934                     curr_tile->selected = 0;
935                     gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
936                 }
937             }
938         }
939     }
940     else
941     {
942         if (game->drag_group_mode)
943         {
944
945         }
946
947         tile_t *tile;
948 //      tile = &game->deck.tiles[game->current_tile];
949         tile = game->current_tile;
950
951         /* First, invalidate the region where the tile currently is. */
952         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
953
954     /* Then, move the tile */
955         tile->x = event->x - game->diff_x;
956         tile->y = event->y - game->diff_y;
957
958     /* Finally, invalidate the region where the tile is now. */
959         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
960     }
961     return TRUE;
962 }
963
964 int main(int argc, char *argv[])
965 {
966     GtkWidget *window;
967     game_t game;
968
969     srand(time(NULL));
970
971     gtk_init (&argc, &argv);
972
973     game_init(&game);
974     deck_print(&game.deck);
975     deck_spread(&game.deck);
976     deck_deal(&game, &game.deck);
977
978     game.state.board = game.board;
979     game.state.deck = game.deck;
980     int i;
981     for (i=0; i < game.num_players; i++)
982         game.state.players[i] = game.players[i];
983
984     //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
985     //deck_print(&game.deck);
986
987     /* Create a new window */
988     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
989     gtk_window_set_default_size (GTK_WINDOW (window),
990                                  GAME_WINDOW_DEFAULT_WIDTH,
991                                  GAME_WINDOW_DEFAULT_HEIGHT);
992
993     gtk_widget_set_events (window,
994                            GDK_EXPOSURE_MASK |
995                            GDK_KEY_PRESS_MASK |
996                            GDK_BUTTON_MOTION_MASK |
997                            GDK_BUTTON_PRESS_MASK |
998                            GDK_BUTTON_RELEASE_MASK);
999
1000     g_signal_connect (G_OBJECT (window), "delete_event",
1001                       G_CALLBACK (gtk_main_quit), NULL);
1002     g_signal_connect (G_OBJECT (window), "expose_event",
1003                       G_CALLBACK (on_expose_event), &game);
1004 //    g_signal_connect (G_OBJECT (window), "configure_event",
1005 //                    G_CALLBACK (on_configure_event), &game);
1006     g_signal_connect (G_OBJECT (window), "key_press_event",
1007                       G_CALLBACK (on_key_press_event), &game);
1008     g_signal_connect (G_OBJECT (window), "button_press_event",
1009                       G_CALLBACK (on_button_press_event), &game);
1010     g_signal_connect (G_OBJECT (window), "button_release_event",
1011                       G_CALLBACK (on_button_release_event), &game);
1012     g_signal_connect (G_OBJECT (window), "motion_notify_event",
1013                       G_CALLBACK (on_button_motion_event), &game);
1014
1015
1016     gtk_widget_show_all (window);
1017     gtk_main ();
1018
1019     return 0;
1020
1021 }