简体   繁体   中英

C++ random_shuffle() not behaving properly

I have a Blackjack program that uses a vector full of integers to simulate a card deck:

vector<short int> deck;

and have populated it with 1-10:

for (int i=0; i<=4; ++i) // Populate the deck with 1-10
{
    for (int c=1; c<=10;++c)
    {
        deck.push_back(c);
    }
}
for (i=0; i<=12;++i)
{
    deck.push_back(10); // Face cards
}

then seeded the random number generator:

srand(time(0)+1);

And attempted to shuffle the deck with random_shuffle(deckofcards.begin(), deckofcards.end()); , however, when the user decides to hit, the card they are dealt is the EXACT SAME for the entire game, here is some sample output:

Dealer: I'm gonna play it safe
Dealer bets $42
Dealing cards...
Dealer lays down a 5
You have a 3, and a 10 and your total is 13
Stand, or hit? Stand, or hit? hit
You have been dealt an Ace, your hand is soft, and your total is now 14
Stand, or hit? hit
You have been dealt an Ace, your hand is soft, and your total is now 15
Stand, or hit? hit
You have been dealt an Ace, your hand is soft, and your total is now 16
Stand, or hit? hit
You have been dealt an Ace, your hand is soft, and your total is now 17
Stand, or hit? hit
You have been dealt an Ace, your hand is soft, and your total is now 18
Stand, or hit? hit
You have been dealt an Ace, your hand is soft, and your total is now 19
Dealer calls stand
The dealer has a 3, a 10, an Ace , an Ace , an Ace , an Ace , an Ace , an Ace    for      total of 17, you have a 3, a 10, an Ace , an Ace , an Ace , an Ace , an Ace , an         Ace  you win 1042!

If it helps, here is the code for when the user types hit :

playerhand.push_back(deck.front());
ptotal+=playerhand.back();
if (playerhand.back()!=1)
cout << "You have been dealt a "<<playerhand.back()<<", your total is now"<<ptotal<<endl;
else
    cout << "You have been dealt an Ace, your hand is soft, and your total is now "<<ptotal<<endl;
dealerhand.push_back(deck.front());
dtotal+=dealerhand.back();

However, this code works, but deals two cards:

cout << "Dealing cards...\n";
playerhand.clear();
dealerhand.clear();
playerhand.push_back(deck.back());
deck.pop_back();
dealerhand.push_back(deck.back());
deck.pop_back();
if (dealerhand.back()==1)
    dhsoft=true;
else
    dhsoft=false;
if (playerhand.back()==1)
{
    phsoft=true;
    cout << "Your hand is soft\n";
}
else
    phsoft=false;
playerhand.push_back(deck.back());
deck.pop_back();
dealerhand.push_back(deck.back());
deck.pop_back();
if (dealerhand.back()==1)
    dhsoft=true;
else
    dhsoft=false;
if (playerhand.back()==1)
{
    cout << "Your hand is soft\n";
    phsoft=true;
}
else
    phsoft=false;
unsigned int i;
for (i=0;i<=dealerhand.size()-1; ++i)
    dtotal+=dealerhand[i];
for (i=0;i<=playerhand.size()-1; ++i)
    ptotal+=playerhand[i];

So, why does the above code work, but the one when the user types "hit" not work? And, more importantly, how can I fix it (no code!)?

The reason it's returning the same card over and over again is that deck.front() just returns a reference to the front element, it doesn't remove it. But since vector doesn't have a convenient way of actually removing the front element, I'd recommend just removing the back one instead:

playerhand.push_back(deck.back());
deck.pop_back();

The deck is random anyway, doesn't matter which way you're dealing from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM