Created
February 7, 2013 00:02
-
-
Save anonymous/4727098 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
using std::string; | |
#include <iomanip> | |
using std::left; using std::setw; | |
#include <iostream> | |
using std::cout; using std::cin; | |
using std::endl; | |
#include <cstdlib> | |
#include <ctime> | |
class Card { | |
public: | |
Card(); | |
Card( int s, int v ); | |
~Card(); | |
int getSuit() const; | |
int getValue() const; | |
string getCardName(); | |
private: | |
int suit; // 0 - 3 | |
int value; // 0 - 12 | |
string cardName; | |
}; | |
const int DECKSIZE = 52; | |
class DeckHand { | |
public: | |
DeckHand(); | |
~DeckHand(); | |
void addCard( Card single ); | |
Card *removeCard( int value ); | |
void showDeck(); | |
private: | |
Card Deck[ DECKSIZE ]; | |
int cardsInDeck; | |
}; | |
Card::Card() { } | |
Card::Card( int s, int v ) { | |
if( v >= 0 && v < 13 ) | |
value = v; | |
else { | |
cout << "Tried to create a card with an invalid value." << endl; | |
value = 2; | |
} | |
if( s >= 0 && s < 4 ) | |
suit = s; | |
else { | |
cout << "Tried to create a card with an invalid suite." << endl; | |
suit = 0; | |
} | |
const char *type[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; | |
const char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", | |
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", | |
"King" }; | |
cardName = string( face[ v ] ) + " of " + string( type[ s ] ) + "\0"; | |
} | |
Card::~Card() { } | |
int Card::getSuit() const { | |
return suit; | |
} | |
int Card::getValue() const { | |
return value; | |
} | |
string Card::getCardName() { | |
return cardName; | |
} | |
DeckHand::DeckHand() { | |
Card Deck[ DECKSIZE ]; | |
cardsInDeck = 0; | |
srand( time( 0 ) ); | |
} | |
DeckHand::~DeckHand() { } | |
void DeckHand::addCard( Card single ) { | |
Deck[ cardsInDeck ] = single; | |
cardsInDeck++; | |
} | |
void DeckHand::showDeck( ) { | |
if( cardsInDeck < 1 ) { | |
cout << "No cards in this deck." << endl; | |
} | |
else { | |
cout << "******************" << endl; | |
cout << "Cards in the deck:" << endl; | |
cout << "******************" << endl; | |
cout << "There are " << cardsInDeck << " cards in the deck." << endl; | |
for( int card = 1; card < cardsInDeck; card++ ) { | |
cout << setw( 25 ) << left << Deck[ card - 1 ].getCardName() << ( card % 2 == 0 ? '\n' : '\t' ); | |
} | |
} | |
} | |
Card *DeckHand::removeCard(int value ) { | |
if( cardsInDeck == 0 ) { | |
cout << "Deck is empty." << endl; | |
return NULL; | |
} | |
// Card *found = new Card; | |
for( int card = 0; card < cardsInDeck; card++ ) { | |
if( Deck[ card ].getValue() == value ) { | |
Card *found = new Card( Deck[ card ].getValue(), Deck[ card ].getSuit() ); | |
cardsInDeck--; | |
for( ; card < cardsInDeck; card++ ) { | |
Deck[ card ] = Deck[ card + 1 ]; | |
} | |
return &found; | |
} | |
} | |
return NULL; | |
} | |
int main( int argc, char **argv ) { | |
int dealt = 0; | |
DeckHand deck; | |
DeckHand hand; | |
cout << "Creating the deck now." << endl; | |
for( int s = 0; s < 4; s++ ) { | |
for( int v = 0; v < 13; v++ ) { | |
deck.addCard( Card( s, v ) ); | |
dealt++; | |
} | |
} | |
deck.showDeck(); | |
cout << "Now I want to remove all four suites, one at a time, of one particular" << endl; | |
cout << "card and put them in to my hand(DeckHand class)." << endl; | |
// I'm going to transfer all of the fours to my hand from the deck | |
// Fours are interger value 3 | |
for( int i = 0; i < 4; i++ ) { | |
hand.addCard( deck.removeCard( 3 ) ); | |
} | |
cout << endl; | |
hand.showDeck(); | |
cout << "\n\nProgram finished with no problems." << endl; | |
cout << "\nPress any key to continue..." << endl; | |
cin.get(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment