gpt4 book ai didi

c++ - 纸牌游戏中的结构 vector 之后不显示纸牌,不确定是否甚至创建了牌组

转载 作者:行者123 更新时间:2023-11-28 01:21:38 25 4
gpt4 key购买 nike

我正在用 C++ 制作一个基本的纸牌游戏,我知道我的纸牌正在创建,我只是不知道它们是否被放入 vector 中。

我已经尝试使用函数来传回信息,我已经在不洗牌的情况下对其进行了测试,看它是否至少检测到了正确的猜测。

struct card
{
string cardFace, cardSuit;
};


int main()
{

vector<card> deck;
card currentCard;
string input;

for (int i = 0; i < 4; i++)
{
string suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };

for (int j = 1; j < 13; j++)//13 cards per suit
{
card a;

if (j == 1)
a.cardFace = "Ace";

if (j == 11)
a.cardFace = "Jack";

if (j == 12)
a.cardFace = "Queen";

if (j == 13)
a.cardFace = "King";

else
a.cardFace = to_string(j);

a.cardSuit = suits[i];
deck.push_back(a);
}
}

random_shuffle(deck.begin(), deck.end());

currentCard.cardFace = deck[0].cardFace;
currentCard.cardSuit = deck[0].cardSuit;

if(input == currentCard.cardFace)
cout << currentCard.cardFace << " is the right answer!" << endl;

return 0;
}

我希望输出是:Jack is the right answer!但我得到:是正确答案!

最佳答案

我在 IdeOne 上试过你程序的变体:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct card
{
string cardFace, cardSuit;
};


int main()
{
vector<card> deck;
card currentCard;

for (int i = 0; i < 4; i++)
{
string suits[4] = { "Hearts", "Diamonds", "Spades", "Clubs" };

for (int j = 1; j < 13; j++)//13 cards per suit
{
card a;

if (j == 1)
a.cardFace = "Ace";

if (j == 11)
a.cardFace = "Jack";

if (j == 12)
a.cardFace = "Queen";

if (j == 13)
a.cardFace = "King";

else
a.cardFace = to_string(j);

a.cardSuit = suits[i];
deck.push_back(a);
}
}

random_shuffle(deck.begin(), deck.end());

currentCard.cardFace = deck[0].cardFace;
currentCard.cardSuit = deck[0].cardSuit;

cout << currentCard.cardFace << " " << currentCard.cardSuit << " is the right answer!" << endl;

return 0;
}

它按预期工作。

关于c++ - 纸牌游戏中的结构 vector 之后不显示纸牌,不确定是否甚至创建了牌组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55957628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com