0

検証が 1 の場合、新しい乱数を生成するにはどうすればよいですか?

別の「生成数行」を置くことはできますか: cardDrawn=(1+(rand()%52));

#include <iostream>;
#include <cstdlib>;
#include <ctime>;
#include <cmath>;

using namespace std;

int main(){
int deck[52];
bool validate[52]={0};
int hand[5];
int cardDrawn;
int count=0;
int j=0;
bool repeat=0;
string cardNames[52]=   {"Ace of Clubs","2 of Clubs","3 of Clubs","4 of Clubs","5 of Clubs","6 of Clubs","7 of Clubs","8 of Clubs","9 of Clubs","10 of Clubs","Jack of Clubs","Queen of Clubs","King of Clubs",//CLUBS
                        "Ace of Spades","2 of Spades","3 of Spades","4 of Spades","5 of Spades","6 of Spades","7 of Spades","8 of Spades","9 of Spades","10 of Spades","Jack of Spades","Queen of Spades","King of Spades",//SPADES
                        "Ace of Hearts","2 of Hearts","3 of Hearts","4 of Hearts","5 of Hearts","6 of Hearts","7 of Hearts","8 of Hearts","9 of Hearts","10 of Hearts","Jack of Hearts","Queen of Hearts","King of Hearts",//HEARTS
                        "Ace of Diamonds","2 of Diamonds","3 of Diamonds","4 of Diamonds","5 of Diamonds","6 of Diamonds","7 of Diamonds","8 of Diamonds","9 of Diamonds","10 of Diamonds","Jack of Diamonds","Queen of Diamonds","King of Diamonds"//DIAMONDS
                        };//array to hold card abbreviations.

srand(time(0));
for (int i=0; i<52; i++){  // fills arrays with 1-52
deck[i]=i+1;};


cardDrawn=(1+(rand()%52));  //generates first card.
hand[0]=cardDrawn;
validate[cardDrawn]=1;
//cout << "Card 1: " << hand[0];

do{
    cardDrawn=(1+(rand()%52));
    if (validate[cardDrawn]==1)
        {
            //count doesn't update!  :D
        }
    else
        {
            hand[count]=cardDrawn;
            count++;
        }
     }while(count<4);

cout << "Card 1: " << cardNames[hand[0]];
cout << "\ncard 2: " << cardNames[hand[1]];
cout << "\ncard 3: " << cardNames[hand[2]];
cout << "\ncard 4: " << cardNames[hand[3]];
cout << "\ncard 5: " << cardNames[hand[4]];

return 0;
}
4

3 に答える 3

1

実現したい効果は、STL を使用することではるかに簡単になります。最初にドローするのと同じ複雑さで、(同じデッキ内で)任意の数のカードに簡単に拡張できます。

実際、手札に 5 枚のカードを持つ 4 人のプレイヤーを追加しました。1 人のプレーヤーにスケールダウンするのはかなり簡単です。また、すべてのカードに名前を追加するプロセスを簡素化しました。

#include <algorithm> //random_shuffle
#include <cstdlib> //srand
#include <ctime> //time
#include <iostream> //cout
#include <string> //string
#include <vector> //vector

using namespace std;

int main()
{
    srand (time (NULL)); //so random_shuffle gives different values

    const vector<string> values = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    const vector<string> suits = {"Clubs", "Spades", "Hearts", "Diamonds"};

    vector<string> cards; //for names of cards

    for (int suit = 0; suit < 4; ++suit)
    {
        for (int value = 0; value < 13; ++value)
        {
            cards.push_back (values [value] + " of " + suits [suit]); //combine suit and value
        }
    }

    vector<vector<string>> playerHands (4); //4 players' hands

    random_shuffle (cards.begin(), cards.end()); //shuffle cards

    for (auto &hand : playerHands) //for each hand
    {
        for (int card = 0; card < 5; ++card) //for 5 cards
        {
            hand.push_back (cards.back()); //add last card to hand
            cards.pop_back(); //remove last card from deck
        }
    }

    int playerCount = 1; //for player's number
    for (auto hand : playerHands) //for each hand
    {
        cout << "Player " << playerCount << ":\n"; //output player number

        int handCount = 1; //for card's number
        for (auto card : hand) //for each card in hand
        {
            cout << "Card " << handCount << ": " << card << '\n'; //output card
            ++handCount; //increase card number
        }

        ++playerCount; //increase player number
        cout << '\n'; //separate players
    }
}

また、デッキがなくなるたびにカード名のリストを生成するか、デッキごとに のコピーを使用することもできますcards( vector<string> currentDeck (cards);)。

これがまったくコンパイルされない場合は、C++11 と適切なコンパイラ オプションが必要です。それ以外の場合は、for (... : ...)に置き換えることができstd::for_eachauto型に置き換えることができ、ベクトル初期化子リストをベクトルを構築する別の方法に置き換えることができます。

于 2012-04-15T02:18:34.707 に答える
0

2 番目の for ループで反復変数に別の名前を使用すると、おそらくi2、問題は解決します。

しかし、あなたが持っているコードは、スコープ規則のいずれかのセットの下で受け入れられるべきだったと思います。

于 2012-04-14T23:03:46.363 に答える
0
j=0;
do{
    cardDrawn = (1 + (rand() % 52));
    if (validate[cardDrawn] == 0)
    {
        hand[j] = cardDrawn;
        validate[cardDrawn] = 1;
        j++;
    }
}while (j<5);

それはおそらくうまくいくでしょう

ああ、悪い..今は大丈夫だ

于 2012-04-14T23:09:30.130 に答える