RobotJones
Apr 29, 2007, 10:48 AM
I can't seem to get 'random_shuffle' from <algorithm> to work correctly. I have defined a class 'aCard' which has data members 'aRank' and 'aSuit'. Then define a vector of aCards called 'deck'. I initialize the deck with 'initDeck()' and try to shuffle the deck with 'shuffleDeck()'. Just for debugging purposes I have 'showDeck()' to display the current status of 'deck'. (Of course I overloaded << to display a card.) When I call showDeck() after shuffleDeck() it displays the deck with the cards in order as if shuffleDeck() never happened. Here's my code:
-------------------------------------------------------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <vector>
#include "aCard.h" // card module defining aCard, aRank, aSuit,.
vector<aCard> deck;
void initDeck()
{
aCard card;
deck.clear();
for(aSuit suit=HEARTS; suit<=SPADES; ++suit)
{
for(aRank rank=TWO; rank<=ACE; ++rank)
{
card.setCard(rank,suit);
deck.push_back(card);
}
}
}
void shuffleDeck()
{
random_shuffle(deck.begin(), deck.end());
}
void showDeck()
{
for(vector<aCard>::const_iterator iter=deck.begin(); iter!=deck.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
}
int main()
{
initDeck();
cout << " after initDeck():\n\n";
showDeck();
shuffleDeck();
cout << "\n\n after shuffleDeck():\n\n";
showDeck();
return 0;
}
-------------------------------------------------------------------------------------------------
My output looks like this:
-------------------------------------------------------------------------------------------------
after initDeck():
2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
after shuffleDeck():
2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
-------------------------------------------------------------------------------------------------
Why doesn't random_shuffle() work? I was thinking that maybe random_shuffle() DOES work, but it's my showDeck() that has the problem. Either way, can anyone help? Thanks in advance.
-------------------------------------------------------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <vector>
#include "aCard.h" // card module defining aCard, aRank, aSuit,.
vector<aCard> deck;
void initDeck()
{
aCard card;
deck.clear();
for(aSuit suit=HEARTS; suit<=SPADES; ++suit)
{
for(aRank rank=TWO; rank<=ACE; ++rank)
{
card.setCard(rank,suit);
deck.push_back(card);
}
}
}
void shuffleDeck()
{
random_shuffle(deck.begin(), deck.end());
}
void showDeck()
{
for(vector<aCard>::const_iterator iter=deck.begin(); iter!=deck.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
}
int main()
{
initDeck();
cout << " after initDeck():\n\n";
showDeck();
shuffleDeck();
cout << "\n\n after shuffleDeck():\n\n";
showDeck();
return 0;
}
-------------------------------------------------------------------------------------------------
My output looks like this:
-------------------------------------------------------------------------------------------------
after initDeck():
2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
after shuffleDeck():
2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH AH
2D 3D 4D 5D 6D 7D 8D 9D 10D JD QD KD AD
2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC AC
2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS AS
-------------------------------------------------------------------------------------------------
Why doesn't random_shuffle() work? I was thinking that maybe random_shuffle() DOES work, but it's my showDeck() that has the problem. Either way, can anyone help? Thanks in advance.