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