C++ (Card Shuffling and Dealing) Create a program to shuffle and deal a deck of
ID: 3704316 • Letter: C
Question
C++
(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:
Data members face and suit of type int.
A constructor that receives two int’s representing the face and suit and uses them to initialize the data members.
Two static arrays of strings representing the faces and suits.
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 vector of Cards named deck to store the Cards.
An integer currentCard representing the next card to deal.
A default constructor that initializes the Cards in the deck. The constructor should use vector function push_back to add each Card to the end of the vector after the Card is created and initialized. This should be done for each of the 52 Cards in the deck.
A shuffle function that shuffles the Cards in the deck. The shuffle algorithm should iterate through the vector of Cards. For each Card, randomly select another Card in the deck and swap the two Cards.
A dealCard function that returns the next Card object from the deck.
A moreCards function that returns a bool value indicating whether there are more Cards to deal.
Explanation / Answer
SourceCode:
Card.cpp
/*
* CPP file for the cards class. This file contains the various
* constants and class variable and method definitions. The class
* is declared in CardsH.h.
*
*/
#include "CardH.h"
#include <iostream>
#include <cstdio>
string Card::suits[4] = {"DIAMONDS","CLUBS","HEARTS","SPADES"};
string Card::faces[13] = {"ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE", "TEN", "JACK", "QUEEN", "KING"};
/*
* Constructor used to initialize an instance of Cards.
*/
Card::Card(int type, int num){
suit = suits[type];
face = faces[num];
}
/*
* The toString function prints out the card's face and suit.
*/
void Card::toString(){
string printout = face + " of " + suit;
cout << printout << endl;
// cout << face << " of " << suit;
}
/*
* The getFace function prints out the card's face.
*/
string Card::getFace(){
return face;
}
/*
* The getSuit function prints out the card's suit.
*/
string Card::getSuit(){
return suit;
}
CardH.h
/*
* Header file for the Card class. This file contains the various
* constants and class variable and method declarations. The class
* is defined in Card.cpp.
*/
#include <string>
#include <vector>
using namespace std;
// The main class header for the Card class.
class Card{
public:
// declares the public functions
Card(int, int);
void toString();
static string suits[4];
static string faces[13];
string getFace();
string getSuit();
private:
// declares the private fucntions.
string suit;
string face;
};
DeckOfCards.cpp
/*
* CPP file for the DeckOfCards class. This file contains the various
* constants and class variable and method declarations. The class
* is defined in DeckOfCards.h.
*/
#include "DeckOfCardsH.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
/*
* Constructor used to initialize an instance of DeckOfCards.
*/
DeckOfCards::DeckOfCards() {
currentCard=0;
// Iterate through the different suits
for (int i=0; i<4; i++){
// Iterate through the different face values
for (int j=0; j<13; j++){
Card c = Card(i,j);
deck.push_back(c);
}
}
// Shuffle the cards.
shuffle();
}
/*
* Shuffle function to shuffle all of the cards in the deck.
*/
void DeckOfCards::shuffle(){
srand(time(NULL));
for (int i=0; i<52; i++){
int s = rand() % 52;
swap(i,s);
}
}
/*
* The swap function swaps two cards in the deck. The card at position a
* is moved to position b and the card at position b is moves to position a.
*/
void DeckOfCards::swap(int a, int b){
Card tmp = deck[b];
deck[b] = deck[a];
deck[a] = tmp;
}
/*
* The dealCard function deals the next Card from the deck.
*/
Card DeckOfCards::dealCard(){
currentCard++;
if (moreCards()){
return deck[currentCard - 1];
}
else{
cout << "The deck is empty." << endl;
}
}
/*
* The moreCards function checks to see if there are more Cards left to deal.
*/
bool DeckOfCards::moreCards(){
if (currentCard < 52){
return true;
}
else{
return false;
}
}
DeckOfCards.h
/*
* Header file for the DeckOfCards class. This file contains the various
* constants and class variable and method declarations. The class
* is defined in DeckOfCards.cpp.
*/
#include <vector>
#include "CardH.h"
// The main class header for the DeckOfCards class.
class DeckOfCards{
public:
// Declares the public function
DeckOfCards();
void shuffle();
Card dealCard();
bool moreCards();
private:
vector<Card> deck;
int currentCard;
void swap(int, int);
};
main.cpp
#include <cstdlib>
#include <iostream.h>
#include <algorithm>
#include "DeckOfCardsH.h"
// Main method to begin inputing and executing code.
int main (int argc, char *argv[]){
// Set up the deck and deal the 5 cards.
DeckOfCards d = DeckOfCards();
vector<Card> hand;
cout << "The cards you have been dealt are: " << endl;
for (int i=0; i<5; i++){
hand.push_back(d.dealCard());
hand[i].toString();
}
// Check to see for pair(s).
int flag = 0;
for (int i=0; i<5; i++){
for (int j = i+1; j<5; j++){
if ((hand[i].getFace()).compare(hand[j].getFace()) == 0 && i != j){
flag++;
}
}
}
if (flag == 1){
cout << "A pair has been found!" << endl;
}
if (flag > 1){
cout << "Two pairs have been found!" << endl;
}
flag = 0;
// Check to see for triple.
for (int i=0; i<5; i++){
for (int j = i+1; j<5; j++){
for (int k= j+1; k<5; k++){
if ((hand[i].getFace()).compare(hand[j].getFace()) == 0 && (hand[i].getFace()).compare(hand[k].getFace()) == 0 && i != j != k){
flag++;
}
}
}
}
if (flag){
cout << "A triple has been found!" << endl;
flag = 0;
}
// Check to see for quadruples.
for (int i=0; i<5; i++){
for (int j = i+1; j<5; j++){
for (int k= j+1; k<5; k++){
for (int l = k+1; l<5; l++){
if ((hand[i].getFace()).compare(hand[j].getFace()) == 0 && (hand[i].getFace()).compare(hand[k].getFace()) == 0 && (hand[i].getFace()).compare(hand[l].getFace()) == 0 && i != j != k != l){
flag++;
}
}
}
}
}
if (flag){
cout << "A quadruple has been found!" << endl;
flag = 0;
}
// Check to see for flush.
if ((hand[0].getSuit()).compare(hand[1].getSuit()) == 0 && (hand[0].getSuit()).compare(hand[2].getSuit()) == 0 && (hand[0].getSuit()).compare(hand[3].getSuit()) == 0 && (hand[0].getSuit()).compare(hand[4].getSuit()) == 0){
cout<< "A flush has been found!" << endl;
}
// Check for straight. Convert the face values to numbers, store them in a vector, sort the vector, and then check to see if the numbers are consecutive.
vector<int> v;
for (int i=0; i<5; i++){
for (int j=0; j<13; j++){
if ((hand[i].getFace()).compare(hand[i].faces[j]) == 0){
v.push_back(j+1);
}
}
}
sort(v.begin(),v.end());
if (v[0] == v[1]-1 && v[0] == v[2]-2 && v[0] == v[3]-3 && v[0] == v[4]-4){
cout << "A straight has been found!" << endl;
}
}
//Please comment in here in case of any quieries and do rate the solution if it is helpful..Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.