zsloan112
zsloan112

Reputation: 43

Card Deck with Unicode Suits

I have created a deck of cards to use for a Black Jack and Poker Game. I used a struct to make the properties of the cards defined as such:

struct ACard{
int num;
char pic[4];
};

I need to set the pic as a the suit of each card. I have been given the Unicode for each such suit:

♠ = \xe2\x99\xa0

♣ = \xe2\x99\xa3

❤ = \xe2\x99\xa5

♦ = \xe2\x99\xa6

I have created a constant variable for each of the four suits like so:

const char spade[4] = "\xe2\x99\xa0";
const char club[4] = "\xe2\x99\xa3";
const char heart[4] = "\xe2\x99\xa5";
const char diamond[4] = "\xe2\x99\xa6";

When initiate the the card class I set the value of each card Ace - King (1-13) like so:

deck::deck(){

//Creating the Spade Cards (A-K)

for(int i = 0; i < 14; i++){    //Start loop
    cards[i].num = i + 1;
    //cards[i].pic = spade;
}               //End loop


//Creating the Club Cards (A-K)

for(int i = 13; i < 27; i++){   //Start loop
    cards[i].num = i -12;
    //cards[i].pic = club;
}               //End loop


//Creating the Heart Cards (A-K)

for(int i = 26; i < 40; i++){   //Start loop
    cards[i].num = i-25;    
    //cards[i].pic = heart;
}

//Creating the Diamond Cards (A-K)

for(int i = 39; i < 52; i++){   //Start loop
    cards[i].num = i - 38;
    //cards[i].pic = diamond;
}               //End loop


nextCard = 0;

}

As you can see I try to set the pic of each card to each card, but that doesn't work. How would I set each pic in the struct to the correct unicode?

Upvotes: 0

Views: 593

Answers (2)

Roy
Roy

Reputation: 3267

#include <iostream>

struct ACard{
    int num;
    const char* pic;

    ACard(int _num, const char* _pic) : num{_num}, pic{_pic}
    {
    }
};

const char spade[4] = "\xe2\x99\xa0";
const char club[4] = "\xe2\x99\xa3";
const char heart[4] = "\xe2\x99\xa5";
const char diamond[4] = "\xe2\x99\xa6";

int main()
{
    ACard AceSpades = ACard(12, spade);

    std::cout << "Ace of spades: " << AceSpades.num << " " << AceSpades.pic << std::endl;
}

This way you aren't unnecessarily copying the constant char array.

Upvotes: 1

Killzone Kid
Killzone Kid

Reputation: 6240

Use memcpy, for example:

memcpy(cards[i].pic, spade, 4);

Upvotes: 0

Related Questions