0

ベクトルを使用して、c++ でカードのデッキを表しています。デッキをシャッフルして最初の要素を削除するのに問題があります。

シャッフルは機能せず、ベクトルは同じ順序を維持します。自分のプログラムで、Deck のコンストラクターで reseed を 1 回呼び出します。int random は毎回異なります。time(NULL) と time(0) を試しました。シャッフリング:

void Deck::reseed() {
srand(time(NULL));
}

void Deck::shuffle() {
int random = rand();
for(int i = 0; i < random; i++) {
    std::random_shuffle(deckVec.begin(), deckVec.end());
}
}

削除は機能せず、最初の要素ではなく最後の要素が削除されます (たとえば、要素 0 ではなく要素 16 が削除されます)。削除:

Card Deck::dealCard() {
//other code...
Card& tempCard = deckVec.front();
std::vector<Card>::iterator temp = deckVec.begin(); //This if for debugging, and 
                                                    //this pointer is correct   
deckVec.erase(deckVec.begin()); 
return tempCard; //the correct card is returned
    }

どんな助けでも大歓迎です。

-----------アップデート---------------- シャッフルの前後に印刷するためのコード:

cout << deck.toString(); //output: Suit: Clubs, Rank: 2
                         //        Suit: Clubs, Rank: 3 ...   
//shuffle if there are three arguments
if(argc == 3)
   deck.shuffle();
cout << deck.toString();//output: Suit: Clubs, Rank: 2
                        //        Suit: Clubs, Rank: 3 ... (identical to above) 

dealCardを表示するのは難しいです。基本的に、私はここでそれを呼び出します:

void operator<<(Hand& hand, Deck& deck) {
    hand.addCard(deck.dealCard());
}

次に、内容を印刷します。出力は次のとおりです。 C2 C2 C2 C2 ... 2 つのクラブの場合。それらはすべてデッキの最初のカードです。

for(int i = 0; i < 5; i++) {
            for(unsigned int j = 0; j < hands.size(); j++) {
                try {
                hands.at(j) << deck;
                } catch(int& i) {
                    cout << "no cards left" << endl;
                }
            }
        }

        //print out the contents
        cout << deck.toString() << endl;
        for(unsigned int i = 0; i < hands.size(); i++) {
            cout << "Hand " << i << ": ";
            cout << hands.at(i).toString() << endl;
        }
4

0 に答える 0