13

Poker

  1. #include "Deck.h"
  2. #include "Hand.h"
  3.  
  4. #include <iostream>
  5.  
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <ctype.h>
  9.  
  10. #ifdef DEBUG
  11. int debug = 1;  // 0 - off, 1 - on, 2 - more, 3 - even more... 9 - all
  12. #endif
  13.  
  14. static char *getint( char *s, int &i )
  15. {
  16.     char *p = s, *q;
  17.     char buf[ 1024 ];
  18.  
  19.     while ( *p && !isdigit( *p ) )
  20.         p++;
  21.     if ( !*p )
  22.         return 0;
  23.     q = buf;
  24.     while ( isdigit( *p ) )
  25.         *q++ = *p++;
  26.     *q = '\0';
  27.     i = atoi( buf );
  28.     return p;
  29. }
  30.  
  31. main( int argc, char **argv )
  32. {
  33.     using namespace std;
  34.  
  35.     Deck deck;
  36.     Hand hand;
  37.  
  38.     int i, n;
  39.     bool keep[ 5 ];
  40.     char input[ 1024 ];
  41.     char *p;
  42.  
  43.     if ( argc > 1 && strncmp( argv[ 1 ], "-D", 2 ) == 0 )
  44.     {
  45.         if ( argc > 2 )
  46.             debug = atoi( argv[ 2 ] );
  47.         else if ( strlen( argv[ 1 ] ) > 2 )
  48.             debug = atoi( argv[ 1 ]+2 );
  49.         else
  50.             debug = 9;  // level max
  51.     }
  52.  
  53.     deck.shuffle();
  54.  
  55.     for ( ;; )  // forever
  56.     {
  57.         cout << "Play or (q)uit? ";
  58.         if ( cin.getline( input, sizeof (input) ) == 0 ||
  59.             strlen( input ) > 0 && (input[ 0 ] == 'q' || input[ 0 ] == 'Q' ) )
  60.             break;
  61.         // play one hand
  62.         deck.deal( hand );
  63.         hand.print( true );
  64.         cout << "Keep (a)ll, (n)one or some (1-5...)? ";
  65.         if ( cin.getline( input, sizeof (input) ) == 0 )
  66.             break;
  67.         switch ( input[ 0 ] )
  68.         {
  69.             case 'a':
  70.                 for ( i = 0; i < 5; i++ )
  71.                     keep[ i ] = true;
  72.                 break;
  73.             case 'n':
  74.             case '\0':
  75.                 for ( i = 0; i < 5; i++ )
  76.                     keep[ i ] = false;
  77.                 break;
  78.             default:
  79.                 for ( i = 0; i < 5; i++ )
  80.                     keep[ i ] = false;
  81.                 p = input;
  82.                 while( (p = getint( p, i )) )
  83.                     if ( i > 0 && i <= 5 )
  84.                         keep[ i-1 ] = true;
  85.                 break;
  86.         }
  87.         for ( i = 0; i < 5; i++ )
  88.             if ( !keep[ i ] )
  89.                 hand[ i ] = deck.deal();
  90.  
  91.         hand.print( true );
  92.     }
  93.     exit( 0 );
  94. }
$ 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

Commentaires

Votre commentaire :
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip aide 2000

Entrez un maximum de 2000 caractères.
Améliorez la présentation de votre texte avec les balises de formatage suivantes :
[p]paragraphe[/p], [b]gras[/b], [i]italique[/i], [u]souligné[/u], [s]barré[/s], [quote]citation[/quote], [pre]tel quel[/pre], [br]à la ligne,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]commande[/code], [code=langage]code source en c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].