Question

In: Computer Science

Card Shuffling and Dealing (C++) All in one file with comments explaining please! Create a program...

Card Shuffling and Dealing (C++)

All in one file with comments explaining please!

Create a program to shuffle and deal a deck of cards. The program should consist of a 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)      An array 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.
d)      A shuffle function that shuffles the Cards in the deck. The shuffle algorithm should iterate through the array 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, and then deal the 52 cards.

Solutions

Expert Solution

Code in C++

/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <bits/stdc++.h>
using namespace std;
class Card{
        public:
        static string suit_str[];
        static string face_str [];
        int face;
        int suit;
        Card(){}
        Card(int face, int suit):face(face), suit(suit){};
        string toString(){
                return face_str[face] +" of " + suit_str[suit];
        }
};
string Card::suit_str[]  = {"Hearts", "Diamonds", "Clubs", "Spades"};
string Card::face_str[] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

class DeckOfCards{
        public:
        Card *deck; 
        int currentCard;
        DeckOfCards(){
                deck = new Card[52];
                int k=0;
                for(int i=0;i<4;i++){
                        for(int j=0;j<13;j++){
                                Card c(j,i);
                                deck[k++]=c;
                        }
                }
                currentCard = 0;
        }
        void shuffle(){
                for(int i=0;i<52;i++){
                        int j=rand()%52;
                        swap(deck[i], deck[j]);
                }
        }
        Card dealCard(){
                return deck[currentCard++];
        }
        bool moreCards(){
                return currentCard<52;
        }
};
int main(){
        DeckOfCards deck;
        deck.shuffle();
        while(deck.moreCards()){
                Card next_card = deck.dealCard();
                cout<<next_card.toString()<<endl;
        }
}

Sample Console Output

Three of Spades
Nine of Hearts
Ten of Diamonds
Ten of Hearts
Five of Diamonds
Eight of Diamonds
King of Diamonds
Five of Clubs
Ace of Clubs
Five of Hearts
Seven of Diamonds
Ace of Diamonds
Jack of Hearts
Six of Clubs
King of Hearts
Queen of Spades
Queen of Hearts
Ten of Clubs
Two of Diamonds
Three of Clubs
Nine of Spades
Six of Spades
Four of Spades
Jack of Spades
Four of Hearts
King of Clubs
Eight of Hearts
Ten of Spades
Queen of Clubs
Four of Diamonds
Ace of Spades
Seven of Hearts
Two of Spades
Eight of Spades
Three of Diamonds
Two of Clubs
Nine of Diamonds
Two of Hearts
Jack of Clubs
Four of Clubs
King of Spades
Six of Hearts
Five of Spades
Nine of Clubs
Seven of Spades
Three of Hearts
Queen of Diamonds
Jack of Diamonds
Six of Diamonds
Eight of Clubs
Ace of Hearts
Seven of Clubs

Let me know in comments if you have any doubts. Do leave a thumbs up if this was helpful.


Related Solutions

IN C LANGUAGE ONLY PLEASE! First Program: create branchloop.c file Content of the program Enter your...
IN C LANGUAGE ONLY PLEASE! First Program: create branchloop.c file Content of the program Enter your name while running your program as you did with hello2.c (using argc and argv) name has to be 2 words max (e.g. Jose Lopez, Maria Rodrigues) . Make sure to check properly. Print your name 10 times using a loop. 10 is a fixed number. Count how many characters are in your first name and last name. If the number of characters of your...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment,...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and the...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked with writing a Python program (using linked lists) to keep track of computer equipment. For each piece of equipment, we track its name, purchase date, purchase amount, and quantity on hand. Write a program that completes the following tasks: allow the user to add a piece of equipment to the front of the list; allow the user to update the quantity of a piece...
JAVA Please put detailed comments as well explaining your program. I'm in a beginning programming class,...
JAVA Please put detailed comments as well explaining your program. I'm in a beginning programming class, and so please use more basic techniques such as if else statements, switch operators, and for loops if needed. http://imgur.com/a/xx9Yc Pseudocode for the main method: Print the headings             Print the directions             Prompt for the month             If the month is an integer       (Hint: Use the Scanner class hasNextInt method.)                         Input the integer for the month                         Get the...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class...
C++ Programming Create a C++ program program that exhibits polymorphism. This file will have three class definitions, one base class and three derived classes. The derived classes will have an inheritance relationship (the “is a” relationship) with the base class. You will use base and derived classes. The base class will have at least one constructor, functions as necessary, and at least one data field. At least one function will be made virtual. Class members will be declared public and...
Create a c++ program with this requirements: Create an input file using notepad ( .txt )...
Create a c++ program with this requirements: Create an input file using notepad ( .txt ) . When testing your program using different input files, you must change the filename inside your program otherwise there will be syntax errors. There are a finite number of lines to be read from the data file. But we can’t assume to know how many before the program executes; so, the standard tactic is to keep reading until you find the “End of File”...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
Please write a complete C coding program (NOT C++) that has the following: (including comments) -...
Please write a complete C coding program (NOT C++) that has the following: (including comments) - declares two local integers x and y - defines a global structure containing two pointers (xptr, yptr) and an integer (z) - declares a variable (mst) by the type of previous structure - requests the values of x and y from the user using only one scanf statement - sets the first pointer in the struct to point to x - sets the second...
This is C++ Create a program that reads an HTML file and converts it to plain...
This is C++ Create a program that reads an HTML file and converts it to plain text. Console: HTML Converter Grocery List * Eggs * Milk * Butter Specifications: The HTML file named groceries.html contains these HTML tags: <h1>Grocery List</h1> <ul> <li>Eggs</li> <li>Milk</li> <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the...
Python please! Create one program to include all 10 elements: Create a function that prints a...
Python please! Create one program to include all 10 elements: Create a function that prints a sentence passed to the function as parameter. On the next line, print the same sentence without the 1st and the last character, use string slicing. Use the following sentence: This is the sentence. Given a string: This string was copied in an article. Replace the relevant words to read as follows: This string was discovered in the article xyz. Given a string: This string...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT