(Card Shuffling and Dealing) Create a program to shuffle and deal a deck of cards. The pro- gram should consist of class Card, class DeckOfCards and a driver program. Class Card should provide:
a) Data members face and suit of type int.
b) A constructor that receives two ints representing the face and suit and uses them to initialize the data members.
c) Two static arrays of strings representing the faces and suits.
d) A toString function that returns the Card as a string in the form “face of suit.” You can use the + operator to concatenate strings.
Class DeckOfCards should contain:
a) A vector of Cards named deck to store the Cards.
b) An integer currentCard representing the next card to deal.
c) A default constructor that initializes the Cards in the deck. The constructor should use vector function push_back to add each Card to the end of the vector after the Card is created and initialized. This should be done for each of the 52 Cards in the deck.
d) A shuffle function that shuffles the Cards in the deck. The shuffle algorithm should iterate through the vector of Cards. For each Card, randomly select another Card in the deck and swap the two Cards.
e) A dealCard function that returns the next Card object from the deck.
f) A moreCards function that returns a bool value indicating whether there are more Cards to deal.
The driver program should create a DeckOfCards object, shuffle the cards, then deal the 52 cards.
```
// Class Card represents the face and suit of a card.
#ifndef CARD_H
#define CARD_H
#include
using namespace std;
class Card
{
public:
static const int totalFaces = 13; // total number of faces
static const int totalSuits = 4; // total number of suits
Card( int cardFace, int cardSuit ); // initialize face and suit
string toString() const; // returns a string representation of a Card
// get the card's face
int getFace() const
{
return face;
} // end function getFace
// get the card's suit
int getSuit() const
{
return suit;
} // end function getSuit
private:
int face;
int suit;
static const string faceNames[ totalFaces ];
static const string suitNames[ totalSuits ];
}; // end class Card
#endif
```
```
// Member-function definitions and array initializers for the Card class.
#include "Card.h"
// Card constructor initializes face and suit
Card::Card( int cardFace, int cardSuit )
{
face = ( cardFace >= 0 && cardFace < totalFaces ) ? cardFace : 0;
suit = ( cardSuit >= 0 && cardSuit < totalSuits ) ? cardSuit : 0;
} // end Card constructor
// returns a string representation of a Card
string Card::toString() const
{
// strings can be concatenated with +
return faceNames[ face ] + " of " + suitNames[ suit ];
} // end function toString
// contents of arrays to convert index into name
const string Card::faceNames[ totalFaces ] =
{ "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
const string Card::suitNames[ totalSuits ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
```
```
// Class DeckOfCards represents a deck of 52 playing cards.
#ifndef DECK_OF_CARDS_H
#define DECK_OF_CARDS_H
#include
#include "Card.h"
using namespace std;
// DeckOfCards class definition
class DeckOfCards
{
public:
DeckOfCards(); // constructor initializes deck
void shuffle(); // shuffles cards in deck
Card dealCard(); // deals cards in deck
bool moreCards() const; // are there any more cards left
private:
vector< Card > deck; // represents deck of cards
unsigned currentCard; // index of next card to be dealt
}; // end class DeckOfCards
#endif
```
```
// Member-function definitions for class DeckOfCards that simulates
// the shuffling and dealing of a deck of playing cards.
#include
#include
#include "DeckOfCards.h" // DeckOfCards class definition
using namespace std;
// DeckOfCards default constructor initializes deck
DeckOfCards::DeckOfCards()
{
currentCard = 0; // set currentCard so first Card dealt is deck[ 0 ]
// populate deck with Card objects
for ( int i = 0; i < Card::totalFaces * Card::totalSuits; ++i )
{
Card card( i % Card::totalFaces, i / Card::totalFaces );
deck.push_back( card ); // adds copy of card to the end of the deck
} // end for
srand( time( 0 ) ); // seed random number generator
} // end DeckOfCards default constructor
// shuffle cards in deck
void DeckOfCards::shuffle()
{
// after shuffling, dealing should start at deck[ 0 ] again
currentCard = 0; // reinitialize currentCard
// for each Card, pick another random Card and swap them
for ( unsigned first = 0; first < deck.size(); ++first )
{
// select a random number between 0 and 51
unsigned second = rand() % deck.size();
// swap current Card with randomly selected Card
Card temp = deck[ first ];
deck[ first ] = deck[ second ];
deck[ second ] = temp;
} // end for
} // end function shuffle
// deal cards in deck
Card DeckOfCards::dealCard()
{
return deck[ currentCard++ ]; // return current Card in vector
} // end function dealCard
// Check if there are more cards in the deck
bool DeckOfCards::moreCards() const
{
return currentCard < deck.size();
} // end function moreCards
```
```
// Card shuffling and dealing program.
#include
#include
#include "DeckOfCards.h" // DeckOfCards class definition
using namespace std;
int main()
{
DeckOfCards myDeckOfCards;
myDeckOfCards.shuffle(); // place Cards in random order
// print all 52 Cards in the order in which they are dealt
for ( int i = 1; myDeckOfCards.moreCards(); i++ )
{
// deal and display a Card
cout << left << setw( 19 ) << myDeckOfCards.dealCard().toString();
if ( i % 4 == 0 ) // output newline every 4 cards
cout << endl;
} // end for
} // end main
```
You might also like to view...
Use the property to bind data to information in another XAML element, and the property to bind to a specific property of that element.
a) Binding, Source b) Target, Property c) ElementName, Path d) Target, Source
What does the T represent in the following statement?
``` template < class T > ``` a. the name of the function template b. T stands for Template c. a generic data type that is used in a function template d. the int data type e. None of these
Which of the following statements about kernel-level threads is false?
a) The kernel can manage each of a process’s threads individually. b) A process that uses kernel-level threads often can adjust the level of service each thread receives from the operating system. c) Software that employs kernel-level threads often is more portable than software that employs user-level threads. d) Kernel-level threads require the operating system to manage all threads in the system.
The ________ function is not used in Access to prevent errors in calculated outputs as used in Excel
Fill in the blank(s) with correct word