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

C++ Programming Card Shuffling and Dealing Create a program to shuffle and deal

ID: 3869673 • Letter: C

Question

C++ Programming

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:

Private data members face and suit of type int.

A constructor that receives two ints 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:

An array 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.

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.

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.

The driver program should create a DeckOfCards object, shuffle the cards, then deal the 52 cards.

Create a PokerHand class.

It should have a way to represent a 5 card poker hand as a private data member.

There should be a constructor method that randomly creates a 5 card poker hand from a DeckOfCards.

It should also have a method to set the poker hand to a particular 5 card hand that the programmer specifies.

It should have methods that return booleans and take in 0 arguments to accomplish each of the following:

Determine whether the hand contains a pair.

Determine whether the hand contains two pairs.

Determine whether the hand contains three of a kind (e.g., three jacks).

Determine whether the hand contains four of a kind (e.g., four aces).

Determine whether the hand contains a flush (i.e., all five cards of the same suit).

Determine whether the hand contains a straight (i.e., five cards of consecutive face values).

Add your driver program the following.

Create a randomly generated PokerHand using your constructor and output the results of your PokerHand methods that determine what type of hand it has.

You should also create other PokerHands and set them to have a pair, two pair, three of a kind, four of a kind a flush and a straight respectively.

Use const where appropriate. I/O should be via the console

Explanation / Answer

DO RATE if satisfied

==================Card.h====================

/*
* Card.h
*
* Created on: Apr 24, 2014
*/

#ifndef CARD_H_
#define CARD_H_

#include <string>

/*
*
*/
class Card {
private:
int face;
int suit;
public:
static const std::string faces[];
static const std::string suits[];
Card();
Card(int suit, int face);
virtual ~Card();
std::string toString();
};
#endif /* CARD_H_ */

=============================Card.cpp-=======================

/*
* Card.cpp
*
* Created on: Apr 24, 2014
*/

#include "Card.h"

const std::string Card::faces[] = {"Ace", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"Jack", "Queen", "King"};
const std::string Card::suits[] = {"Spade", "Heart", "Diamond", "Club"};

Card::Card(){
face = 0;
suit = 0;
}
Card::Card(int suit, int face) {
this->face = face;
this->suit = suit;
}

Card::~Card() {
// TODO Auto-generated destructor stub
}
/*
* String representation of the Card
*/
std::string Card::toString(){
return Card::faces[face] + " of " + Card::suits[suit];
}

==========================DeckOfCard.h================================

/*
* DeckOfCards.h
*
* Created on: Apr 24, 2014
*/

#ifndef DECKOFCARDS_H_
#define DECKOFCARDS_H_

#include "Card.h"

/*
*
*/
class DeckOfCards {
private:
Card deck[52];
int currentCard;
public:
DeckOfCards();
void shuffleCards();
Card dealCard();
bool moreCards();
};

#endif /* DECKOFCARDS_H_ */

==================================DeckOfCards.cpp==============================

/*
* DeckOfCards.cpp
*
* Created on: Apr 24, 2014
*/
#include <stdlib.h>
#include "DeckOfCards.h"
/**
* Default Constructor for DeckOfCards. Initializes all cards
*/
DeckOfCards::DeckOfCards() {
for( int i=0; i<4; i++)
for(int j=0; j<13; j++)
deck[i*13+j] = Card(i, j);
currentCard = 0;
}
/**
* Shuffles cards in the deck;
*/
void DeckOfCards::shuffleCards(){
for(int i=0; i<52; i++){
int rnd = rand()%52;
Card temp = deck[i];
deck[i] = deck[rnd];
deck[rnd] = temp;
}
}

Card DeckOfCards::dealCard() {
return deck[currentCard++];
}

bool DeckOfCards::moreCards(){
return currentCard != 52;
}

================================Driver.cpp==========================

//============================================================================
// Name : Driver.cpp
// Author :
// Version :
// Copyright :
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <iomanip>
#include "card.h"
#include "DeckOfCards.h"

using namespace std;

int main() {
DeckOfCards doc;
doc.shuffleCards();

int j=0;
while (doc.moreCards()) {
Card c = doc.dealCard();
cout << setw(20) << c.toString();
if (++j % 4 == 0) {
j=0;
cout << endl;
}
}
}

Card.h:

#ifndef CARD_H
#define CARD_H
#include<string>
using namespace std;

class Card
{
private:
int iFace, iSuit;
//static const char *suit[4];
//static const char *face[13];
static string suit[4];
static string face[13];
public:
Card (int = 0, int = 0);
string toString() const;
char getSuitLetter() const;
short getCardNum() const;
  
};
#endif

DeckOfCards.h:

#ifndef DECK_H
#define DECK_H
#include "card.h"
#include<iostream>
#include <vector>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;

class DeckOfCards
{
private:
int currentCard;
int lastCard;
vector<Card>deck;
void initializeDeckOfCards();
void printCard();
public:
DeckOfCards();
void shuffle();
Card dealCard();
bool moreCards() const;
   int getNumDealed() const;
void printDeckOfCards(int, int);
};
#endif

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