同一张卡被抽出C++的麻烦
Trouble with Same Card Being Drawn C++
我正在为我的C++类创建一个黑桃游戏。我已经解决了大部分问题。但是,当我从一副牌中抽出一张牌时,它在整个程序运行过程中都是同一张牌。我不知道如何将第二张卡等改回随机卡。任何帮助将不胜感激。另外,我知道大多数代码可能没有正确编写,并且有更简单的方法来编写某些区域,但我必须遵循某些标准。
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
enum eSuit {H, C, D, S};
struct SSingleCard
{
int iRank;
eSuit Suits;
};
struct SDeck
{
SSingleCard aiDeck[52];
int iCardIndex;
};
struct SPlayerHand
{
SSingleCard aiHand[13];
int iTotalHand;
};
struct SCardDisplay
{
char cSuit;
char cRank;
};
void showCard(SSingleCard);
int main(int argc, char* argv[])
{
cout << "Welcome! This is a 2-Player Version of Spades! You vs. The Computer!";
cout << endl;
cout << "You draw first!";
cout << endl;
char cKeepCard;
SDeck Deck;
Deck.iCardIndex = 0;
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 13; j++)
{
Deck.aiDeck[Deck.iCardIndex].iRank = j + 2;
Deck.aiDeck[Deck.iCardIndex].Suits = eSuit(i);
Deck.iCardIndex++;
}
}
srand( time( NULL ) );
for( int i = 0; i < 52; i++ )
{
int iRandomNumber = ( rand() % 51) + 1;
SSingleCard card = Deck.aiDeck[i];
Deck.aiDeck[i] = Deck.aiDeck[iRandomNumber];
Deck.aiDeck[iRandomNumber] = card;
}
do
{
SPlayerHand sPlayersHand;
SPlayerHand sComputersHand;
Deck.iCardIndex = 0;
cout << "You drew ";
showCard(Deck.aiDeck[Deck.iCardIndex]);
cout << "Keep this card? (Y/N)";
string sDecision;
cin >> sDecision;
if(sDecision == "N" || sDecision == "n")
{
sPlayersHand.aiHand[0,1,2,3,4,5,6,7,8,9,10,11,12] = Deck.aiDeck[Deck.iCardIndex];;
cout << "You discarded ";
showCard(Deck.aiDeck[Deck.iCardIndex]);
Deck.iCardIndex++;
} else if (sDecision == "Y" || sDecision == "y")
{
sPlayersHand.aiHand[0,1,2,3,4,5,6,7,8,9,10,11,12] = Deck.aiDeck[Deck.iCardIndex];
showCard(Deck.aiDeck[Deck.iCardIndex]);
Deck.iCardIndex++;
showCard(Deck.aiDeck[14]);
Deck.iCardIndex++;
sComputersHand.aiHand[0] = Deck.aiDeck[Deck.iCardIndex];
Deck.iCardIndex++;
} else {
cout << "That input is invalid. Try again." << endl;
}
}while (Deck.iCardIndex < 52);
std::cin.sync();
std::cin.get();
return 0;
}
void showCard(SSingleCard card)
{
if( card.iRank >= 2 && card.iRank <=9 )
cout << card.iRank;
switch( card.iRank )
{
case 10:
cout << "T";
break;
case 11:
cout << "J";
break;
case 12:
cout << "Q";
break;
case 13:
cout << "K";
break;
case 14:
cout << "A";
break;
}
if( card.Suits >= 0 && card.Suits <= 4 )
switch( card.Suits )
{
case 0:
cout << 'C' << endl;
break;
case 1:
cout << 'D' << endl;
break;
case 2:
cout << 'S' << endl;
break;
case 3:
cout << 'H' << endl;
break;
}
}
第 85 行:
Deck.iCardIndex = 0;
这是在循环内。每次你想抽一张牌时,你都会重置索引,然后从牌组的顶部重新开始。
更一般地说,您应该从简单到复杂,在每一步进行测试。当你准备好Deck
时,你应该尝试发几张牌,然后再投入互动选择和其他东西。那么这个错误会更容易发现。
相关文章:
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 查找奇数平方和时遇到麻烦
- 将双倍转换为 LPARAM 的麻烦
- 麻烦得到提升::记录器编译
- 翻转 ppm 图像C++麻烦
- 尝试使用 indexOf 创建一个 if 语句来检查字符串是否包含字符.有一点麻烦
- 搬家turtlebot_gazebo麻烦
- CSUF EPP - 你有什么想法(在添加到链接列表末尾时遇到麻烦)
- 我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价
- 作为一个C++新手,我遇到了麻烦
- 我需要帮助创建一个评分系统,但它一直给我一个错误,注释掉的整数是给我带来麻烦的部分
- 找麻木的麻烦
- MySQL 从注入过渡到参数化时遇到麻烦
- 字符串和双精度的麻烦,等值后再得到一个数字
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 自定义QVariant麻烦
- 串和阵列的麻烦
- 在C 向量中修改对象值的麻烦
- C DirectX 11从第二个顶点缓冲区呈现麻烦
- 编译 opencv 示例麻烦