50
Poker
- #include "Deck.h"
- #include "Hand.h"
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <ctype.h>
- #ifdef DEBUG
- int debug = 1; // 0 - off, 1 - on, 2 - more, 3 - even more... 9 - all
- #endif
- static char *getint( char *s, int &i )
- {
- char *p = s, *q;
- char buf[ 1024 ];
- while ( *p && !isdigit( *p ) )
- p++;
- if ( !*p )
- return 0;
- q = buf;
- while ( isdigit( *p ) )
- *q++ = *p++;
- *q = '\0';
- i = atoi( buf );
- return p;
- }
- main( int argc, char **argv )
- {
- using namespace std;
- Deck deck;
- Hand hand;
- int i, n;
- bool keep[ 5 ];
- char input[ 1024 ];
- char *p;
- 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
- }
- deck.shuffle();
- for ( ;; ) // forever
- {
- cout << "Play or (q)uit? ";
- if ( cin.getline( input, sizeof (input) ) == 0 ||
- strlen( input ) > 0 && (input[ 0 ] == 'q' || input[ 0 ] == 'Q' ) )
- break;
- // play one hand
- deck.deal( hand );
- hand.print( true );
- cout << "Keep (a)ll, (n)one or some (1-5...)? ";
- if ( cin.getline( input, sizeof (input) ) == 0 )
- break;
- switch ( input[ 0 ] )
- {
- case 'a':
- for ( i = 0; i < 5; i++ )
- keep[ i ] = true;
- break;
- case 'n':
- case '\0':
- for ( i = 0; i < 5; i++ )
- keep[ i ] = false;
- break;
- default:
- for ( i = 0; i < 5; i++ )
- keep[ i ] = false;
- p = input;
- while( (p = getint( p, i )) )
- if ( i > 0 && i <= 5 )
- keep[ i-1 ] = true;
- break;
- }
- for ( i = 0; i < 5; i++ )
- if ( !keep[ i ] )
- hand[ i ] = deck.deal();
- hand.print( true );
- }
- exit( 0 );
- }
$ make
g++ -DDEBUG -g -c -o poker.o poker.cpp
g++ -DDEBUG -g -c -o Card.o Card.cpp
g++ -DDEBUG -g -c -o Hand.o Hand.cpp
g++ -DDEBUG -g -c -o Deck.o Deck.cpp
ar -cr poker.lib Card.o Hand.o Deck.o
g++ -DDEBUG -g poker.o poker.lib -o poker
$ poker
Play or (q)uit?
<< Tc 6h Kd 2c Qs >>
>> NOTHING <<
Keep (a)ll, (n)one or some (1-5...)?
<< Th 9d 7d Jc 9s >>
>> ONEPAIR <<
Play or (q)uit?
<< 9s 9d 5c 9c Qh >>
>> THREEOFKIND <<
Keep (a)ll, (n)one or some (1-5...)? 1 2 4
<< 9s 9d Td 9c Jh >>
>> THREEOFKIND <<
Play or (q)uit? q
Comments