X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=kub.c;h=213270901d3cd502be2afb4eb03989719a7ca6de;hb=5e61d8a5fc190e3064a225fec01505b7acf4d6b4;hp=452175ef3dc135ca7c2afaa93fb4a4b0128a3434;hpb=b46af358edada8f53c60a89183f13aff69942b13;p=kub diff --git a/kub.c b/kub.c index 452175e..2132709 100644 --- a/kub.c +++ b/kub.c @@ -93,6 +93,8 @@ typedef struct game { int current_tile; int diff_x, diff_y; + int click_x, click_y; + int release_x, release_y; } game_t; static void tile_init (tile_t *tile, color_t color, int number) @@ -142,11 +144,11 @@ static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region if (tile->color == RED) 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); - if (tile->number + 1 > 9) - cairo_move_to (cr, 1, 25); - else - cairo_move_to (cr, 10, 25); + cairo_set_source_rgb (cr, 1.0, .843, 0.0); + if (tile->number + 1 > 9) + cairo_move_to (cr, 1, 25); + else + cairo_move_to (cr, 10, 25); cairo_set_font_size(cr, 25); cairo_show_text (cr, number_string); @@ -189,21 +191,27 @@ 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); - + sizeof (tile_t), tile_compare); + if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3) { - return 0; + 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) - { - return 0; - } - if(tile_group->tiles[i].number != tile_group->tiles[i + 1].number -1) - { - return 0; - } + { + 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 && + i+1 != tile_group->num_tiles) + { + printf("fail run - invalid number sequence; "); + return 0; + } + } return 1; } @@ -273,27 +281,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) { - return 0; + printf("fail set - invalid num tiles; "); + return 0; } for (i = 0; i < tile_group->num_tiles - 1; ++i) { - if (tile_group->tiles[i].number != tile_group->tiles[i + 1].number) - { - return 0; - } + 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) - { - return 0; - } + if (seen_color[i] > 1) + { + printf("fail set - repeat color; "); + return 0; + } } return 1; } @@ -445,7 +458,10 @@ static void game_init(game_t *game) if (error) FATAL_ERROR (error->message); - game->current_tile = game->deck.num_tiles - 1; + /*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->diff_x = game->diff_y = 0; } @@ -477,24 +493,52 @@ static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, 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) ) - { - game->current_tile = i; - game->diff_x = event->x - tile_x; - game->diff_y = event->y - tile_y; - } + 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; + } + } + if (game->current_tile == -1) + { + game->click_x = event->x; + game->click_y = event->y; } - return TRUE; } static gboolean on_button_release_event (GtkWidget *widget, GdkEventButton *event, game_t *game) { + if (game->current_tile == -1) + { + tile_group_t group; + group.num_tiles = 0; + + int i, tile_x, tile_y; + 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++; + } + } + printf("is run %d\n", tile_group_is_run_one(&group) ); + printf("is set %d\n", tile_group_is_set(&group) ); + 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); return TRUE; }