Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming I need some help with creating a poker hand class. Create a PokerHand class. It should have a way to represent a 5

C++ Programming

I need some help with creating a poker hand class.

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 to the 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

Card.h, Card.cpp, DeckOfCards.h, DeckOfCards.cpp, PokerHand.h, PokerHand.cpp and pokerdriver.cpp that has your main.

My program so far.

// Card.cpp

#include #include #include "Card.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; }

-----------------------------------------------------------

// Card.h

#ifndef CARD_H #define CARD_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(); };

#endif

-------------------------------------------------------------------------

//DeckOfCards.h

#ifndef DECKOFCARD_H #define DECKOFCARD_H #include #include "Card.h"

class DeckOfCards { private: vectordeck; int currentCard; void swap(Card &first, Card &second); public: DeckOfCards(); void shuffle(); Card dealCard(); bool moreCards(); };

#endif

--------------------------------------------------------------------

//DeckOfCards.cpp

#include #include #include #include "DeckOfCards.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::swap(Card &first, Card &second) { Card temp = first ; first = second; second = temp; } void DeckOfCards::shuffle(){ time_t t; srand(time(&t)); for(int i=0; i<52; i++) { int s = rand()%52; swap (deck.at(i),deck.at(s)); } }

Card DeckOfCards::dealCard() { currentCard++; shuffle(); if (moreCards()) { return deck[currentCard - 1]; } else { cout << "The deck is empty " << endl; } }

bool DeckOfCards::moreCards() {

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

}

-----------------------------------------------------------------------------

// main.cpp

#include #include #include #include "Card.h" #include "DeckOfCards.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

AutoCAD Database Connectivity

Authors: Scott McFarlane

1st Edition

0766816400, 978-0766816404

More Books

Students also viewed these Databases questions