C ++:在纸牌游戏中展示套装和排名

时间:2016-11-22 09:23:04

标签: c++

当我试图向玩家展示时,我在打印卡套装和排名方面遇到了问题。我似乎无法弄清楚如何正确地做到这一点。任何提示都会有所帮助。以下是我的代码剪辑

const string RANKS[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
const string SUITS[] = {"Spades", "Hearts", "Diamonds", "Clubs"};

void showHand(vector<int> &deck) {
    cout << endl;

    for (int i = 0; i < 4; i++) {
        cout << "Player " << i + 1 << ": ";
        for (int k = 0; k < 13; k++) {
            cout << card[k][i] << " ";
        }
        cout << endl;
    }
}

在这种情况下,这仅打印卡片的整数值,但不打印相应的套装和等级。我该如何显示它们?

1 个答案:

答案 0 :(得分:0)

首先,你需要将卡片索引转换为两个等级并适合indizes,如两个函数

int suit_index(int card_index){
    return card_index / 13;
}

int rank_index(int card_index){
    return card_index % 13;
}

鉴于此,最内层循环内的打印当前是&#34; cout&lt;&lt;卡[k] [i]&lt;&lt; &#34; &#34 ;;&#34;变

card = card[k][i];
suit = suit_index(card);
rank = rank_index(card);
cout << RANKS[rank] << " of " << SUITS[suit] << " ";

(或者您直接使用suit = card / 13;等等,如果您的转换不同,只想拥有占位符)