Re: Winning Conditions for Poker.
On Oct 20, 10:26 am, Zeppe
<ze...@remove.all.this.long.comment.yahoo.it> wrote:
few advices, not exhaustive, inline.
daniel.ka...@gmail.com wrote:
This is ugly and inefficient. You want 52 cards to be shuffled
and take six of them, so you'd better shuffle numbers from 0
to 51 in a vector and take the first six ones.
That's the simplest and most general solution (since it allows
you to deal additional cards without any extra work, and most
closely simulates the real game.) Another alternative is to
fill the array, then choose one card from the first 52, put it
in the hand and swap it to the end, then choose the next from
the first 51, etc. Something like:
std::vector< int > deck( (boost::counting_iterator( 0 )),
(boost::counting_iterator( 52 )) ) ;
std::vector< int > hand ;
while ( hand.size() != cardsInHand ) {
std::size_t i = rand() % deck.size() ;
hand.push_back( deck[ i ] ) ;
std::swap( deck[ i ], *deck.end() - 1 ) ;
deck.pop_back() ;
}
(but random_shuffle is still simpler:
std::vector< int > deck( (boost::counting_iterator( 0 )),
(boost::counting_iterator( 52 )) ) ;
std::random_shuffle( deck.begin(), deck.end() ) ;
std::vector< int > result( deck.begin(), deck.begin() +
cardsInHand ) ;
Or even "hand.resize( cardsInHand )" and use deck as the hand.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34