In: Computer Science
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.
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.