From 9fbd1cbcaf515db5aed676bd6d50f8c47b5244b6 Mon Sep 17 00:00:00 2001 From: Murilo Pereira Date: Fri, 3 Jun 2011 00:07:02 -0300 Subject: [PATCH] Only one reference to symbol strings. --- src/card.h | 42 +++++++++++-------------------- src/display.c | 69 ++++++++++++--------------------------------------- src/display.h | 5 ---- 3 files changed, 31 insertions(+), 85 deletions(-) diff --git a/src/card.h b/src/card.h index 46ec30a..347c442 100644 --- a/src/card.h +++ b/src/card.h @@ -5,34 +5,22 @@ enum value { NO_VALUE = -1, - ACE = 1, - TWO = 2, - THREE = 3, - FOUR = 4, - FIVE = 5, - SIX = 6, - SEVEN = 7, - EIGHT = 8, - NINE = 9, - TEN = 10, - JACK = 11, - QUEEN = 12, - KING = 13 -}; - -enum suit { - NO_SUIT = -1, - DIAMONDS = 0, - SPADES = 1, - HEARTS = 2, - CLUBS = 3 -}; - -enum face { - NO_FACE = -1, - COVERED = 0, - EXPOSED = 1 + ACE, + TWO, + THREE, + FOUR, + FIVE, + SIX, + SEVEN, + EIGHT, + NINE, + TEN, + JACK, + QUEEN, + KING }; +enum suit { NO_SUIT = -1, DIAMONDS, SPADES, HEARTS, CLUBS }; +enum face { NO_FACE = -1, COVERED, EXPOSED }; struct card { struct frame *frame; diff --git a/src/display.c b/src/display.c index 0e46c6e..b6a62f2 100644 --- a/src/display.c +++ b/src/display.c @@ -4,56 +4,15 @@ #include #include #include + #include "game.h" #include "display.h" #include "common.h" -static char *card_suit(enum suit suit) { - char *card_suit; - - if (!(card_suit = malloc(4 * sizeof(*card_suit)))) { - fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); - exit(errno); - } - - switch(suit) { - case DIAMONDS: strcpy(card_suit, DIAMONDS_SYMBOL); break; - case SPADES: strcpy(card_suit, SPADES_SYMBOL); break; - case HEARTS: strcpy(card_suit, HEARTS_SYMBOL); break; - case CLUBS: strcpy(card_suit, CLUBS_SYMBOL); break; - default: strcpy(card_suit, "?"); - } - - return(card_suit); -} - -static char *card_value(enum value value) { - char *card_value; - - if (!(card_value = malloc(2 * sizeof(*card_value)))) { - fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__)); - exit(errno); - } - - switch(value) { - case TWO: card_value = "2"; break; - case THREE: card_value = "3"; break; - case FOUR: card_value = "4"; break; - case FIVE: card_value = "5"; break; - case SIX: card_value = "6"; break; - case SEVEN: card_value = "7"; break; - case EIGHT: card_value = "8"; break; - case NINE: card_value = "9"; break; - case TEN: card_value = "10"; break; - case JACK: card_value = "J"; break; - case QUEEN: card_value = "Q"; break; - case KING: card_value = "K"; break; - case ACE: card_value = "A"; break; - default: card_value = "?"; - } - - return(card_value); -} +static const char *card_suits[4] = { "\u2666", "\u2660", "\u2665", "\u2663" }; +static const char *card_values[13] = { + "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" +}; void erase_card(struct card *card) { werase(card->frame->window); @@ -72,11 +31,11 @@ void erase_stack(struct stack *stack) { } void draw_value(struct card *card) { - mvwprintw(card->frame->window, 0, 0, card_value(card->value)); + mvwprintw(card->frame->window, 0, 0, card_values[card->value]); mvwprintw(card->frame->window, 4, - 6 - (strlen(card_value(card->value)) - 1), - card_value(card->value)); + 7 - strlen(card_values[card->value]), + card_values[card->value]); } void draw_suit(struct card *card) { @@ -85,10 +44,14 @@ void draw_suit(struct card *card) { } else { wattron(card->frame->window, COLOR_PAIR(BLACK_ON_WHITE)); } - mvwprintw(card->frame->window, 0, 1 + (strlen(card_value(card->value) + 1)), - card_suit(card->suit)); - mvwprintw(card->frame->window, 4, 5 - (strlen(card_value(card->value) + 1)), - card_suit(card->suit)); + mvwprintw(card->frame->window, + 0, + strlen(card_values[card->value]), + card_suits[card->suit]); + mvwprintw(card->frame->window, + 4, + 6 - strlen(card_values[card->value]), + card_suits[card->suit]); if (card->suit % 2 == 0) { wattroff(card->frame->window, COLOR_PAIR(RED_ON_WHITE)); } else { diff --git a/src/display.h b/src/display.h index 12133e4..a31c5e5 100644 --- a/src/display.h +++ b/src/display.h @@ -8,11 +8,6 @@ #define EMPTY_STACKS_NUMBER 13 -#define DIAMONDS_SYMBOL "\u2666" -#define SPADES_SYMBOL "\u2660" -#define HEARTS_SYMBOL "\u2665" -#define CLUBS_SYMBOL "\u2663" - #define BLACK_ON_WHITE 1 #define RED_ON_WHITE 2 #define WHITE_ON_BLUE 3