Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE DIVIDE YOUR PROGRAM INTO 3 FILES. 1 Header and 2 Cpp. files (Card Shuffli

ID: 3814853 • Letter: P

Question

PLEASE DIVIDE YOUR PROGRAM INTO 3 FILES. 1 Header and 2 Cpp. files

(Card Shuffling and Dealing) Create a program to shuffle and deal a deck of cards. The program 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) 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, then deal the 52 cards.

Explanation / Answer

Here is the code with comments for the given question. If compiling on linux,

use g++ Card.cpp DeckOfCards.cpp Driver.cpp -o main

and then run using ./main

Please don't forget to rate the answer if it helped. Thank you very much.

Card.h

#include <iostream>
using namespace std;
class Card
{
private:
int face;
int suit;
static const string FACES[];
static const string SUITS[];
public:
Card();
Card(int suit1,int face1);
string toString();
};

Card.cpp

#include <iostream>
#include "Card.h"
using namespace std;
//define the static variables out of class
const string Card::FACES[]={"Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
const string Card::SUITS[]={"Heart","Diamond","Club","Spade"};
Card::Card()
{
face=suit=0;
}
Card::Card(int suit1,int face1)
{
face=face1;
suit=suit1;
}

string Card::toString()
{
return FACES[face]+" of "+SUITS[suit];
}

DeckOfCards.h

#include <iostream>
#include <cstdlib>
#include "Card.h"
using namespace std;
class DeckOfCards
{
private:
Card deck[52];
int currentCard; //current card to deal
public:
DeckOfCards();
void shuffle();
Card dealCard();
bool moreCards();
};

DeckOfCards.cpp

#include <iostream>
#include <cstdlib>
#include "DeckOfCards.h"
using namespace std;

DeckOfCards::DeckOfCards()
{
//create the 13 cards for each suit total 13*4=52 cards
for(int suit=0,k=0;suit<4;suit++) //for the 4 suits
{
for(int face=0;face<13;face++) //for 13 cards in each suit
{
deck[k]=Card(suit,face);
k++;
}
}
currentCard=0;//initialize the currentCard
}

void DeckOfCards::shuffle()
{
Card temp;
int j;
srand(time(NULL));
for(int i=0;i<52;i++)
{
j=rand() % 52;
//swap the card at i with randomly generated j location
temp=deck[i];
deck[i]=deck[j];
deck[j]=temp;
}


}

Card DeckOfCards::dealCard()
{
Card c=deck[currentCard];
if(currentCard<52)
currentCard++; //increment if still there are cards
return c;
}

bool DeckOfCards::moreCards()
{
return currentCard<52; //if its less than 52, then there are more cards otherwise no
}

Driver.cpp

#include<iostream>
#include "DeckOfCards.h"
using namespace std;
int main()
{
DeckOfCards cards;
cards.shuffle();
while(cards.moreCards())
{
cout<<cards.dealCard().toString()<<endl;
}
}

output

Six of Spade
Queen of Diamond
Jack of Spade
Seven of Diamond
Six of Diamond
Ten of Diamond
Five of Diamond
Two of Heart
King of Club
Three of Heart
Three of Club
Eight of Diamond
Four of Spade
Queen of Spade
King of Diamond
Eight of Spade
Four of Club
Five of Heart
King of Heart
Ten of Club
Nine of Club
Ace of Club
Three of Diamond
Five of Club
Eight of Club
Ace of Spade
Ten of Heart
Three of Spade
Ace of Diamond
King of Spade
Jack of Diamond
Two of Spade
Seven of Spade
Seven of Club
Four of Heart
Ace of Heart
Jack of Club
Two of Club
Nine of Diamond
Five of Spade
Nine of Spade
Queen of Heart
Queen of Club
Ten of Spade
Eight of Heart
Jack of Heart
Six of Club
Four of Diamond
Seven of Heart
Six of Heart
Nine of Heart
Two of Diamond

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote