From ab303017f7875e536abebf83acb864ec2de52e3d Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 1 Mar 2007 06:43:00 -0800 Subject: [PATCH] Rename card to tile --- kub.c | 184 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/kub.c b/kub.c index 0efcbae..bad09fd 100644 --- a/kub.c +++ b/kub.c @@ -12,34 +12,34 @@ 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; +} 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 / 3) 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; #define GAME_MAX_PLAYERS 4 @@ -54,17 +54,17 @@ typedef struct game { RsvgHandle *blanktile; } game_t; -static void card_print(card_t card) +static void tile_print(tile_t tile) { - printf("%6s %2d\n", colors[card.color], card.number + 1); + printf("%6s %2d\n", colors[tile.color], tile.number + 1); } -static void card_draw(game_t *game, card_t *card, cairo_t *cr, int x, int y) +static void tile_draw(game_t *game, tile_t *tile, cairo_t *cr, int x, int y) { char number_string[3]; int len; - len = snprintf (number_string, 3, "%d", card->number + 1); + len = snprintf (number_string, 3, "%d", tile->number + 1); if (len < 0 || len >= 3) FATAL_ERROR ("snprintf failed"); @@ -72,13 +72,13 @@ static void card_draw(game_t *game, card_t *card, cairo_t *cr, int x, int y) cairo_translate(cr, x, y); rsvg_handle_render_cairo (game->blanktile, cr); - if (card->color == BLACK) + if (tile->color == BLACK) cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); - if (card->color == BLUE) + if (tile->color == BLUE) cairo_set_source_rgb (cr, 0.0, 0.0, 1.0); - if (card->color == RED) + if (tile->color == RED) cairo_set_source_rgb (cr, 1.0, 0.0, 0.0); - if (card->color == YELLOW) + if (tile->color == YELLOW) cairo_set_source_rgb (cr, 1.0, .843, 0.0); cairo_move_to (cr, 10, 25); cairo_set_font_size(cr, 25); @@ -87,9 +87,9 @@ static void card_draw(game_t *game, card_t *card, cairo_t *cr, int x, int y) 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) @@ -97,44 +97,44 @@ 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 card_t *card_one = one; - const card_t *card_two = two; - return card_one->number - card_two->number; + const tile_t *tile_one = one; + const tile_t *tile_two = two; + return tile_one->number - tile_two->number; } -static int card_group_is_run_one(card_group_t *card_group) +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); + qsort (&tile_group->tiles[0], tile_group->num_tiles, + sizeof (tile_t), tile_compare); - if (card_group->num_cards > 13 || card_group->num_cards < 3) + if (tile_group->num_tiles > 13 || tile_group->num_tiles < 3) { 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) { 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) { return 0; } @@ -142,89 +142,89 @@ static int card_group_is_run_one(card_group_t *card_group) } -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]; + color_t seen_color[tile_group->num_tiles]; - if (card_group->num_cards > 4 || card_group->num_cards < 3) + if (tile_group->num_tiles > 4 || tile_group->num_tiles < 3) { 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) { return 0; } } - seen_color[i] = card_group->cards[i].color; - for (i = 0; i < card_group->num_cards; ++i) + seen_color[i] = tile_group->tiles[i].color; + for (i = 0; i < tile_group->num_tiles; ++i) { - seen_color[card_group->cards[i].color]++; - if (seen_color[card_group->cards[i].color] > 1) + seen_color[tile_group->tiles[i].color]++; + if (seen_color[tile_group->tiles[i].color] > 1) { return 0; } @@ -234,8 +234,8 @@ static int card_group_is_set(card_group_t *card_group) 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? "); @@ -252,12 +252,12 @@ static void deck_deal(game_t *game, deck_t *deck) { 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; + deck->num_tiles -= 1; + game->players[i].hand.num_tiles += 1; } } printf ("Game dealt for %d player(s)\n", game->num_players); @@ -266,17 +266,17 @@ 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; + deck->num_tiles = 0; for (h = 0; h <= 1; ++h) { for (i = 0; i <= 3; ++i) { 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); + deck->tiles[j + (i * 13) + (h * 52)].color = i; + deck->tiles[j + (i * 13) + (h * 52)].number = j; + deck->num_tiles += 1; + printf ("There are %d tiles in the deck\n", deck->num_tiles); } } } @@ -284,15 +284,15 @@ static void deck_init(deck_t *deck) 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; } } @@ -305,11 +305,11 @@ static void deck_print(deck_t *deck) { 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) @@ -320,7 +320,7 @@ static void deck_draw(game_t *game, cairo_t *cr) { for (j = 0; j < 13; ++j) { - card_draw(game, &game->deck.cards[j + (i * 13)], + tile_draw(game, &game->deck.tiles[j + (i * 13)], cr, 45 * j, 55 * i); } } @@ -329,9 +329,9 @@ static void deck_draw(game_t *game, cairo_t *cr) static void hand_print(game_t *game) { int i; - for (i = 0; i < game->players[0].hand.num_cards; ++i) + for (i = 0; i < game->players[0].hand.num_tiles; ++i) { - card_print(game->players[0].hand.cards[i]); + tile_print(game->players[0].hand.tiles[i]); } } -- 2.43.0