Question
Write a program that implements a deck of cards. Assume a normal 52-card deck that consists of the 4 suits (Hearts, Diamonds, Clubs, Spades) and
Write a program that implements a deck of cards. Assume a normal 52-card deck that consists of the 4 suits (Hearts, Diamonds, Clubs, Spades) and 13 values (2 10, J, Q, K, A). You can utilize the Card class developed in the classroom
Deliverables:
1. Updated Card class (Card.h)
2. Deck class that supports shuffling and dealing cards (Deck.h).
3. A driver program that creates a deck of cards, shuffles the deck, and deals out a 5 card hand, which is printed to the screen. (cardTest.cpp)
Here is what I have so far. I just need to add shuffling and dealing 5 cards.
----------------------------------------------------
card.cpp:
#include
int main() { //test suit and value printing //Suit mySuit = Hearts; //Value myVal = TWO; //cout << myVal << " of " << mySuit << endl; //create a Card //Card myCard(Spades, ACE); //Card yourCard(Hearts, THREE); //myCard = yourCard; //cout << myCard << endl // << yourCard << endl; DeckOfCards newDeck; //lets print some cards to see if the deck is working for(int counter = 0; counter < NUMCARDS; counter++) if (newDeck.getCardsRemaining() > 0) cout << newDeck.dealCard() << endl; //what happens below? Card temp; if (newDeck.getCardsRemaining() > 0) temp = newDeck.dealCard(); else cout << "Deck must be empty!" << endl; //cout << temp << endl; //add code here to shuffle the deck, and deal 5 cards
//add code here to print the "hand" (the 5 cards)
return 0; }
----------------------------------------------------
Deck.h:
#ifndef DECK_H #define DECK_H //Deck class #include "Card.h" #define NUMCARDS 52 class DeckOfCards { private: Card thedeck[NUMCARDS]; int topCard; int cardsRemaining; public: int getCardsRemaining(); void setCardsRemaining(int); DeckOfCards();//default constructor void shuffle();//need to implement this Card dealCard();//need to implement this };//end class int DeckOfCards::getCardsRemaining() { return cardsRemaining; } void DeckOfCards::setCardsRemaining(int c) { //validate the input if (c >= 0 && c <= NUMCARDS) cardsRemaining = c; else cardsRemaining = 0;//default value } DeckOfCards::DeckOfCards() { topCard = 0; cardsRemaining = NUMCARDS; for(int s = Hearts; s <= Spades; s++) { //handle each suit for(int v = TWO; v <= ACE; v++) { //handle each value thedeck[topCard].setCardSuit((Suit)s);//set up the cards correctly thedeck[topCard].setCardValue((Value)v); topCard++; }//end for }//end for //reset topCard to 0 topCard = 0; }//end constructor void DeckOfCards::shuffle() { //implement code to randomly swap cards } Card DeckOfCards::dealCard() { cardsRemaining--; return thedeck[topCard++];//this updates the topCard, //after selecting the card for dealing } #endif
----------------------------------------------------
Card.h:
#ifndef CARD_H #define CARD_H //A card Class enum Suit {Hearts, Diamonds, Clubs, Spades}; enum Value {TWO=2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE}; class Card { private: Suit cardSuit; Value cardValue; public: Card(Suit, Value); Card(); friend ostream& operator<<(ostream&, const Card&); void setCardSuit(Suit); void setCardValue(Value); Suit getCardSuit(); Value getCardValue(); }; Suit Card::getCardSuit() { return cardSuit; } Value Card::getCardValue() { return cardValue; } void Card::setCardSuit(Suit s) { cardSuit = s; } void Card::setCardValue(Value v) { cardValue = v; } Card::Card() { /*empty*/ } //code for the constructor Card::Card(Suit s, Value v){ cout << "Creating card." <
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started