Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming I keep getting an error message and I can't figure out where it is. Here is the question. Card Shuffling and Dealing Create

C++ Programming

I keep getting an error message and I can't figure out where it is.

Here is the question.

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

And here is my code. Card.cpp

#include #include #include "CardH.h" #include

using namespace std;

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

Card::Card(int type,int num) { suit = suits[type]; face = faces[num]; }

void Card::toString() { string printout = face + " of " + suit; cout << printout << endl; }

string Card::getFace() { return face; }

string Card::getSuit() { return suit; }

______________________________________________________ CardH.h

#include #include using namespace std;

class Card { private:

string face; string suit;

public:

static string faces[13]; static string suits[4]; string getFace(); string getSuit(); Card(int, int); void toString(); };

________________________________________

DeckOfCardsH.h

#include #include "CardH.h"

class DeckOfCards { private: vectordeck; int currentCard; void swap(int, int);

public: DeckOfCards(); void shuffle(); Card dealCard(); bool moreCards(); };

___________________________________________________

DeckOfCards.cpp

#include #include #include #include "DeckOfCardsH.h"

DeckOfCards::DeckOfCards() { currentCard = 0; for( int i=0; i<4; i++){

for(int j=0; j<13; j++){

Card c = Card(i,j); deck.push_back(c); } }

}

void DeckOfCards::shuffle(){ srand(time(NULL)); for(int i=0; i<52; i++) { int s = rand()%52; swap (i,s); } }

Card DeckOfCards::dealCard() { currentCard++;

if (moreCards()) { return deck[currentCard - 1]; } else { cout << "The deck is empty " << endl; } }

bool DeckOfCards::moreCards() {

if (currentCard < 52) { return true; } else { return false; }

}

__________________________________________________

Driver.cpp

#include #include #include #include "cardH.h" #include "DeckOfCardsH.h"

using namespace std;

int main(int argc, char *argv[]) { DeckOfCards d = DeckOfCards(); vectorhand;

cout << "The cards you have been delt are: " << endl;

for (int i=0;i<5;i++) { hand.push_back(d.dealCard()); hand[i].toString(); }

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 << "You have been delt a pair! " << endl; }

if (flag > 1) { cout << "You have been delt two pair! " << endl; }

flag = 0;

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 << "You have been delt Three of a kind! " << endl; flag = 0; }

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 << "You have been delt a flush! " << endl;

vectorv; 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 << "You have been delt a straight! " << endl; } }

system ("pause"); return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions