Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create an object-oriented program that creates a deck of cards, shuffles them, and deals the specified number of cards to the player. See a sample

Create an object-oriented program that creates a deck of cards, shuffles them, and deals the specified number of cards to the player. See a sample of console screen shown below.

This program would contain multiple files, including two .h and three .cpp files as defined in Specifications below.

If you are using IDE to develop this program, name your project (and solution in VS2017) as Cards to hold all files created. If you prefer to g++ and an editor without an IDE, save all files in a folder or directory also called Cards.

Console

image text in transcribed

Specifications

  • Use a Card class to store the rank and suit for each card. In addition, use a member function, get_str(), to get a string representation for each card such as Ace of Spades, 2 of Spades, etc.
  • Use a Deck class to store the 52 cards in a standard playing deck (one card for each rank and suit):

ranks: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace

suits: Clubs, Diamonds, Hearts, Spades

To store 52 cards, use a member, deck, which is a vector of Card. To save your time in design this class, its default constructor is provided as following:

Deck::Deck() { vector ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" }; vector suits = { "Clubs", "Diamonds", "Hearts", "Spades" }; for (string rank : ranks) { for (string suit : suits) { deck.push_back(Card(rank, suit)); } } }

This class should also include a member function, shuffle(), that shuffles the deck, a member function, count(), that counts the number of cards in the deck, and a member function, deal_card(), that deals a card from the deck, which should reduce the count of the cards in the deck by 1.

Hint 1: See Chapter 6 for how to use vector class, which is very similar to an array (a built-in construct) with no pre-allocated size and can grow and shrink dynamically.

Hint 2: To shuffle the cards, you can use rand() and srand() functions. Because some cards may be already dealt to the user, be sure the random number generated is based on the deck's current size.

  • Store the Card and Deck classes in separate header and implementation files. That is, there should be Card.h, Card.cpp, Deck.h, and Deck.cpp in your Cards (project) folder.
  • When the program (i.e., main.cpp) starts, it should get a new deck of cards, shuffle them, and display a message that indicates the total number of cards in the deck.
  • The program should prompt the user for the desired number of cards. Then, it should deal the user the desired number of cards and display a message that indicates the number of cards left in the deck as shown in the above Console screen.
Card Dealer I have shuffled a deck of 52 cards How many cards would you like?: 7 Here are your cards: Jack of Hearts Jack of Diamonds 2 of Diamonds 6 of Spades Jack of Spades 6 of Hearts King of Diamonds There are 45 cards left in the deck Good luck

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genomes Browsers And Databases Data Mining Tools For Integrated Genomic Databases

Authors: Peter Schattner

1st Edition

0521711320, 978-0521711326

More Books

Students also viewed these Databases questions

Question

True or false. Most amino acids are zwitterions at physiological pH

Answered: 1 week ago

Question

7-16 Compare Web 2.0 and Web 3.0.

Answered: 1 week ago