X-Git-Url: https://git.cworth.org/git?p=kub;a=blobdiff_plain;f=kub.c;h=d5bf5b6ce3e29c34345617e5d0e870fb8b24c173;hp=0efcbae7428845b34d7cbd1e56d4b1d26ae7ad75;hb=HEAD;hpb=1e67ae2cb7b0cd02b94b2a1127dacfe5048ba6e4 diff --git a/kub.c b/kub.c index 0efcbae..d5bf5b6 100644 --- a/kub.c +++ b/kub.c @@ -5,239 +5,392 @@ #include #include -#define FATAL_ERROR(msg) \ +/* + * TILE_NUMBER_X_OFFSET = 3 + * // + * || TILE_NUMBER_WIDTH = 34 + * ||/ / + * || | TILE_WIDTH = 40 + * || | / + * | | + * +-----+ - TILE_NUMBER_Y_OFFSET = 3 - + * |+---+| - | + * || || | + * || || TILE_NUMBER_HEIGHT = 24 | + * |+---+| - | + * | _ | _ |-TILE_HEIGHT = 50 + * | / \ | | + * || || TILE_SUN_HEIGHT = 20 | + * | \_/ | _ | + * | | TILE_SUN_Y_OFFSET = 3 | + * +-----+ - - + * || | TILE_SUN_WIDTH = 20 + * || + * || TILE_SUN_X_OFFSET = 10 + */ +#define TILE_WIDTH 40 +#define TILE_HEIGHT 50 + +#define TILE_NUMBER_X_OFFSET 3 +#define TILE_NUMBER_Y_OFFSET 3 +#define TILE_NUMBER_WIDTH 34 +#define TILE_NUMBER_HEIGHT 24 + +#define TILE_SUN_X_OFFSET 10 +#define TILE_SUN_Y_OFFSET 3 +#define TILE_SUN_WIDTH 20 +#define TILE_SUN_HEIGHT 20 + +#define FATAL_ERROR(msg) \ do { fprintf (stderr, "Error: %s\n", msg); exit (1); } while (0) char *colors[] = {"Black", "Blue", "Red", "Yellow"}; typedef enum {BLACK, BLUE, RED, YELLOW} color_t; -typedef struct card { +typedef struct tile { color_t color; int number; -} card_t; + int x; + int y; + int selected; + int owned; + int in_hand;//Draw the tile in the hand, or elsewhere on the board? +} tile_t; -#define DECK_MAX_CARDS 104 +#define DECK_MAX_TILES 104 typedef struct deck { - card_t cards[DECK_MAX_CARDS]; - int num_cards; + tile_t tiles[DECK_MAX_TILES]; + int num_tiles; } deck_t; -#define CARD_GROUP_MAX_CARDS DECK_MAX_CARDS +#define TILE_GROUP_MAX_TILES DECK_MAX_TILES -typedef struct card_group { - card_t cards[CARD_GROUP_MAX_CARDS]; - int num_cards; -} card_group_t; +typedef struct tile_group { + tile_t tiles[TILE_GROUP_MAX_TILES]; + int num_tiles; +} tile_group_t; -#define BOARD_MAX_CARD_GROUPS (DECK_MAX_CARDS / 3) +#define BOARD_MAX_TILE_GROUPS DECK_MAX_TILES typedef struct board { - card_group_t groups[BOARD_MAX_CARD_GROUPS]; + tile_group_t groups[BOARD_MAX_TILE_GROUPS]; int num_groups; } board_t; typedef struct player { - card_group_t hand; + 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 +typedef struct state { + player_t players[GAME_MAX_PLAYERS]; + board_t board; + deck_t deck; +} state_t; + typedef struct game { player_t players[GAME_MAX_PLAYERS]; int num_players; board_t board; deck_t deck; + selection_box_t selection_box; + state_t state; RsvgHandle *blanktile; + RsvgHandle *selectedtile; + RsvgHandle *ownedtile; + + int current_player; + 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; /*Currently unused*/ } game_t; -static void card_print(card_t card) +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) { - printf("%6s %2d\n", colors[card.color], card.number + 1); + tile->color = color; + tile->number = number; + tile->x = 0; + tile->y = 0; + tile->selected = 0; + tile->owned = 0; + tile->in_hand = 1; } -static void card_draw(game_t *game, card_t *card, cairo_t *cr, int x, int y) +static void tile_set_x_y (tile_t *tile, int x, int y) +{ + tile->x = x; + tile->y = y; +} + +static void tile_print(tile_t tile) +{ + printf("%6s %2d\n", colors[tile.color], tile.number + 1); +} + +static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, GdkRegion *region) { char number_string[3]; int len; + GdkRectangle rectangle; - len = snprintf (number_string, 3, "%d", card->number + 1); + rectangle.x = tile->x - 1; + rectangle.y = tile->y - 1; + rectangle.width = TILE_WIDTH + 2; + rectangle.height = TILE_HEIGHT + 2; + if (gdk_region_rect_in (region, &rectangle) == GDK_OVERLAP_RECTANGLE_OUT) + return; + + len = snprintf (number_string, 3, "%d", tile->number + 1); if (len < 0 || len >= 3) FATAL_ERROR ("snprintf failed"); cairo_save(cr); - cairo_translate(cr, x, y); - rsvg_handle_render_cairo (game->blanktile, cr); - - if (card->color == BLACK) - cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); - if (card->color == BLUE) - cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); - if (card->color == RED) - cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); - if (card->color == YELLOW) + cairo_translate(cr, tile->x, tile->y); + + 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); + if (tile->color == BLUE) + 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); + if (tile->color == YELLOW) cairo_set_source_rgb (cr, 1.0, .843, 0.0); - cairo_move_to (cr, 10, 25); + 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); - + cairo_restore(cr); } -static void card_group_init(card_group_t *card_group) +static void tile_group_init(tile_group_t *tile_group) { - card_group->num_cards = 0; + tile_group->num_tiles = 0; } static void board_init(board_t *board) { int i; board->num_groups = 0; - - for (i = 0; i <= BOARD_MAX_CARD_GROUPS; ++i) + + for (i = 0; i <= BOARD_MAX_TILE_GROUPS; ++i) { - card_group_init(&board->groups[i]); + tile_group_init(&board->groups[i]); } } static void player_init(player_t *player) { - card_group_init(&player->hand); + tile_group_init(&player->hand); } -/* If card_one < card_two, then return value will be negative +/* If tile_one < tile_two, then return value will be negative if they are equal, 0 will be returned, - if card_one > card_two, then return value will be positive */ -static int card_compare(const void *one, const void *two) + if tile_one > tile_two, then return value will be positive */ +static int tile_compare(const void *one, const void *two) +{ + const tile_t *tile_one = one; + const tile_t *tile_two = two; + return tile_one->number - tile_two->number; //Sort lowest to highest +} + +static int int_compare(const void *vx, const void *vy) { - const card_t *card_one = one; - const card_t *card_two = two; - return card_one->number - card_two->number; + const int *x = vx, *y = vy; + return *y - *x; //Sort highest to lowest } -static int card_group_is_run_one(card_group_t *card_group) +/* +static int tile_in_box(game_t *game, tile_t *tile) +{ + +} +*/ + +static int tile_group_is_run_one(tile_group_t *tile_group) { int i; - qsort (&card_group->cards[0], card_group->num_cards, - sizeof (card_t), card_compare); - - if (card_group->num_cards > 13 || card_group->num_cards < 3) + 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 < card_group->num_cards - 1; ++i) - if(card_group->cards[i].color != card_group->cards[i + 1].color) - { + 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(card_group->cards[i].number != card_group->cards[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; } -static int card_group_is_run_two(card_group_t *card_group) +static int tile_group_is_run_two(tile_group_t *tile_group) { int i; int lowest = 14, highest = 0; color_t run_color; - /* By definition, a run must have at least 3 cards. Also, it's - * impossible for any group of cards with more than 13 cards to be + /* By definition, a run must have at least 3 tiles. Also, it's + * impossible for any group of tiles with more than 13 tiles to be * a run, (there are only 13 unique numbers so a group with more - * than 13 cards must have some duplicates). + * than 13 tiles must have some duplicates). */ - if (card_group->num_cards < 3 || card_group->num_cards > 13) + if (tile_group->num_tiles < 3 || tile_group->num_tiles > 13) { return 0; } - /* Loop through all cards in the group, ensuring that they are all + /* Loop through all tiles in the group, ensuring that they are all * the same color and finding the highest and lowest number in the * group. */ - run_color = card_group->cards[0].color; + run_color = tile_group->tiles[0].color; - for (i = 0; i < card_group->num_cards; i++) + for (i = 0; i < tile_group->num_tiles; i++) { - if (card_group->cards[i].color != run_color) + if (tile_group->tiles[i].color != run_color) return 0; - if (card_group->cards[i].number > highest) + if (tile_group->tiles[i].number > highest) { - highest = card_group->cards[i].number; + highest = tile_group->tiles[i].number; } - if (card_group->cards[i].number < lowest) + if (tile_group->tiles[i].number < lowest) { - lowest = card_group->cards[i].number; + lowest = tile_group->tiles[i].number; } } - /* For a run, the difference between the highest and lowest cards - * will always be one less than the number of cards in the + /* For a run, the difference between the highest and lowest tiles + * will always be one less than the number of tiles in the * group. If not then we know it's not a run. */ - if (highest - lowest != card_group->num_cards - 1) + if (highest - lowest != tile_group->num_tiles - 1) { return 0; } /* XXX: There's a bug here. We're guessing that at this point * anything we're looking at must be a run. This would be correct - * if there were no duplicate cards, but since there are + * if there were no duplicate tiles, but since there are * duplicates this us quite broken. For example consider two - * sequences of entirely red cards: + * sequences of entirely red tiles: * * This is a run: 1, 2, 3, 4 * But this is not: 1, 3, 4, 4 * * As currently written, this function will consider both of these * groups to be a run. One possible fix is to throw away the - * highest - lowest heuristic and instead simply sort the cards up + * highest - lowest heuristic and instead simply sort the tiles up * front and ensure the difference between each adjacent pair is * exactly 1. */ return 1; } -static int card_group_is_set(card_group_t *card_group) +static int tile_group_is_set(tile_group_t *tile_group) { int i; - color_t seen_color[card_group->num_cards]; - - if (card_group->num_cards > 4 || card_group->num_cards < 3) + 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 < card_group->num_cards - 1; ++i) + for (i = 0; i <= tile_group->num_tiles - 1; ++i) { - if (card_group->cards[i].number != card_group->cards[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] = card_group->cards[i].color; - for (i = 0; i < card_group->num_cards; ++i) + for (i = 0; i < 4; i++) { - seen_color[card_group->cards[i].color]++; - if (seen_color[card_group->cards[i].color] > 1) - { + if (seen_color[i] > 1) + { + printf("fail set - repeat color; "); return 0; - } + } } return 1; } +static gboolean board_valid(board_t *board) +{ + int i; + gboolean valid = TRUE; + for (i = 0; i < board->num_groups; ++i) + { + if (!(tile_group_is_run_one(&board->groups[i])) && + !(tile_group_is_set(&board->groups[i]))) + { + valid = FALSE; + } + } + return valid; +} + static void deck_deal(game_t *game, deck_t *deck) { - card_t temp; - int rand_card; + 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) @@ -245,19 +398,20 @@ 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) { - rand_card = ((deck->num_cards + 1.0) * rand()) / (RAND_MAX + 1.0); - temp = deck->cards[rand_card]; - deck->cards[rand_card] = deck->cards[deck->num_cards - 1]; - game->players[i].hand.cards[j] = temp; - deck->num_cards -= 1; - game->players[i].hand.num_cards += 1; + rand_tile = ((deck->num_tiles + 1.0) * rand()) / (RAND_MAX + 1.0); + 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; } } printf ("Game dealt for %d player(s)\n", game->num_players); @@ -265,96 +419,187 @@ static void deck_deal(game_t *game, deck_t *deck) static void deck_init(deck_t *deck) { - int h, i, j; - deck->num_cards = 0; + 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) { - deck->cards[j + (i * 13) + (h * 52)].color = i; - deck->cards[j + (i * 13) + (h * 52)].number = j; - deck->num_cards += 1; - printf ("There are %d tiles in the deck\n", deck->num_cards); + 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) { - card_t temp; - int rand_card; + tile_t temp; + int rand_tile; int last; - for (last = deck->num_cards; last > 0; --last) + for (last = deck->num_tiles; last > 0; --last) { - rand_card = ((last + 1.0) * rand()) / (RAND_MAX + 1.0); - temp = deck->cards[rand_card]; - deck->cards[rand_card] = deck->cards[last - 1]; - deck->cards[last - 1] = temp; + rand_tile = ((last + 1.0) * rand()) / (RAND_MAX + 1.0); + temp = deck->tiles[rand_tile]; + deck->tiles[rand_tile] = deck->tiles[last - 1]; + deck->tiles[last - 1] = temp; } } 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) { for (j = 0; j < 13; ++j) { - card_print(deck->cards[j + (i * 13) + (h * 52)]); + tile_print(deck->tiles[j + (i * 13) + (h * 52)]); } } } - printf ("There are %d tiles in the deck\n" , deck->num_cards); + printf ("There are %d tiles in the deck\n" , deck->num_tiles); } -static void deck_draw(game_t *game, cairo_t *cr) +static void deck_spread(deck_t *deck) { int i, j; - - for (i = 0; i < 8; ++i) + for (i = 0; i < 8; i++) { - for (j = 0; j < 13; ++j) + for (j = 0; j < 13; j++) { - card_draw(game, &game->deck.cards[j + (i * 13)], - cr, 45 * j, 55 * i); + deck->tiles[j + (i * 13)].x = j * 50; + deck->tiles[j + (i * 13)].y = i * 60; } } -} +} -static void hand_print(game_t *game) +static void deck_draw(game_t *game, cairo_t *cr, GdkRegion *region) { int i; - for (i = 0; i < game->players[0].hand.num_cards; ++i) + for (i = 0; i < game->deck.num_tiles; i++) + { + tile_draw(game, &game->deck.tiles[i], cr, region); + } +} + +static void board_draw(game_t *game, cairo_t *cr, GdkRegion *region) +{ + int i, j; + for (i = 0; i < game->board.num_groups; i++) + { + for (j = 0; j < game->board.groups[i].num_tiles; j++) + { + tile_draw(game, &game->board.groups[i].tiles[j], cr, region); + //tile_print(game->board.groups[i].tiles[j]); + } + } +} + +static void board_print(game_t *game) +{ + int i, j; + printf("\tBegin board print\n"); + for (i = 0; i < game->board.num_groups; i++) + { + printf("\tBegin group %d\n", i); + for (j = 0; j < game->board.groups[i].num_tiles; j++) + { + tile_print(game->board.groups[i].tiles[j]); + } + printf("\tEnd group %d\n", i); + } +} + +static void hand_print(game_t *game, int player) +{ + int i; + for (i = 0; i < game->players[player].hand.num_tiles; i++) + { + tile_print(game->players[player].hand.tiles[i]); + } +} + +static void hand_draw(game_t *game, int player, cairo_t *cr, GdkRegion *region, GtkWidget *widget) +{ + int i; + int width = widget->allocation.width; +// int width = GAME_WINDOW_DEFAULT_WIDTH; + int height = widget->allocation.height; +// int height = GAME_WINDOW_DEFAULT_HEIGHT; + int num_tiles = game->players[player].hand.num_tiles; + for (i = 0; i < num_tiles; i++) + { + if (game->players[player].hand.tiles[i].in_hand) + tile_set_x_y(&game->players[player].hand.tiles[i], + ((width/num_tiles)) * i, (height - TILE_HEIGHT - 6) ); + } + for (i = 0; i < game->players[player].hand.num_tiles; i++) { - card_print(game->players[0].hand.cards[i]); +// if (game->players[player].hand.tiles[i].in_hand) + tile_draw(game, &game->players[player].hand.tiles[i], cr, region); } } +static void save_state(game_t *game) +{ + printf("State saved\n"); + game->state.board = game->board; + game->state.deck = game->deck; + int i; + for (i=0; i < game->num_players; i++) + game->state.players[i] = game->players[i]; +} + +static void restore_state(game_t *game) +{ + game->board = game->state.board; + game->deck = game->state.deck; + int i; + for (i=0; i < game->num_players; i++) + game->players[i] = game->state.players[i]; +} + static void game_init(game_t *game) { int i; GError *error = NULL; game->num_players = 0; - + for (i = 0; i < GAME_MAX_PLAYERS; ++i) { player_init(&game->players[i]); game->num_players += 1; } + game->current_player = 0; + 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); + + 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; } static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_t *game) @@ -363,48 +608,381 @@ static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event, game_ cr = gdk_cairo_create (widget->window); - deck_draw(game, cr); + deck_draw(game, cr, event->region); + board_draw(game, cr, event->region); + + if (game->selection_box.visible) + selection_box_draw(&game->selection_box, cr); + + hand_draw(game, game->current_player, cr, event->region, widget); + //hand_draw(game, 0, cr, event->region); cairo_destroy (cr); return TRUE; } +/* +static gboolean on_configure_event (GtkWidget *widget, GdkEventConfigure *event, game_t *game) +{ + cairo_t *cr; + + cr = gdk_cairo_create (event->window); + hand_draw(game, game->current_player, cr, event->window, widget); +// gtk_widget_queue_draw(widget); +} +*/ static gboolean on_key_press_event (GtkWidget *widget, GdkEventKey *event, game_t *game) { + player_t *player = &game->players[game->current_player]; + cairo_t *cr; + cr = gdk_cairo_create (widget->window); printf ("You pressed key %d\n", event->keyval); + if (event->keyval == 100) //HIT "d" + { + //Draw(take) top tile from deck and put in hand + if (game->deck.num_tiles > 0) + { + tile_t top_tile = game->deck.tiles[game->deck.num_tiles-1]; + game->deck.num_tiles--; + player->hand.tiles[player->hand.num_tiles] = top_tile; + player->hand.tiles[player->hand.num_tiles].owned=1; + player->hand.num_tiles++; + printf("Tile added to hand\n"); + gtk_widget_queue_draw(widget); + } + } + else if (event->keyval == 115) //HIT "s" + save_state(game); + else if (event->keyval == 65293) //HIT ENTER + { + player_t *player = &game->players[game->current_player]; + int i; + for (i = 0; i < player->hand.num_tiles; i++) + player->hand.tiles[i].in_hand = 1; + save_state(game); + printf ("\tEnd of player %d's turn\n", game->current_player+1); + if (game->current_player == game->num_players-1) + game->current_player = 0; + else + game->current_player += 1; + gtk_widget_queue_draw(widget); + } + else if (event->keyval == 65307) //HIT ESCAPE + { + restore_state(game); + printf ("\tChanges Reverted\n"); + gtk_widget_queue_draw(widget); + } + else if (event->keyval == 65474) //HIT "F5" + { + gtk_widget_queue_draw(widget); + } + else if (event->keyval == 65505 || event->keyval == 65506) + game->drag_group_mode = 1; return TRUE; } static gboolean on_button_press_event (GtkWidget *widget, GdkEventButton *event, game_t *game) { - printf ("You pressed button %d\n", event->button); + int i, tile_x, tile_y; + tile_t *curr_tile; + player_t *curr_player = &game->players[game->current_player]; + /*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; + curr_tile->in_hand = 0; + 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++) + { + 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->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->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) { - printf ("You released button %d\n", event->button); + if (game->select_mode) + { + 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; + + int i, tile_x, tile_y, tile_x2, tile_y2; + int tiles_to_remove[game->deck.num_tiles]; + for (i = 0; i < game->deck.num_tiles; i++) + { + 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*/ + (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 of tile selected*/ + (x_min >= tile_x && x_min <= tile_x2 && + y_min >= tile_y && y_min <= tile_y) ) + { + tiles_to_remove[group.num_tiles] = i; + + 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) ); + + qsort (tiles_to_remove, group.num_tiles, sizeof (int), int_compare); + + for (i = 0; i < group.num_tiles; i++) + { + tile_print(group.tiles[i]); + gtk_widget_queue_draw_area (widget, group.tiles[i].x - 1, + group.tiles[i].y - 1, TILE_WIDTH + 1, + TILE_HEIGHT + 2); + + group.tiles[i].x = x_min + (i * (TILE_WIDTH)); + group.tiles[i].y = y_min; + + gtk_widget_queue_draw_area (widget, group.tiles[i].x - 1, + group.tiles[i].y - 1, TILE_WIDTH + 1, + TILE_HEIGHT + 2); + + //Remove tile from deck + if (tiles_to_remove[i] != game->deck.num_tiles - 1) + game->deck.tiles[tiles_to_remove[i]] = game->deck.tiles[game->deck.num_tiles-1]; + game->deck.num_tiles--; + } + + if (group.num_tiles > 0) + { + game->board.groups[game->board.num_groups] = group; + game->board.num_groups++; + } + board_print(game); + printf("\nBut is the board valid?\t\t%s\n", board_valid(&game->board) ? "yes" : "no"); + } + game->select_mode = 1; return TRUE; } +static gboolean on_button_motion_event (GtkWidget *widget, GdkEventMotion *event, + game_t *game, cairo_t *cr) +{ + 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]; + + 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*/ + (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 of tile selected*/ + (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 + { + 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); + } + } + } + } + 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; + + /* 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); + } + return TRUE; +} + 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_deal(&game, &game.deck); - hand_print(&game); - deck_print(&game.deck); + deck_spread(&game.deck); + deck_deal(&game, &game.deck); + + game.state.board = game.board; + game.state.deck = game.deck; + int i; + for (i=0; i < game.num_players; i++) + game.state.players[i] = game.players[i]; + + //hand_print(&game, 0); //With Zero being passed, will print hand for player 1(players[0]) + //deck_print(&game.deck); /* Create a new window */ window = gtk_window_new (GTK_WINDOW_TOPLEVEL); @@ -414,20 +992,26 @@ int main(int argc, char *argv[]) gtk_widget_set_events (window, GDK_EXPOSURE_MASK | - GDK_KEY_PRESS_MASK | - GDK_BUTTON_PRESS_MASK | + GDK_KEY_PRESS_MASK | + GDK_BUTTON_MOTION_MASK | + GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK); g_signal_connect (G_OBJECT (window), "delete_event", G_CALLBACK (gtk_main_quit), NULL); g_signal_connect (G_OBJECT (window), "expose_event", G_CALLBACK (on_expose_event), &game); +// g_signal_connect (G_OBJECT (window), "configure_event", +// G_CALLBACK (on_configure_event), &game); g_signal_connect (G_OBJECT (window), "key_press_event", G_CALLBACK (on_key_press_event), &game); g_signal_connect (G_OBJECT (window), "button_press_event", G_CALLBACK (on_button_press_event), &game); g_signal_connect (G_OBJECT (window), "button_release_event", G_CALLBACK (on_button_release_event), &game); + g_signal_connect (G_OBJECT (window), "motion_notify_event", + G_CALLBACK (on_button_motion_event), &game); + gtk_widget_show_all (window); gtk_main ();