557
Deck
- #if !defined( _DECK_H )
- #define _DECK_H
- #include <iostream>
- using namespace std;
- #include "Card.h"
- #include "Hand.h"
- class Deck
- {
- friend ostream &operator<<( ostream &, const Deck & );
- public:
- Deck( bool swe=false) ;
- void shuffle( );
- Card deal();
- void deal( Hand & );
- private:
- int top;
- Card cards[ 52 ];
- bool shuffleWhenEmpty;
- void check( );
- };
- #endif
- #include "Deck.h"
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- Deck::Deck( bool swe ) : shuffleWhenEmpty( swe )
- {
- for ( int s = 0; s < 4; s++ )
- for ( int r = 0; r < 13; r++ ) {
- cards[ 13*s+r ]._rank = rank(r);
- cards[ 13*s+r ]._suit = suit(s);
- }
- top = 0;
- }
- static void swap( Card &c1, Card &c2 )
- {
- Card ct = c1;
- c1 = c2;
- c2 = ct;
- }
- void Deck::shuffle( )
- { // swap all cards at random
- srand( time( 0 ) );
- for ( int i = 0; i < 52; i++ )
- swap( cards[ i ], cards[ rand() % 52 ] );
- top = 0;
- }
- void Deck::check( )
- {
- if ( ++top >= 52 )
- { // reset cards
- if ( shuffleWhenEmpty )
- shuffle();
- top = 0;
- }
- }
- void Deck::deal( Hand &hand )
- {
- for ( int i = 0; i < hand.size(); i++ )
- {
- hand[ i ] = cards[ top ];
- check();
- }
- }
- Card Deck::deal( )
- {
- Card c = cards[ top ];
- check();
- return c;
- }
- ostream &operator<<( ostream &os, const Deck &deck )
- {
- for ( int s = 0; s < 4; s++ ) {
- for ( int r = 0; r < 13; r++ )
- os << deck.cards[ 13*s+r ] << " ";
- os << endl;
- }
- return os;
- }
- #if defined( STANDALONE )
- #include <cstring>
- #include <cstdlib>
- #ifdef DEBUG
- int debug = 1; // 0 - off, 1 - on, 2 - more, 3 - even more... 9 - all
- #endif
- main( int argc, char **argv )
- {
- using namespace std;
- if ( argc > 1 && strncmp( argv[ 1 ], "-D", 2 ) == 0 )
- {
- if ( argc > 2 )
- debug = atoi( argv[ 2 ] );
- else if ( strlen( argv[ 1 ] ) > 2 )
- debug = atoi( argv[ 1 ]+2 );
- else
- debug = 9; // level max
- }
- int i;
- Hand hand;
- Deck deck = Deck( true ); // shuffleWhenEmpty=true
- cout << deck;
- deck.shuffle( );
- cout << endl;
- cout << deck;
- cout << endl;
- for ( i = 0; i < 10; i++ )
- {
- deck.deal( hand );
- hand.print( true );
- cout << endl;
- }
- // statistics
- const int NDEALS = 1500000;
- int stats[ 9 ];
- for ( i = 0; i < 9; i++ )
- stats[ i ] = 0;
- for ( i = 0; i < NDEALS; i++ )
- {
- deck.deal( hand );
- stats[ hand.eval() ]++;
- }
- static const char *headers[] =
- {
- "NOTHING",
- "ONEPAIR",
- "TWOPAIRS",
- "THREEOFKIND",
- "STRAIGHT",
- "FLUSH",
- "FULLHOUSE",
- "FOUROFKIND",
- "STRAIGHTFLUSH",
- };
- // find longest header
- int width = 0;
- for ( i = 0; i < sizeof (headers)/sizeof (char *); i++ )
- {
- int w = strlen( headers[ i ] );
- if ( w > width )
- width = w;
- }
- // print stats
- cout.fill( ' ' );
- cout.precision( 2 );
- cout.setf( ios::fixed, ios::floatfield );
- cout.setf( ios::internal, ios::adjustfield );
- cout << "Combinations found after " << NDEALS << " deals:\n";
- for ( i = 0; i < sizeof (headers)/sizeof (char *); i++ )
- {
- cout.width( width );
- cout << headers[ i ];
- cout << "->" << stats[ i ] << "\t";
- cout.width( 5 );
- cout << stats[ i ]/(NDEALS/100.0) << "%\n";
- }
- }
- #endif
$ make tdeck
g++ -DDEBUG -g -c -o Hand.o Hand.cpp
g++ -DDEBUG -g -c -o Card.o Card.cpp
g++ -DDEBUG -g DSTANDALONE -DDEBUG Deck.cpp Hand.o Card.o -o tdeck
$ tdeck
...
Combinations found after 1500000 deals:
NOTHING->735788 49.05%
ONEPAIR->626043 41.74%
TWOPAIRS->69556 4.64%
THREEOFKIND->35127 2.34%
STRAIGHT->4200 0.28%
FLUSH->2009 0.13%
FULLHOUSE->1004 0.07%
FOUROFKIND->0 0.00%
STRAIGHTFLUSH->26273 1.75%
Almost one hand out of two has nothing and not a single four of a kind!
Comments