0

我正在为我的 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;
            }
}
4

1 回答 1

2

第 85 行:

Deck.iCardIndex = 0;

这是在循环内。每次你想抽一张牌时,你都会重置索引并从牌组的顶部重新开始。

更一般地说,你应该从简单到复杂,在每一步都进行测试。准备好后,您Deck应该先尝试发几张牌,然后再放入交互式选择和其他内容。那么这个错误会更容易被发现。

于 2012-10-08T03:25:38.863 回答