Thread: Card game, having a problem with strings.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    33

    Card game, having a problem with strings.

    In the shuffleDeck function, I'm printing out "cards" in the while loop and I'm getting junk after some of the strings along with a segfault at the bottom of the list. I tried using realloc, in order to reallocate the string to the size of the new string being copied to it, but that didn't do it. Any help? Thanks.
    Code:
    void setCards(char **cards){ 
        char *suit[SUIT_MAX] = {"Clubs", "Diamonds", "Hearts", "Spades"};
        char *rank[RANK_MAX] = {"Ace", "2", "3", "4", "5", "6", 
            "7", "8", "9", "10", "Jack", "Queen", "King"};
    
    
        int rankIdx, suitIdx, cardIdx;
        for(suitIdx = 0; suitIdx < SUIT_MAX; suitIdx++){
            for(rankIdx = 0; rankIdx < RANK_MAX; rankIdx++){
                cardIdx = suitIdx * RANK_MAX + rankIdx;
                cards[cardIdx] = malloc(strlen(suit[suitIdx]) + strlen(rank[rankIdx]) + 4);
                sprintf(cards[cardIdx], "%s of %s", rank[rankIdx], suit[suitIdx]);
                cards[cardIdx][strlen(cards[cardIdx])] = '\0';
            }
        }
    
    
    }
    
    
    void shuffleDeck(char **cards){
        int cardIdx = RANK_MAX * SUIT_MAX;
        char *temp;
        while(cardIdx > 1){
            int random = rand() % cardIdx;
            cardIdx--;
            temp = malloc(strlen(cards[cardIdx]));
            strcpy(temp, cards[cardIdx]);
            temp[strlen(temp)] = '\0';
            cards[cardIdx] = realloc(cards[cardIdx], strlen(cards[random])); <-- ??
            strcpy(cards[cardIdx], cards[random]);
            strcpy(cards[random], temp);
            free(temp);
            puts(cards[cardIdx]);
        }
    
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    You're not leaving any room for the terminating '\0' when you allocate memory. The last part of this statement should be at least +5, but a bit more won't hurt:

    Code:
        cards[cardIdx] = malloc(strlen(suit[suitIdx]) + strlen(rank[rankIdx]) + 8);
    All this does is store a '\0' where strlen() already found a '\0'.

    Code:
        cards[cardIdx][strlen(cards[cardIdx])] = '\0';

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your main problem is that you need to add 5 not 4 in this line, since you need one extra for the zero-terminator:
    Code:
    cards[cardIdx] = malloc(strlen(suit[suitIdx]) + strlen(rank[rankIdx]) + 4);
    Since sprintf will ensure the string is zero-terminated, you don't need this line:
    Code:
    cards[cardIdx][strlen(cards[cardIdx])] = '\0';
    shuffleDeck can be simplified by just swapping the pointers.
    Code:
    void shuffleDeck(char **cards){
        int cardIdx = RANK_MAX * SUIT_MAX;
        char *temp;
        while(cardIdx > 1){
            int random = rand() % cardIdx;
            cardIdx--;
            temp = cards[cardIdx];
            cards[cardIdx] = cards[random];
            cards[random] = temp;
            puts(cards[cardIdx]);
        } 
    }
    EDIT: That rcgldr is quick!

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    33
    *Smacks forehead*

    Thank you both, good suggestion ooga!

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    502
    Quote Originally Posted by atac View Post
    *Smacks forehead*

    Thank you both, good suggestion ooga!
    In fact what you want to do is to write a generic shuffle, which is the exact reverse of qsort.
    qsort() sorts an arbitrary array of objects of arbitrary width, shuffle() mixes them up again.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. War card game
    By bigzcoder in forum C Programming
    Replies: 3
    Last Post: 04-19-2012, 09:38 PM
  2. Card game war
    By Dr Saucie in forum C Programming
    Replies: 3
    Last Post: 02-11-2010, 11:25 PM
  3. Card game help
    By aaroroge in forum Game Programming
    Replies: 9
    Last Post: 07-16-2005, 06:37 PM
  4. card game
    By rugger78 in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2004, 04:50 PM
  5. Bridge Card Game
    By IneedHelp in forum C++ Programming
    Replies: 0
    Last Post: 02-09-2003, 06:32 PM