带有Unicode套装的卡片组

时间:2017-10-22 15:28:47

标签: c++ unicode struct

我创造了一副牌用于黑杰克和扑克游戏。我用一个结构来定义卡的属性:

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

我需要将pic设置为每张卡的套装。 我为每件这样的套装都获得了Unicode:

♠= \xe2\x99\xa0

♣= \xe2\x99\xa3

❤= \xe2\x99\xa5

♦= \xe2\x99\xa6

我为四个套装中的每一个创建了一个常量变量,如下所示:

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";

当启动卡类时,我将每张卡的值设置为Ace - King(1-13),如下所示:

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;

}

正如您所看到的,我尝试将每张卡的pic设置为每张卡,但这不起作用。如何将结构中的每个pic设置为正确的unicode?

2 个答案:

答案 0 :(得分:1)

#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;
}

这样你就不会不必要地复制常量字符数组。

答案 1 :(得分:0)

使用memcpy,例如:

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