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

Part 1 Create a program to shuffle and deal a deck of cards. The program should

ID: 3726428 • Letter: P

Question

Part 1

Create a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and a driver program with a main function. 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.

Part 2

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. Call each of the hand determination methods you wrote on each hand and output the results to standard output to show that the methods are working correctly.

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

You should be turning in a Card.h, Card.cpp, DeckOfCards.h, DeckOfCards.cpp, PokerHand.h, PokerHand.cpp and pokerdriver.cpp that has your main.

Explanation / Answer

SourceCode:

part-1:

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;
}
}