为C 模拟和洗牌
Simulating and shuffling a deck of cards for C++?
本文关键字:模拟 更新时间:2023-10-16
我是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
我不确定我在做什么错。
我也不知道如何制作程序,所以它只会绘制一张卡一次而不是无限地绘制。
有什么建议?
你忘了
#include <string>
正如其他答案所说的那样,您需要通过在文件顶部添加" #include"来包含"字符串"库。您也忘了在您的循环中声明"我"。您的faceValue阵列缺少" Queen",一旦添加" Queen",您还需要更改生成CardValue的方式,以便它是Rand()%13。另外,为了确保您不处理两次相同的卡绘制(您可能需要多次绘制以获取未绘制卡片)。
这是进行检查的快速简单方法(我已经修改并更正了您的代码以显示它,但请记住,这不是这样做的最好方法):
#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;
}
}
字符串不是C 的内置类型,您需要手动包含该库才能使用数据类型 string 。您的编译器无法识别关键字字符串,因为您从未包含库,因此它认为字符串是变量名称。图书馆将包括:
#include <string>
#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;
}
此程序将帮助您洗牌剩下的。
相关文章:
- 如何使用Google Mock来模拟gettimeofday()
- G锁定铸造到基础上会释放模拟行为
- 有什么好的方法可以让系统调用代理允许在单元测试中进行模拟
- 落砂模拟碰撞检测C++和SFML
- 在gtest.中使用fff.h模拟系统API
- 谷歌模拟和覆盖关键字
- 用C#中的并集模拟C++嵌套结构
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 在模拟器中使用并集来模拟CPU寄存器有多合适
- 我写了一个C++程序来模拟Enigma机器.我没有得到输出
- 如何模拟不同边数的骰子滚动?
- 模拟持久按键
- 使用SIR模型的疾病爆发模拟
- 在 c++ 中模拟输入并在 JAVA 中读取它?
- 转发变量参数列表以模拟 std::thread
- 如何在谷歌模拟中匹配 C 样式数组
- 如何使用兰德随机化模拟点击
- 模拟GPS数据,以便使用Qt与Traccar一起使用
- QKeyPress - 在Qt中模拟按键
- 如何使用不同的谷歌模拟运行相同的谷歌测试用例?