为C ++模拟和改组一副牌?

时间:2012-11-05 20:48:21

标签: c++ shuffle

我是C ++的新手,我需要模拟一副牌,并能够从牌组中随机抽出一张牌。 但我的编码有问题:

  #include <iostream>
#include <cstdlib> //for rand and srand
#include <cstdio>
using namespace std;

string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
string facevalue[] = {"Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "King", "Ace"};

string getcard()
{string card;
int cardvalue = rand()%12;
int cardsuit = rand()%4;
card += facevalue[cardvalue];
card += "of";
card += suit[cardsuit];
return card;
}

int main ()
{int numberofcards = 0;
for (int = 0; i < numberofcards; i++)
{cout << "You drew a" << getcard() << endl;}

}

当我尝试编译它时说:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

我不确定我做错了什么。

此外,我不知道如何制作我的节目,所以它只画一张卡而不是无限制。

有什么建议吗?

4 个答案:

答案 0 :(得分:3)

你忘了

#include <string>

答案 1 :(得分:2)

正如其他答案所说,您需要在文件顶部添加“#include”来包含“字符串”库。你忘了在你的for循环中声明“i”。您的facevalue数组缺少“Queen”,一旦添加“Queen”,您还需要更改生成cardvalue的方式,使其为rand()%13。 此外,为了确保您不会同时处理相同的卡,您需要跟踪已经处理了哪些卡,然后包括一张getcard()的支票,该卡将绘制当前卡的新卡已经绘制(您可能需要多次绘制以获得未绘制的卡片)。

这是一种快速简单的检查方法(我修改并更正了您的代码以显示它,但请记住,这不是最好的方法):

#include <iostream>
#include <string>
#include <cstdlib> //for rand and srand
#include <cstdio>
using namespace std;

string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
string facevalue[] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
int numberOfCardsDrawn = 0;
string drawnCards[52];

string drawCard () {
    string card;
    int cardvalue = rand()%13;
    int cardsuit = rand()%4;
    card += facevalue[cardvalue];
    card += " of ";
    card += suit[cardsuit];
    return card;
}

bool isMyCardAlreadyDrawn (string card) {
    for(int i = 0; i < 52; i++) {
        if(card.compare(drawnCards[i]) == 0) { // if this is true, then both strings are the same
            return true;
        }
    }
    return false; // if the code reaches this point, then the card has not been drawn yet
}

string getCard () {
    string card = drawCard();
    while (isMyCardAlreadyDrawn(card) == true) { // keep drawing until an undrawn card is found
        card = drawCard(); // draw a new card
    }
    drawnCards[numberOfCardsDrawn] = card;
    numberOfCardsDrawn++;
    return card;
}

int main () {
    int numberOfCards = 52;
    for (int i = 0; i < numberOfCards; i++){
        cout << "You drew a " << getCard() << endl;
    }
}

答案 2 :(得分:1)

string不是c ++的内置类型,您需要手动包含库才能使用数据类型 string 。您的编译器无法识别关键字字符串,因为您从未包含该库,因此它认为string是变量名称。该图书馆将包括在内:

#include <string>

答案 3 :(得分:1)

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
enum Suit{clubs,diamonds,hearts,spades};
const int jack=11;
const int queen=12;
const int king=13;
const int ace=14;
////////////////////////////////////////////////////////////////////////////////
class card
{
private:
int number;
Suit suit;
public:
card()
{}
void set(int n,Suit s)
{suit = s; number =n;}
void display();
};
//------------------------------------------------------------------------------
void card::display()

{
if(number>=2 && number<=10)
cout<<number;
else
switch(number)
{case jack: cout<<"J";break;
case queen: cout<<"Q";break;
case king: cout<<"K";break;
case ace: cout<<"A";break;
}
switch(suit)
{
case clubs:cout<< static_cast<char>(5);break;
case diamonds:cout<< static_cast<char>(4);break;
case hearts:cout<< static_cast<char>(3);break;
case spades:cout<< static_cast<char>(6);break;
}
}
////////////////////////////////////////////////////////////////////////////////
int main()  
{card deck[52];
int j;
cout<<endl;
for (j=0;j<52;j++)
{
int num=(j%13)+2;
Suit su=Suit(j/13);
deck[j].set(num,su);
}
cout<<"\nOrdered Deck: \n";
for(j=0;j<52;j++)
{
deck[j].display();
cout<<" ";
if(!((j+1)%13))
cout<<endl;
}
srand(time(NULL)); 
for(j=0;j<52;j++)
{
int k=rand()%52;
card temp= deck[j];
deck[j]=deck[k];
deck[k]=temp;
}
cout<<"\nShuffled Deck: \n";
for(j=0;j<52;j++)
{
deck[j].display();
cout<<",";
if(!((j+1)%13))
cout<<endl;
}
getch();
return 0;
}

这个程序将帮助你洗牌 自己动手。