Search code examples
arrayscpokerplaying-cards

Create a deck of cards with Faces and Suits using int Arrays


I am trying to create a simple poker game Texas Hold'em style with .

At first, I tried to create a deck of cards with 2 char arrays:

char *suits_str[4] = {"Spades", "Hearts", "Diamonds", "Clubs"};
char *faces_str[13] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", 
                       "J", "Q", "K", "A"};

Problem

Everything went well, it was pretty simple to represent the cards. But when it come to analyzing the hands to determine the winner, it seems like using char type values was a pretty bad idea.

So I want to change the data type of the cards into int:

  • Suits: 0="Clubs", 1="Diamonds", 2="Hearts", 3="Spades"
  • Faces: 0="2", 1="3", 11="King", 12="Ace"

Example:

int suits[4] = {0, 1, 2, 3};
int faces[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

But now I don't know how to convert those int values back into their matching string values of the cards.

Question

What is the correct and simple approach to represent the cards using int Arrays?


Solution

  • As you can see from the initializer, you do not need arrays to represent the card values and faces, simple numeric values between resp. 0 and 12 for values and 0 and 3 for faces can be used. To convert to strings, use the card value and face as an index into the arrays suits_str and faces_str:

    int suit = 0;   /* spades */
    int face = 12;  /* ace */
    
    printf("I am the %s of %s\n", suits_str[suit], faces_str[face]);
    

    You can use enums to make you cade more readable:

    enum suits { SPADES, HEARTS, DIAMONDS, CLUBS };
    enum faces { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
                 JACK, QUEEN, KING, ACE };
    int suit = SPADES;
    int face = ACE;
    
    printf("I am the %s of %s\n", suits_str[suit], faces_str[face]);
    

    Another idea is to number cards from 0 to 51 and use formulas to extract the face and suit:

    int card = rand() % 52;
    int suit = card / 13;
    int face = card % 13;
    
    printf("I am the %s of %s\n", suits_str[suit], faces_str[face]);
    

    You could create a deck of cards by initializing an array of 52 ints and shuffling it with a simple method:

    int deck[52];
    for (int i = 0; i < 52; i++) {
         /* start with a sorted deck */
         deck[i] = i;
    }
    
    for (int i = 0; i < 1000; i++) {
        /* shuffle by swapping cards pseudo-randomly a 1000 times */
        int from = rand() % 52;
        int to = rand() % 52;
        int card = deck[from];
        deck[from] = deck[to];
        deck[to] = card;
    }
    
    printf("Here is a shuffled deck:\n"
    for (int i = 0; i < 52; i++) {
         int card = deck[i];
         int suit = card / 13;
         int face = card % 13;
         printf("%s of %s\n", suits_str[suit], faces_str[face]);
    }