]> git.cworth.org Git - kub/blobdiff - kub.c
Make game.current_tile a pointer of type tile_t rather than a simple int.
[kub] / kub.c
diff --git a/kub.c b/kub.c
old mode 100644 (file)
new mode 100755 (executable)
index 69be1d2..f4a5788
--- a/kub.c
+++ b/kub.c
@@ -12,7 +12,7 @@
  * ||/   /
  * ||   |  TILE_WIDTH = 40
  * ||   | /
- * |     | 
+ * |     |
  * +-----+ - TILE_NUMBER_Y_OFFSET = 3  -
  * |+---+| -                           |
  * ||   ||                             |
@@ -22,7 +22,7 @@
  * | / \ |                             |
  * ||   ||   TILE_SUN_HEIGHT = 20      |
  * | \_/ | _                           |
- * |     |   TILE_SUN_Y_OFFSET = 3     |       
+ * |     |   TILE_SUN_Y_OFFSET = 3     |
  * +-----+ -                           -
  * ||   | TILE_SUN_WIDTH = 20
  * ||
@@ -41,7 +41,7 @@
 #define TILE_SUN_WIDTH         20
 #define TILE_SUN_HEIGHT                20
 
-#define FATAL_ERROR(msg) \
+#define FATAL_ERROR(msg)                                               \
     do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0)
 
 char *colors[] = {"Black", "Blue", "Red", "Yellow"};
@@ -53,6 +53,8 @@ typedef struct tile {
     int number;
     int x;
     int y;
+    int selected;
+    int owned;
 } tile_t;
 
 #define DECK_MAX_TILES 104
@@ -80,6 +82,10 @@ typedef struct player {
     tile_group_t hand;
 } player_t;
 
+typedef struct selection_box {
+    int x1, x2, y1, y2, visible;
+} selection_box_t;
+
 #define GAME_MAX_PLAYERS 4
 #define GAME_WINDOW_DEFAULT_WIDTH  800
 #define GAME_WINDOW_DEFAULT_HEIGHT 600
@@ -89,20 +95,44 @@ typedef struct game {
     int num_players;
     board_t board;
     deck_t deck;
+    selection_box_t selection_box;
     RsvgHandle *blanktile;
+    RsvgHandle *selectedtile;
+    RsvgHandle *ownedtile;
 
-    int current_tile;
+    tile_t *current_tile;
+//    int current_tile;
+    int select_mode;
+    int drag_group_mode;
     int diff_x, diff_y;
     int click_x, click_y;
-    int release_x, release_y;
+    int release_x, release_y;    /*Currently unused*/
 } game_t;
 
+static void selection_box_init(selection_box_t *box)
+{
+    box->x1 = 0;
+    box->x2 = 0;
+    box->y1 = 0;
+    box->y2 = 0;
+    box->visible = 0;
+}
+
+static void selection_box_draw(selection_box_t *box, cairo_t *cr)
+{
+    cairo_rectangle (cr, box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1);
+    cairo_set_source_rgba(cr, 0.0, 0.1, 0.2, 0.5);
+    cairo_fill (cr);
+}
+
 static void tile_init (tile_t *tile, color_t color, int number)
 {
     tile->color = color;
     tile->number = number;
     tile->x = 0;
     tile->y = 0;
+    tile->selected = 0;
+    tile->owned = 0;
 }
 
 static void tile_set_x_y (tile_t *tile, int x, int y)
@@ -135,23 +165,29 @@ static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region
 
     cairo_save(cr);
     cairo_translate(cr, tile->x, tile->y);
-    rsvg_handle_render_cairo (game->blanktile, cr);
-    
+
+    if (tile->selected)
+       rsvg_handle_render_cairo (game->selectedtile, cr);
+    if (tile->owned)
+       rsvg_handle_render_cairo (game->ownedtile, cr); 
+    else
+       rsvg_handle_render_cairo (game->blanktile, cr);
+
     if (tile->color == BLACK)
-        cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
+       cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
     if (tile->color == BLUE)
-        cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);      
+       cairo_set_source_rgb (cr, 0.0, 0.0, 1.0);
     if (tile->color == RED)
-        cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
+       cairo_set_source_rgb (cr, 1.0, 0.0, 0.0);
     if (tile->color == YELLOW)
-    cairo_set_source_rgb (cr, 1.0, .843, 0.0);
+       cairo_set_source_rgb (cr, 1.0, .843, 0.0);
     if (tile->number + 1 > 9)
-        cairo_move_to (cr, 1, 25);
+       cairo_move_to (cr, 1, 25);
     else
-        cairo_move_to (cr, 10, 25);
+       cairo_move_to (cr, 10, 25);
     cairo_set_font_size(cr, 25);
     cairo_show_text (cr, number_string);
-    
+
     cairo_restore(cr);
 }
 
@@ -164,8 +200,8 @@ static void board_init(board_t *board)
 {
     int i;
     board->num_groups = 0;
-    
-    for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i) 
+
+    for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i)
     {
        tile_group_init(&board->groups[i]);
     }
@@ -192,20 +228,26 @@ static int tile_group_is_run_one(tile_group_t *tile_group)
     int i;
     qsort (&tile_group->tiles[0], tile_group->num_tiles,
           sizeof (tile_t), tile_compare);
-          
+
     if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3)
     {
+       printf("fail run - invalid num tiles; ");
        return 0;
     }
     for (i = 0; i < tile_group->num_tiles - 1; ++i)
+    {
        if(tile_group->tiles[i].color != tile_group->tiles[i + 1].color)
-       {
+        {
+           printf("fail run - colors don't match; ");
            return 0;
-       }
-       if(tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1)
-       {
+        }
+       if( tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1 &&
+           i+1 != tile_group->num_tiles)
+        {
+           printf("fail run - invalid number sequence; ");
            return 0;
-       }
+        }
+    }
     return 1;
 }
 
@@ -275,27 +317,32 @@ static int tile_group_is_run_two(tile_group_t *tile_group)
 static int tile_group_is_set(tile_group_t *tile_group)
 {
     int i;
-    color_t seen_color[tile_group->num_tiles];
-    
+    color_t seen_color[4];
+    for (i = 0; i < 4; i++)
+       seen_color[i] = 0;
+
     if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3)
     {
+       printf("fail set - invalid num tiles; ");
        return 0;
     }
-    for (i = 0; i < tile_group->num_tiles - 1; ++i) 
+    for (i = 0; i <= tile_group->num_tiles - 1; ++i)
     {
-       if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number)
-       {
+       if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number &&
+           i+1 != tile_group->num_tiles)
+        {
+           printf("fail set - numbers don't match; ");
            return 0;
-       }
+        }
+       seen_color[tile_group->tiles[i].color] += 1;
     }
-    seen_color[i] = tile_group->tiles[i].color;
-    for (i = 0; i < tile_group->num_tiles; ++i)
+    for (i = 0; i < 4; i++)
     {
-       seen_color[tile_group->tiles[i].color]++;
-       if (seen_color[tile_group->tiles[i].color] > 1)
-       {
+       if (seen_color[i] > 1)
+        {
+           printf("fail set - repeat color; ");
            return 0;
-       }
+        }
     }
     return 1;
 }
@@ -305,7 +352,7 @@ static void deck_deal(game_t *game, deck_t *deck)
     tile_t temp;
     int rand_tile;
     int i, j, newline;
-    
+
     printf ("How many players(1-4) should I deal in? ");
     game->num_players = getchar();
     if (game->num_players == EOF)
@@ -313,9 +360,9 @@ static void deck_deal(game_t *game, deck_t *deck)
        printf ("\nGoodbye.\n");
        exit (1);
     }
-    newline = getchar();   
+    newline = getchar();
     game->num_players -= '0';
-    
+
     for (i = 0; i < game->num_players; ++i)
     {
        for (j = 0; j < 14; ++j)
@@ -324,6 +371,7 @@ static void deck_deal(game_t *game, deck_t *deck)
            temp = deck->tiles[rand_tile];
            deck->tiles[rand_tile] = deck->tiles[deck->num_tiles - 1];
            game->players[i].hand.tiles[j] = temp;
+           game->players[i].hand.tiles[j].owned = 1;
            deck->num_tiles -= 1;
            game->players[i].hand.num_tiles += 1;
        }
@@ -333,20 +381,20 @@ static void deck_deal(game_t *game, deck_t *deck)
 
 static void deck_init(deck_t *deck)
 {
-    int h, i, j;  
+    int h, i, j;
     deck->num_tiles = 0;
     for (h = 0; h <= 1; ++h)
     {
-       for (i = 0; i <= 3; ++i) 
+       for (i = 0; i <= 3; ++i)
        {
-           for (j = 0; j <= 12; ++j) 
+           for (j = 0; j <= 12; ++j)
            {
                tile_init (&deck->tiles[deck->num_tiles++], i, j);
                printf ("There are %d tiles in the deck\n", deck->num_tiles);
            }
        }
     }
-} 
+}
 
 static void deck_shuffle(deck_t *deck)
 {
@@ -364,7 +412,7 @@ static void deck_shuffle(deck_t *deck)
 
 static void deck_print(deck_t *deck)
 {
-    int h, i, j;  
+    int h, i, j;
     for (h = 0; h < 2; ++h)
     {
        for (i = 0; i < 4; ++i)
@@ -396,9 +444,9 @@ static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region)
     int i;
     for (i = 0; i < game->deck.num_tiles; i++)
     {
-           tile_draw(game, &game->deck.tiles[i], cr, region);
+       tile_draw(game, &game->deck.tiles[i], cr, region);
     }
-}   
+}
 
 static void hand_print(game_t *game, int player)
 {
@@ -414,9 +462,9 @@ static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region)
     int i;
     int gwdw = GAME_WINDOW_DEFAULT_WIDTH;
     int gwdh = GAME_WINDOW_DEFAULT_HEIGHT;
-    for (i = 0; i < game->players[player].hand.num_tiles; i++)    
+    for (i = 0; i < game->players[player].hand.num_tiles; i++)
     {
-       tile_set_x_y(&game->players[player].hand.tiles[i],  
+       tile_set_x_y(&game->players[player].hand.tiles[i],
                     ((gwdw / game->players[player].hand.num_tiles)) * i,
                     (gwdh - TILE_HEIGHT - 6) );
     }
@@ -432,25 +480,35 @@ static void game_init(game_t *game)
     GError *error = NULL;
 
     game->num_players = 0;
-    
+
     for (i = 0; i < GAME_MAX_PLAYERS; ++i)
     {
        player_init(&game->players[i]);
        game->num_players += 1;
     }
     
+    selection_box_init(&game->selection_box);
     board_init(&game->board);
     deck_init(&game->deck);
     deck_shuffle(&game->deck);
 
-    game->blanktile = rsvg_handle_new_from_file ("tiles/blanktile.svg", &error);
+    game->selectedtile = rsvg_handle_new_from_file ("tiles/selected_tile.svg", &error);
     if (error)
        FATAL_ERROR (error->message);
 
-    /*This line appears to be useless, has been replaced by line below*/
-    //game->current_tile = game->deck.num_tiles - 1;    
-    game->current_tile = -1;
-    
+    game->ownedtile = rsvg_handle_new_from_file ("tiles/owned_tile.svg", &error);
+    if (error)
+       FATAL_ERROR (error->message);
+
+    game->blanktile = rsvg_handle_new_from_file ("tiles/blank_tile.svg", &error);
+    if (error)
+       FATAL_ERROR (error->message);
+
+//    game->current_tile = game->deck.num_tiles - 1;
+    game->current_tile = &game->deck.tiles[0];
+    game->select_mode = 1;
+    game->drag_group_mode = 0;
+
     game->diff_x = game->diff_y = 0;
 }
 
@@ -462,6 +520,9 @@ static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_
 
     deck_draw(game, cr, event->region);
 
+    if (game->selection_box.visible)
+       selection_box_draw(&game->selection_box, cr);
+
     hand_draw(game, 0, cr, event->region);
 
     cairo_destroy (cr);
@@ -472,6 +533,8 @@ static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_
 static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game)
 {
     printf ("You pressed key %d\n", event->keyval);
+    if (event->keyval == 65505 || event->keyval == 65506)
+       game->drag_group_mode = 1;
 
     return TRUE;
 }
@@ -479,75 +542,283 @@ static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_
 static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
 {
     int i, tile_x, tile_y;
-    
+    tile_t *curr_tile;
+    player_t *curr_player = &game->players[0];
+
+    /*Handle tiles in player's hand */
+    for (i = 0; i < curr_player->hand.num_tiles; i++)
+    {
+       curr_tile = &curr_player->hand.tiles[i];
+       if (curr_tile->selected)
+       {
+           curr_tile->selected = 0;
+           gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+       }
+       tile_x = curr_player->hand.tiles[i].x;
+       tile_y = curr_player->hand.tiles[i].y;
+       if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
+           event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
+        {
+           game->select_mode = 0;
+
+           game->current_tile = curr_tile;
+
+           if (!curr_tile->selected)
+               curr_tile->selected = 1;
+           else
+               curr_tile->selected = 0;
+           gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+
+           game->diff_x = event->x - tile_x;
+           game->diff_y = event->y - tile_y;
+        }
+    }
+
+    /*Handle tiles in deck */
     for (i = 0; i < game->deck.num_tiles; i++)
     {
-        tile_x = game->deck.tiles[i].x;
-        tile_y = game->deck.tiles[i].y;
-        if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
-            event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
+       curr_tile = &game->deck.tiles[i];
+       if (curr_tile->selected)
+       {
+           curr_tile->selected = 0;
+           gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+       }
+
+       tile_x = game->deck.tiles[i].x;
+       tile_y = game->deck.tiles[i].y;
+       if (event->x >= tile_x && event->x <= (tile_x + TILE_WIDTH) &&
+           event->y >= tile_y && event->y <= (tile_y + TILE_HEIGHT) )
         {
-            game->current_tile = i;
-            game->diff_x = event->x - tile_x;
-            game->diff_y = event->y - tile_y;
+           game->select_mode = 0;
+//         game->current_tile = i;
+           game->current_tile = curr_tile;
+
+//delete_this?     curr_tile = &game->deck.tiles[game->current_tile];
+           if (!curr_tile->selected)
+               curr_tile->selected = 1;
+           else
+               curr_tile->selected = 0;
+           gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+
+           game->diff_x = event->x - tile_x;
+           game->diff_y = event->y - tile_y;
         }
     }
-    if (game->current_tile == -1)
-    {            
-            game->click_x = event->x;
-            game->click_y = event->y;
+    if (game->select_mode)
+    {
+//     game->deck.tiles[game->current_tile].selected = 0;
+       game->current_tile->selected = 0;
+       gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+
+       game->selection_box.visible = 1;
+       /*These next two lines appear to be dead
+       game->click_x = event->x;
+       game->click_y = event->y;*/
+
+       game->selection_box.x1 = event->x;
+       game->selection_box.x2 = event->x;
+       game->selection_box.y1 = event->y;
+       game->selection_box.y2 = event->y;
     }
     return TRUE;
 }
 
 static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game)
 {
-    if (game->current_tile == -1)
+    if (game->select_mode)
     {
-        tile_group_t group;
-        group.num_tiles = 0;
-        
-        int i, tile_x, tile_y;
-        for (i = 0; i < game->deck.num_tiles; i++)
+       game->select_mode = 0;
+       selection_box_t *box;
+       box = &game->selection_box;
+
+       int x_min = MIN(box->x1, box->x2);
+       int x_max = MAX(box->x1, box->x2);
+       int y_min = MIN(box->y1, box->y2);
+       int y_max = MAX(box->y1, box->y2);
+       int width = abs(box->x2 - box->x1);
+       int height = abs(box->y2 - box->y1);
+
+       box->visible = 0;       
+       gtk_widget_queue_draw_area (widget, x_min, y_min, width, height);
+
+//     tile_group_t group;
+//     group.num_tiles = 0;
+
+       tile_t* group[TILE_GROUP_MAX_TILES];
+       int num_tiles = 0;
+
+       int i, tile_x, tile_y, tile_x2, tile_y2;
+       for (i = 0; i < game->deck.num_tiles; i++)
         {
-            tile_x = game->deck.tiles[i].x;
-            tile_y = game->deck.tiles[i].y;
-            if ( (event->x >= tile_x && game->click_x <= tile_x &&
-                 event->y >= tile_y && game->click_y <= tile_y) ||
-                 (event->x >= tile_x && game->click_x <= tile_x &&
-                 event->y <= (tile_y + TILE_HEIGHT) && game->click_y >= tile_y) )
-            {
-                group.tiles[group.num_tiles] = game->deck.tiles[i];
-                group.num_tiles++;
+           tile_x = game->deck.tiles[i].x;
+           tile_y = game->deck.tiles[i].y;
+           tile_x2 = tile_x + TILE_WIDTH;
+           tile_y2 = tile_y + TILE_HEIGHT;
+           if (/*If top-left corner*/
+               (tile_x >= x_min && tile_x <= x_max &&
+                tile_y >= y_min && tile_y <= y_max) ||
+               /*or bottom-right corner*/
+               (tile_x2 >= x_min && tile_x2 <= x_max &&
+                tile_y2 >= y_min && tile_y2 <= y_max) ||
+               /*or bottom-left corner*/
+               (tile_x >= x_min && tile_x <= x_max &&
+                tile_y2 >= y_min && tile_y2 <= y_max) ||
+               /*or top-right corner of tile selected*/
+               (tile_x2 >= x_min && tile_x2 <= x_max &&
+                tile_y >= y_min && tile_y <= y_max) ||
+               /*or left edge*/
+               (y_min >= tile_y && y_min <= tile_y2 &&
+                x_min <= tile_x && x_max >= tile_x) ||
+               /*or top edge*/
+               (x_min >= tile_x && x_min <= tile_x2 &&
+                y_min <= tile_y && y_max >= tile_y) ||
+               /*or right edge*/
+               (y_min >= tile_y && y_min <= tile_y2 &&
+                x_min >= tile_x && x_min <= tile_x2) ||
+               /*or bottom edge*/
+               (x_min >= tile_x && x_min <= tile_x2 &&
+                y_min >= tile_y && y_min <= tile_y) )
+            {          
+//             group.tiles[group.num_tiles] = game->deck.tiles[i];
+//             group.num_tiles++;
+
+               group[num_tiles] = &game->deck.tiles[i];;
+               num_tiles++;
             }
         }
-        for (i = 0; i < group.num_tiles; i++)
-            tile_print(group.tiles[i]);
-    }
-    
-    game->current_tile = -1;
-    printf ("You released button %d\n", event->button);
+       //printf("is run %d\n", tile_group_is_run_one(&group) );
+       //printf("is set %d\n", tile_group_is_set(&group) );
+
+//     int matching_y = y_min;
+       
+//     for (i = 0; i < group.num_tiles; i++)
+       for (i = 0; i < num_tiles; i++)
+       {
+//         tile_print(group.tiles[i]);
+           gtk_widget_queue_draw_area (widget, group[i]->x - 1 , group[i]->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+           group[i]->x = x_min + (i * (TILE_WIDTH));
+           group[i]->y = y_min;
+           gtk_widget_queue_draw_area (widget, group[i]->x - 1 , group[i]->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+       }    
+/*     int j;
+       int new_x = 0;
+       for (i = 0; i < group.num_tiles; i++)
+       {
+           for (j = 0; j < game->deck.num_tiles; j++)
+           {
+               if (group.tiles[i].x == game->deck.tiles[j].x &&
+                   group.tiles[i].y == game->deck.tiles[j].y)
+               {
+                   new_x = x_min + (i * (TILE_WIDTH));
+                   gtk_widget_queue_draw_area (widget, game->deck.tiles[j].x - 1 , game->deck.tiles[j].y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+                   game->deck.tiles[j].x = new_x;
+                   game->deck.tiles[j].y = matching_y;
+                   gtk_widget_queue_draw_area (widget, new_x - 1 , matching_y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+               }
+           }
+       }*/
+    }    
+    game->select_mode = 1;
 
     return TRUE;
 }
 
-static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event, 
+static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event,
                                        game_t *game, cairo_t *cr)
 {
-    tile_t *tile;
-    
-    tile = &game->deck.tiles[game->current_tile];
-    
-    /* First, invalidate the region where the tile currently is. */
-    gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
-    
+    if (game->select_mode)
+    {
+       selection_box_t *box;
+       box = &game->selection_box;
+       box->visible = 1;
+
+       int x_min = MIN(box->x1, box->x2);
+       int x_max = MAX(box->x1, box->x2);
+       int y_min = MIN(box->y1, box->y2);
+       int y_max = MAX(box->y1, box->y2);
+       int width = abs(box->x2 - box->x1);
+       int height = abs(box->y2 - box->y1);
+
+       gtk_widget_queue_draw_area ( widget,  x_min, y_min, width, height );
+
+       box->x2 = event->x;
+       box->y2 = event->y;
+
+       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) );
+
+       int i, tile_x, tile_y, tile_x2, tile_y2;
+       tile_t *curr_tile;
+       for (i = 0; i < game->deck.num_tiles; i++)
+        {
+           curr_tile = &game->deck.tiles[i];
+           if (curr_tile->selected)
+           {
+               curr_tile->selected = 0;
+               gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+           }
+
+           tile_x = game->deck.tiles[i].x;
+           tile_y = game->deck.tiles[i].y;
+           tile_x2 = tile_x + TILE_WIDTH;
+           tile_y2 = tile_y + TILE_HEIGHT;
+           if (/*If top-left corner*/
+               (tile_x >= x_min && tile_x <= x_max &&
+                tile_y >= y_min && tile_y <= y_max) ||
+               /*or bottom-right corner*/
+               (tile_x2 >= x_min && tile_x2 <= x_max &&
+                tile_y2 >= y_min && tile_y2 <= y_max) ||
+               /*or bottom-left corner*/
+               (tile_x >= x_min && tile_x <= x_max &&
+                tile_y2 >= y_min && tile_y2 <= y_max) ||
+               /*or top-right corner of tile selected*/
+               (tile_x2 >= x_min && tile_x2 <= x_max &&
+                tile_y >= y_min && tile_y <= y_max) ||
+               /*or left edge*/
+               (y_min >= tile_y && y_min <= tile_y2 &&
+                x_min <= tile_x && x_max >= tile_x) ||
+               /*or top edge*/
+               (x_min >= tile_x && x_min <= tile_x2 &&
+                y_min <= tile_y && y_max >= tile_y) ||
+               /*or right edge*/
+               (y_min >= tile_y && y_min <= tile_y2 &&
+                x_min >= tile_x && x_min <= tile_x2) ||
+               /*or bottom edge*/
+               (x_min >= tile_x && x_min <= tile_x2 &&
+                y_min >= tile_y && y_min <= tile_y) )
+           {
+               curr_tile->selected = 1;
+               gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+           }
+/*
+           else
+           {
+               curr_tile->selected = 0;
+               gtk_widget_queue_draw_area (widget, curr_tile->x - 1, curr_tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+               }*/
+       }
+    }
+    else
+    {
+       if (game->drag_group_mode)
+       {
+
+       }
+
+       tile_t *tile;
+//     tile = &game->deck.tiles[game->current_tile];
+       tile = game->current_tile;
+
+       /* First, invalidate the region where the tile currently is. */
+       gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+
     /* Then, move the tile */
-    tile->x = event->x - game->diff_x;
-    tile->y = event->y - game->diff_y;
-    
+       tile->x = event->x - game->diff_x;
+       tile->y = event->y - game->diff_y;
+
     /* Finally, invalidate the region where the tile is now. */
-    gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
-    
+       gtk_widget_queue_draw_area (widget, tile->x - 1, tile->y - 1, TILE_WIDTH + 1, TILE_HEIGHT + 2);
+    }
     return TRUE;
 }
 
@@ -555,11 +826,11 @@ int main(int argc, char *argv[])
 {
     GtkWidget *window;
     game_t game;
-    
+
     srand(time(NULL));
 
     gtk_init (&argc, &argv);
-    
+
     game_init(&game);
     deck_print(&game.deck);
     deck_spread(&game.deck);
@@ -575,9 +846,9 @@ int main(int argc, char *argv[])
 
     gtk_widget_set_events (window,
                           GDK_EXPOSURE_MASK |
-                          GDK_KEY_PRESS_MASK | 
+                          GDK_KEY_PRESS_MASK |
                           GDK_BUTTON_MOTION_MASK |
-                          GDK_BUTTON_PRESS_MASK | 
+                          GDK_BUTTON_PRESS_MASK |
                           GDK_BUTTON_RELEASE_MASK);
 
     g_signal_connect (G_OBJECT (window), "delete_event",