Here is the full code for my idea:
http://download.cdsoft.co.uk/code/carddeck.zip
This is the crux of it; the shuffling method:
[syntax="c++"]
/**
* Randomly shuffles the array of cards.
*/
void CardDeck:
huffle()
{
std::vector<int> couples;
int tempDeck[52];
// can't shuffle 0 or 1 cards.
if(m_availableCards.size() < 2)
return;
// copy the current deck.
for(int i = 0; i < m_availableCards.size(); i++)
tempDeck[i] = m_availableCards[i];
// seed the PNR generator.
srand(time(0));
// separate half of the deck into a different list.
for(int i = 1; i < m_availableCards.size(); i += 2)
couples.push_back(i);
// now swap elements randomly.
for(int i = 0; i < m_availableCards.size(); i += 2)
{
int swapIndex = rand() % couples.size();
int deckIndex = couples[swapIndex];
// find an iterator to this item and remove it.
std::vector<int>::iterator item = std::find(couples.begin(),
couples.end(),
deckIndex);
if(item != couples.end())
couples.erase(item);
int temp = tempDeck[i];
tempDeck[i] = tempDeck[deckIndex];
tempDeck[deckIndex] = temp;
}
// now reconstruct the deck by randomly picking one from the top
// and one from the bottom.
int top = 0;
int bottom = m_availableCards.size() - 1;
// clear the existing available cards.
m_availableCards.clear();
while(top <= bottom)
{
if((rand() % 2) == 1)
m_availableCards.push_back(tempDeck[top++]);
else
m_availableCards.push_back(tempDeck[bottom--]);
}
}
[/syntax]