1

我是 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

4 回答 4

3

你忘了

#include <string>
于 2012-11-05T20:49:47.903 回答
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;
    }
}
于 2012-11-05T21:08:55.567 回答
1

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

#include <string>
于 2012-11-05T20:54:18.467 回答
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;
}

该程序将帮助您洗牌,其余的由您自己完成。

于 2013-08-22T06:42:05.623 回答