0

我的代码已经制作了一副纸牌,但我该如何洗牌呢?我的随机播放功能似乎不起作用。我可能在其他地方也有一些错误,如果你能看到它们,请告诉我。它编译并运行,但它按顺序列出卡片。

#include <iostream>
#include <string>
#include <ctime>
#include <vector>
using namespace std;

class Card{
public:
    int face;
    int suit;
    void setData(int f, int s){
        face = f;
        suit = s;
    }
    string toString(int F, int S){
        static string faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven",       "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
        static string suits[4] = {"Clubs", "Spades", "Diamonds", "Hearts"};
        string FandS = faces[F] + " of " + suits[S] + "\n";
        return FandS;
    }
};

class DeckOfCards:public Card{
public:
    Card deck[13][4];
    int currentCard;

    void shuffle(){
        srand (time(0));
        Card temp[13][4]; int R, r;
        for(int shuf=0; shuf<52; shuf++){
            for(int i=0; i<13; i++){
                for(int j=0; j<4; j++){
                    R = rand()%13;  
                    r = rand()%4;
                    temp[i][j] = deck[i][j];
                    deck[i][j] = deck[R][r];
                    deck[R][r] = temp[i][j];
                }
            }
        }
    }

    bool moreCards(){
        currentCard=52;
        currentCard--;
        if(currentCard>0){
            return true;
        }else 
            return false;
    }

    void dealCard(){
        for(int i=0; i<13; i++){
            for(int j=0; j<4; j++){
                cout << toString(i, j);
            }
        }
    }

    DeckOfCards(){  
        for(int i=0; i<13; i++){
            for(int j=0; j<4; j++){
                deck[i][j].setData(face, suit);
            }
        }
    }

};

int main(){
    DeckOfCards myDeck;
    myDeck.shuffle();
    myDeck.dealCard();
    return 0;
}
4

3 回答 3

2

这就是您的牌按顺序“发牌”的原因:

void dealCard(){
    for(int i=0; i<13; i++){
        for(int j=0; j<4; j++){
            cout << toString(i, j);
        }
    }
}

你根本不使用甲板。您只需按顺序打印出来。

尝试这个:

cout << toString(deck[i][j].face, deck[i][j].suit);

你真的应该写一个Card::toString没有参数的函数并让它使用它的facesuit成员。

cout << deck[i][j].toString();

郑重声明,我真的不喜欢你,你把你的牌组安排成二维数组。完全没有必要这样做。而且我更不喜欢DeckOfCards继承自Card.

由于我在吹毛求疵,因此您的临时交换变量不需要整个甲板大小的数组。你只需要一个Card。事实上,你应该std::swap改用。

于 2013-02-26T20:48:15.143 回答
1

除了其他答案之外,我认为您对甲板的初始分配不起作用。

DeckOfCards(){  
    for(int i=0; i<13; i++){
        for(int j=0; j<4; j++){
            deck[i][j].setData(face, suit);
         }
    }
}

您将每张牌都设置为“(脸,花色)”。在这一点上是什么?我认为您的意思是将它们设置为“(i,j)”。我很惊讶这个编译,因为 face 和 suit 只被声明为 card 对象的属性。

于 2013-02-26T20:58:08.720 回答
0

我建议您避免使用数组并使用std::vector,并且使用std::random_shuffle来洗牌。

这是我对您的代码所做的快速编辑,以向您展示它是如何完成的

#include <ctime>
#include <vector>
#include <algorithm>

using namespace std;

class Card{
public:
    int face;
    int suit;
    void setData(int f, int s){
        face = f;
        suit = s;
    }
};

class DeckOfCards:public Card{
public:
    std::vector<Card> deck;

    void shuffle(){
        srand (time(0));
        std::random_shuffle(deck.begin(), deck.end());
    }

    DeckOfCards(){  
        deck.reserve(13 * 4);
        for(int i=0; i<13; i++){
            for(int j=0; j<4; j++){
                Card card;
                card.setData(i, j);
                deck.push_back(card);
            }
        }
    }

};

int main(){
    DeckOfCards myDeck;
    myDeck.shuffle();
    return 0;
}
于 2013-02-26T20:49:55.017 回答