2013-02-26 38 views
0

我的代码已经使得扑克牌,但我怎么洗牌呢?我的shuffle功能似乎不起作用。 我可能在其他地方也有一些错误,如果你能看到它们,请让我知道。它编译并运行,但它按顺序列出了卡片。甲板的卡,不能弄清楚如何洗牌

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

'的std :: random_shuffle()'通常是一个很大的可能性。 – WhozCraig 2013-02-26 20:41:56

+4

你是什么意思“它不工作”?这些卡片不是随机排列的?这些卡片根本不存在?重复卡​​片?程序崩溃? – chrisaycock 2013-02-26 20:43:10

+0

你的卡片初始化不正确,也不是你的Card :: toString()成员。 – WhozCraig 2013-02-26 20:51:09

回答

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来代替。

+1

非常感谢,帮助了很多,对不起,如果我的代码让你失望大声笑。我不是很懂电脑的人 – user2112867 2013-02-26 21:15:26

+0

没问题。坚持下去,你最终会减少错误,或者至少培养自己找到和纠正错误的能力。快乐的编码! – paddy 2013-02-26 23:26:47

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; 
} 
+0

我的教授还没有教导我们使用矢量,所以这是希腊人对我哈哈。谢谢,虽然 – user2112867 2013-02-26 21:16:28

1

除了其他的答案,我觉得你在甲板上的初始分配不工作。

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

您正在将每张卡设置为'(face,suit)'。这是什么?我想你的意思是把它们设置为'(i,j)'。我很惊讶这个编译,因为面和西装只被宣布为卡对象的属性。

+0

谢谢,这让我感到非常愚蠢的笑声。尽管我现在已经开始工作了。 – user2112867 2013-02-26 21:15:47