]> git.cworth.org Git - kub/blob - kub.c
Save state at start of game
[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         save_state(game);
661         printf ("\tEnd of player %d's turn\n", game->current_player+1);
662         if (game->current_player == game->num_players-1)
663             game->current_player = 0;
664         else
665             game->current_player += 1;
666         gtk_widget_queue_draw(widget);
667     }
668     else if (event->keyval == 65307)            //HIT ESCAPE
669     {
670         restore_state(game);
671         printf ("\tChanges Reverted\n");
672         gtk_widget_queue_draw(widget);
673     }
674     else if (event->keyval == 65474)            //HIT "F5"
675     {
676         gtk_widget_queue_draw(widget);
677     }
678     else if (event->keyval == 65505 || event->keyval == 65506)
679         game->drag_group_mode = 1;
680     return TRUE;
681 }
682
683 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
684 {
685     int i, tile_x, tile_y;
686     tile_t *curr_tile;
687     player_t *curr_player = &game->players[game->current_player];
688
689     /*Handle tiles in player's hand */
690     for (i = 0; i < curr_player->hand.num_tiles; i++)
691     {
692         curr_tile = &curr_player->hand.tiles[i];
693         if (curr_tile->selected)
694         {
695             curr_tile->selected = 0;
696             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
697         }
698         tile_x = curr_player->hand.tiles[i].x;
699         tile_y = curr_player->hand.tiles[i].y;
700         if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
701             event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
702         {
703             game->select_mode = 0;
704             game->current_tile = curr_tile;
705             curr_tile->in_hand = 0;
706             if (!curr_tile->selected)
707                 curr_tile->selected = 1;
708             else
709                 curr_tile->selected = 0;
710             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
711             game->diff_x = event->x - tile_x;
712             game->diff_y = event->y - tile_y;
713         }
714     }
715
716     /*Handle tiles in deck */
717     for (i = 0; i < game->deck.num_tiles; i++)
718     {
719         curr_tile = &game->deck.tiles[i];
720         if (curr_tile->selected)
721         {
722             curr_tile->selected = 0;
723             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
724         }
725
726         tile_x = game->deck.tiles[i].x;
727         tile_y = game->deck.tiles[i].y;
728         if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
729             event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
730         {
731             game->select_mode = 0;
732  
733 //          game->current_tile = i;
734             game->current_tile = curr_tile;
735
736 //delete_this?      curr_tile = &game->deck.tiles[game->current_tile];
737             if (!curr_tile->selected)
738                 curr_tile->selected = 1;
739             else
740                 curr_tile->selected = 0;
741             gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
742
743             game->diff_x = event->x - tile_x;
744             game->diff_y = event->y - tile_y;
745         }
746     }
747     if (game->select_mode)
748     {
749 //      game->deck.tiles[game->current_tile].selected = 0;
750         game->current_tile->selected = 0;
751         gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
752
753         game->selection_box.visible = 1;
754         /*These next two lines appear to be dead
755         game->click_x = event->x;
756         game->click_y = event->y;*/
757
758         game->selection_box.x1 = event->x;
759         game->selection_box.x2 = event->x;
760         game->selection_box.y1 = event->y;
761         game->selection_box.y2 = event->y;
762     }
763     return TRUE;
764 }
765
766 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
767 {
768     if (game->select_mode)
769     {
770         game->select_mode = 0;
771         selection_box_t *box;
772         box = &game->selection_box;
773
774         int x_min = MIN(box->x1, box->x2);
775         int x_max = MAX(box->x1, box->x2);
776         int y_min = MIN(box->y1, box->y2);
777         int y_max = MAX(box->y1, box->y2);
778         int width = abs(box->x2 - box->x1);
779         int height = abs(box->y2 - box->y1);
780
781         box->visible = 0;       
782         gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
783
784         tile_group_t group;
785         group.num_tiles = 0;
786
787         int i, tile_x, tile_y, tile_x2, tile_y2;
788         int tiles_to_remove[game->deck.num_tiles];
789         for (i = 0; i < game->deck.num_tiles; i++)
790         {
791             tile_x = game->deck.tiles[i].x;
792             tile_y = game->deck.tiles[i].y;
793             tile_x2 = tile_x + TILE_WIDTH;
794             tile_y2 = tile_y + TILE_HEIGHT;
795             if (/*If top-left corner*/
796                 (tile_x >= x_min && tile_x <= x_max &&
797                  tile_y >= y_min && tile_y <= y_max) ||
798                 /*or bottom-right corner*/
799                 (tile_x2 >= x_min && tile_x2 <= x_max &&
800                  tile_y2 >= y_min && tile_y2 <= y_max) ||
801                 /*or bottom-left corner*/
802                 (tile_x >= x_min && tile_x <= x_max &&
803                  tile_y2 >= y_min && tile_y2 <= y_max) ||
804                 /*or top-right corner*/
805                 (tile_x2 >= x_min && tile_x2 <= x_max &&
806                  tile_y >= y_min && tile_y <= y_max) ||
807                 /*or left edge*/
808                 (y_min >= tile_y && y_min <= tile_y2 &&
809                  x_min <= tile_x && x_max >= tile_x) ||
810                 /*or top edge*/
811                 (x_min >= tile_x && x_min <= tile_x2 &&
812                  y_min <= tile_y && y_max >= tile_y) ||
813                 /*or right edge*/
814                 (y_min >= tile_y && y_min <= tile_y2 &&
815                  x_min >= tile_x && x_min <= tile_x2) ||
816                 /*or bottom edge of tile selected*/
817                 (x_min >= tile_x && x_min <= tile_x2 &&
818                  y_min >= tile_y && y_min <= tile_y) )
819             {
820                 tiles_to_remove[group.num_tiles] = i;
821
822                 group.tiles[group.num_tiles] = game->deck.tiles[i];
823                 group.num_tiles++;
824             }
825         }
826         printf("is run %d\n", tile_group_is_run_one(&group) );
827         printf("is set %d\n", tile_group_is_set(&group) );
828
829         qsort (tiles_to_remove, group.num_tiles, sizeof (int), int_compare);
830
831         for (i = 0; i < group.num_tiles; i++)
832         {
833             tile_print(group.tiles[i]);
834             gtk_widget_queue_draw_area (widget, group.tiles[i].x - 1,
835                                         group.tiles[i].y - 1, TILE_WIDTH + 1,
836                                         TILE_HEIGHT + 2);
837
838             group.tiles[i].x = x_min + (i * (TILE_WIDTH));
839             group.tiles[i].y = y_min;
840
841             gtk_widget_queue_draw_area (widget, group.tiles[i].x - 1,
842                                         group.tiles[i].y - 1, TILE_WIDTH + 1,
843                                         TILE_HEIGHT + 2);
844
845             //Remove tile from deck
846             if (tiles_to_remove[i] != game->deck.num_tiles - 1)
847                 game->deck.tiles[tiles_to_remove[i]] = game->deck.tiles[game->deck.num_tiles-1];
848             game->deck.num_tiles--;
849         }    
850
851         if (group.num_tiles > 0)
852         {
853             game->board.groups[game->board.num_groups] = group;
854             game->board.num_groups++;
855         }
856         board_print(game);
857         printf("\nBut is the board valid?\t\t%s\n", board_valid(&game->board) ? "yes" : "no");
858     }    
859     game->select_mode = 1;
860
861     return TRUE;
862 }
863
864 static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
865                                         game_t *game, cairo_t *cr)
866 {
867     if (game->select_mode)
868     {
869         selection_box_t *box;
870         box = &game->selection_box;
871         box->visible = 1;
872
873         int x_min = MIN(box->x1, box->x2);
874         int x_max = MAX(box->x1, box->x2);
875         int y_min = MIN(box->y1, box->y2);
876         int y_max = MAX(box->y1, box->y2);
877         int width = abs(box->x2 - box->x1);
878         int height = abs(box->y2 - box->y1);
879
880         gtk_widget_queue_draw_area ( widget,  x_min, y_min, width, height );
881
882         box->x2 = event->x;
883         box->y2 = event->y;
884
885         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) );
886
887         int i, tile_x, tile_y, tile_x2, tile_y2;
888         tile_t *curr_tile;
889         for (i = 0; i < game->deck.num_tiles; i++)
890         {
891             curr_tile = &game->deck.tiles[i];
892
893             tile_x = game->deck.tiles[i].x;
894             tile_y = game->deck.tiles[i].y;
895             tile_x2 = tile_x + TILE_WIDTH;
896             tile_y2 = tile_y + TILE_HEIGHT;
897             if (/*If top-left corner*/
898                 (tile_x >= x_min && tile_x <= x_max &&
899                  tile_y >= y_min && tile_y <= y_max) ||
900                 /*or bottom-right corner*/
901                 (tile_x2 >= x_min && tile_x2 <= x_max &&
902                  tile_y2 >= y_min && tile_y2 <= y_max) ||
903                 /*or bottom-left corner*/
904                 (tile_x >= x_min && tile_x <= x_max &&
905                  tile_y2 >= y_min && tile_y2 <= y_max) ||
906                 /*or top-right corner*/
907                 (tile_x2 >= x_min && tile_x2 <= x_max &&
908                  tile_y >= y_min && tile_y <= y_max) ||
909                 /*or left edge*/
910                 (y_min >= tile_y && y_min <= tile_y2 &&
911                  x_min <= tile_x && x_max >= tile_x) ||
912                 /*or top edge*/
913                 (x_min >= tile_x && x_min <= tile_x2 &&
914                  y_min <= tile_y && y_max >= tile_y) ||
915                 /*or right edge*/
916                 (y_min >= tile_y && y_min <= tile_y2 &&
917                  x_min >= tile_x && x_min <= tile_x2) ||
918                 /*or bottom edge of tile selected*/
919                 (x_min >= tile_x && x_min <= tile_x2 &&
920                  y_min >= tile_y && y_min <= tile_y) )
921             {
922                 curr_tile->selected = 1;
923                 gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
924             }
925
926             else
927             {
928                 if (curr_tile->selected)
929                 {
930                     curr_tile->selected = 0;
931                     gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
932                 }
933             }
934         }
935     }
936     else
937     {
938         if (game->drag_group_mode)
939         {
940
941         }
942
943         tile_t *tile;
944 //      tile = &game->deck.tiles[game->current_tile];
945         tile = game->current_tile;
946
947         /* First, invalidate the region where the tile currently is. */
948         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
949
950     /* Then, move the tile */
951         tile->x = event->x - game->diff_x;
952         tile->y = event->y - game->diff_y;
953
954     /* Finally, invalidate the region where the tile is now. */
955         gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
956     }
957     return TRUE;
958 }
959
960 int main(int argc, char *argv[])
961 {
962     GtkWidget *window;
963     game_t game;
964
965     srand(time(NULL));
966
967     gtk_init (&argc, &argv);
968
969     game_init(&game);
970     deck_print(&game.deck);
971     deck_spread(&game.deck);
972     deck_deal(&game, &game.deck);
973
974     game.state.board = game.board;
975     game.state.deck = game.deck;
976     int i;
977     for (i=0; i < game.num_players; i++)
978         game.state.players[i] = game.players[i];
979
980     //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0])
981     //deck_print(&game.deck);
982
983     /* Create a new window */
984     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
985     gtk_window_set_default_size (GTK_WINDOW (window),
986                                  GAME_WINDOW_DEFAULT_WIDTH,
987                                  GAME_WINDOW_DEFAULT_HEIGHT);
988
989     gtk_widget_set_events (window,
990                            GDK_EXPOSURE_MASK |
991                            GDK_KEY_PRESS_MASK |
992                            GDK_BUTTON_MOTION_MASK |
993                            GDK_BUTTON_PRESS_MASK |
994                            GDK_BUTTON_RELEASE_MASK);
995
996     g_signal_connect (G_OBJECT (window), "delete_event",
997                       G_CALLBACK (gtk_main_quit), NULL);
998     g_signal_connect (G_OBJECT (window), "expose_event",
999                       G_CALLBACK (on_expose_event), &game);
1000 //    g_signal_connect (G_OBJECT (window), "configure_event",
1001 //                    G_CALLBACK (on_configure_event), &game);
1002     g_signal_connect (G_OBJECT (window), "key_press_event",
1003                       G_CALLBACK (on_key_press_event), &game);
1004     g_signal_connect (G_OBJECT (window), "button_press_event",
1005                       G_CALLBACK (on_button_press_event), &game);
1006     g_signal_connect (G_OBJECT (window), "button_release_event",
1007                       G_CALLBACK (on_button_release_event), &game);
1008     g_signal_connect (G_OBJECT (window), "motion_notify_event",
1009                       G_CALLBACK (on_button_motion_event), &game);
1010
1011
1012     gtk_widget_show_all (window);
1013     gtk_main ();
1014
1015     return 0;
1016
1017 }