27
Card
- #if !defined( _CARD_H )
- #define _CARD_H
- #include <iostream>
- using namespace std;
- #include "debug.h"
- enum rank
- {
- TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
- NINE, TEN, JACK, QUEEN, KING, ACE
- };
- enum suit
- {
- CLUBS, DIAMONDS, HEARTS, SPADES
- };
- class Card
- {
- friend class Hand;
- friend class Deck;
- friend ostream &operator<<( ostream &, const Card & );
- public:
- ~Card( )
- {
- ONDEBUG9( cout << "\t~Card()\n"; );
- }
- Card()
- {
- ONDEBUG8( cout << "\tCard( )\n"; );
- }
- Card( rank rank, suit suit ) : _rank( rank ), _suit( suit )
- {
- ONDEBUG8( cout << "\tCard( rank, suit )\n"; );
- }
- Card( const Card &c ) : _rank( c._rank), _suit( c._suit )
- {
- ONDEBUG8( cout << "\tCard( const Card & )\n"; );
- }
- void operator=( const Card &c )
- {
- ONDEBUG7( cout << "Card::operator=( const Card & )\n"; );
- _rank = c._rank;
- _suit = c._suit;
- }
- private:
- rank _rank;
- suit _suit;
- };
- #endif
- #include "Card.h"
- #include <iostream>
- using namespace std;
- ostream &operator<<( ostream &os, const Card &card )
- {
- switch( card._rank )
- {
- case TWO: os << "2"; break;
- case THREE: os << "3"; break;
- case FOUR: os << "4"; break;
- case FIVE: os << "5"; break;
- case SIX: os << "6"; break;
- case SEVEN: os << "7"; break;
- case EIGHT: os << "8"; break;
- case NINE: os << "9"; break;
- case TEN: os << "T"; break;
- case JACK: os << "J"; break;
- case QUEEN: os << "Q"; break;
- case KING: os << "K"; break;
- case ACE: os << "A"; break;
- }
- switch( card._suit )
- {
- case CLUBS: os << "c"; break;
- case DIAMONDS: os << "d"; break;
- case HEARTS: os << "h"; break;
- case SPADES: os << "s"; break;
- }
- return os;
- }
- #if defined( STANDALONE )
- #include <cstring>
- #include <cstdlib>
- #ifdef DEBUG
- int debug = 1; // 0 - off, 1 - on, 2 - more, 3 - even more, etc.
- #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
- }
- Card card2H = Card( TWO, HEARTS );
- cout << "2H (two of hearts) -> " << card2H << endl;
- Card cardKS = Card( KING, SPADES );
- cout << "KS (king of spades) -> " << cardKS << endl;
- }
- #endif
$ make tcard
g++ -DDEBUG -g -DSTANDALONE -DDEBUG Card.cpp -o tcard
$ tcard
2H (two of hearts) -> 2h
KS (king of spades) -> Ks
Comments