27

Card

  1. #if !defined( _CARD_H )
  2. #define _CARD_H
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. #include "debug.h"
  9.  
  10. enum rank
  11. {
  12.     TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
  13.     NINE, TEN, JACK, QUEEN, KING, ACE
  14. };
  15.  
  16. enum suit
  17. {
  18.     CLUBS, DIAMONDS, HEARTS, SPADES
  19. };
  20.  
  21. class Card
  22. {
  23.     friend class Hand;
  24.     friend class Deck;
  25.     friend ostream &operator<<( ostream &, const Card & );
  26. public:
  27.     ~Card( )
  28.     {
  29.         ONDEBUG9( cout << "\t~Card()\n"; );
  30.     }
  31.     Card()
  32.     {
  33.         ONDEBUG8( cout << "\tCard( )\n"; );
  34.     }
  35.     Card( rank rank, suit suit ) : _rank( rank ), _suit( suit )
  36.     {
  37.         ONDEBUG8( cout << "\tCard( rank, suit )\n"; );
  38.     }
  39.     Card( const Card &c ) : _rank( c._rank), _suit( c._suit )
  40.     {
  41.         ONDEBUG8( cout << "\tCard( const Card & )\n"; );
  42.     }
  43.  
  44.     void operator=( const Card &c )
  45.     {
  46.         ONDEBUG7( cout << "Card::operator=( const Card & )\n"; );
  47.         _rank = c._rank;
  48.         _suit = c._suit;
  49.     }
  50.  
  51. private:
  52.     rank _rank;
  53.     suit _suit;
  54. };
  55.  
  56. #endif
  1. #include "Card.h"
  2.  
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. ostream &operator<<( ostream &os, const Card &card )
  8. {
  9.     switch( card._rank )
  10.     {
  11.         case TWO:       os << "2";  break;
  12.         case THREE:     os << "3";  break;
  13.         case FOUR:      os << "4";  break;
  14.         case FIVE:      os << "5";  break;
  15.         case SIX:       os << "6";  break;
  16.         case SEVEN:     os << "7";  break;
  17.         case EIGHT:     os << "8";  break;
  18.         case NINE:      os << "9";  break;
  19.         case TEN:       os << "T";  break;
  20.         case JACK:      os << "J";  break;
  21.         case QUEEN:     os << "Q";  break;
  22.         case KING:      os << "K";  break;
  23.         case ACE:       os << "A";  break;
  24.     }
  25.     switch( card._suit )
  26.     {
  27.         case CLUBS:     os << "c";  break;
  28.         case DIAMONDS:  os << "d";  break;
  29.         case HEARTS:    os << "h";  break;
  30.         case SPADES:    os << "s";  break;
  31.     }
  32.     return os;
  33. }
  34.  
  35. #if defined( STANDALONE )
  36.  
  37. #include <cstring>
  38. #include <cstdlib>
  39.  
  40. #ifdef DEBUG
  41. int debug = 1;  // 0 - off, 1 - on, 2 - more, 3 - even more, etc.
  42. #endif
  43.  
  44. main( int argc, char **argv )
  45. {
  46.     using namespace std;
  47.  
  48.     if ( argc > 1 && strncmp( argv[ 1 ], "-D", 2 ) == 0 )
  49.     {
  50.         if ( argc > 2 )
  51.             debug = atoi( argv[ 2 ] );
  52.         else if ( strlen( argv[ 1 ] ) > 2 )
  53.             debug = atoi( argv[ 1 ]+2 );
  54.         else
  55.             debug = 9;  // level max
  56.     }
  57.  
  58.     Card card2H = Card( TWO, HEARTS );
  59.     cout << "2H (two of hearts) -> " << card2H << endl;
  60.     Card cardKS = Card( KING, SPADES );
  61.     cout << "KS (king of spades) -> " << cardKS << endl;
  62. }
  63.  
  64. #endif
$ make tcard
g++ -DDEBUG -g -DSTANDALONE -DDEBUG Card.cpp -o tcard
$ tcard
2H (two of hearts) -> 2h
KS (king of spades) -> Ks

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[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]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].