C++ Program Help! I cant figuer out how to make this program deal two five-card
ID: 3707679 • Letter: C
Question
C++ Program
Help! I cant figuer out how to make this program deal two five-card poker hands, evaluates each hand and determines which is the better hand. It also needs to have the dealer’s five-card hand is dealt “face down” so the player can not see it. Then it should evaluate the dealer’s hand, and, based on the quality of the hand, the dealer should draw one, two or three more cards to replace the corresponding number of unneeded cards in the original hand. The program should then reevaluate the dealer’s hand.
If it cannot be done with the following program provided please specify why, Thank You For Helping!
//DeckOfCards.h
#ifndef DECKOFCARDS_H
#define DECKOFCARDS_H
#include <vector>
#include "Card.h"
class DeckOfCards {
private:
vector<Card> deck;
int currentCard;
void swap(int, int);
public:
// Declares the public function
DeckOfCards();
void shuffle();
Card dealCard();
bool moreCards();
};
#endif // !DECKOFCARDS_H
//DeckOfCards.cpp
#include<iostream>
#include<string>
#include"DeckOfCards.h"
#include <cstdlib>
#include<ctime>
#include<time.h>
unsigned seed;
using namespace std;
/*
* 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(0));
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;
}
}
//Card.h
#ifndef CARD_H
#define CARD_H
#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;
};
#endif
//Card.cpp
#include "Card.h"
#include <iostream>
#include <cstdio>
string Card::suits[4] = { "DIAMONDS","CLUBS","HEARTS","SPADES" };
string Card::faces[13] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "JACK", "QUEEN", "KING","ACE" };
/*
* 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;
}
string Card::getFace() {
return face;
}
string Card::getSuit() {
return suit;
}
//PokerHand.h
#ifndef POKERHAND_H
#define POKERHAND_H
#include <vector>
#include "Card.h"
#include"DeckOfCards.h"
using namespace std;
class PokerHand {
public:
PokerHand();
bool pair();
bool twoPairs();
bool threeOfKind();
bool fourOfKind();
bool flush();
bool straight();
vector<Card>hand;
};
#endif
//PokerHand.cpp
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include"Card.h"
#include "DeckOfCards.h"
#include"PokerHand.h"
using namespace std;
PokerHand::PokerHand() {
// Set up the deck and deal the 5 cards.
DeckOfCards doc = DeckOfCards();
cout << "The cards you have been dealt are: " << endl;
for (int i = 0; i < 5; i++) {
hand.push_back(doc.dealCard());
hand[i].toString();
}
}
bool PokerHand::pair() {
// Check to see for pair(s).
int pairFound = 0;
for (int count = 0; count<5; count++) {
for (int j = count + 1; j<5; j++) {
if ((hand[count].getFace()).compare(hand[j].getFace()) == 0 && count != j) {
pairFound++;
}
}
}
if (pairFound == 1) {
cout << "A pair found!" << endl;
return true;
}
else
return false;
}
bool PokerHand::twoPairs() {
int twoPairsFound = 0;
for (int count = 0; count<5; count++) {
for (int j = count + 1; j<5; j++) {
if ((hand[count].getFace()).compare(hand[j].getFace()) == 0 && count != j) {
twoPairsFound++;
}
}
}
if (twoPairsFound == 2) {
cout << "Two pairs found!" << endl;
return true;
}
else
return false;
}
bool PokerHand::threeOfKind() {
int threeOfKindFound = 0;
for (int count = 0; count<5; count++) {
for (int x = count + 1; x<5; x++) {
for (int k = x + 1; k<5; k++) {
if ((hand[count].getFace()).compare(hand[x].getFace()) == 0 &&
(hand[count].getFace()).compare(hand[k].getFace()) == 0 &&
count != x != k) {
threeOfKindFound++;
}
}
}
}
if (threeOfKindFound) {
cout << "Three of a kind found!" << endl;
return true;
}
else
return false;
}
bool PokerHand::fourOfKind() {
int flag = 0;
for (int count = 0; count<5; count++) {
for (int j = count + 1; j<5; j++) {
for (int k = j + 1; k<5; k++) {
for (int l = k + 1; l<5; l++) {
if ((hand[count].getFace()).compare(hand[j].getFace()) == 0 &&
(hand[count].getFace()).compare(hand[k].getFace()) == 0 &&
(hand[count].getFace()).compare(hand[l].getFace()) == 0 &&
count != j != k != l) {
flag++;
}
}
}
}
}
if (flag) {
cout << "A quadruple found!" << endl;
return true;
}
else
return false;
}
bool PokerHand::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;
return true;
}
else
return false;
}
bool PokerHand::straight() {
vector<int> vector2;
for (int count = 0; count<5; count++) {
for (int j = 0; j<13; j++) {
if ((hand[count].getFace()).compare(hand[count].faces[j]) == 0) {
vector2.push_back(j + 1);
}
}
}
sort(vector2.begin(), vector2.end());
if (vector2[0] == vector2[1] - 1 && vector2[0] == vector2[2] - 2 &&
vector2[0] == vector2[3] - 3 && vector2[0] == vector2[4] - 4) {
cout << "A straight found!" << endl;
return true;
}
else
return false;
}
//PokerHandDriver.cpp
#include <iostream>
#include<stdlib.h>
#include"Card.h"
#include "DeckOfCards.h"
#include"PokerHand.h"
#include<vector>
using namespace std;
int main() {
DeckOfCards doc = DeckOfCards();
vector<bool> hand;
PokerHand ph;
hand.push_back(ph.pair());
hand.push_back(ph.twoPairs());
hand.push_back(ph.threeOfKind());
hand.push_back(ph.fourOfKind());
hand.push_back(ph.flush());
hand.push_back(ph.straight());
system("pause");
return 0;
}
Explanation / Answer
//This will deal two five card poker hand
//and then determine the winner from amongst handss
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <time.h>
#include<conio.h>//single character imput...
//...without the use of carriage return
void shuffle( int [][ 13 ] );
void deal(int[],int[],int[],int[]);
void print(int[],int[]);
int Ppair(int[],int[]);
int Twopair(int[],int[]);
int threeofkind(int[],int[]);
int fourofkind(int[],int[]);
int flush(int[],int[]);
int straight(int[],int[]);
void clear_deck(int[],int[],int[],int[]);
int Tie_Check(int[],int[],int);
int deck[ 4 ][ 13 ] = { 0 };
const char *suit[ 4 ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
const char *face[ 13 ] =
{ "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
const char *type[7]={"none","pair","twopairs","threeofkind",
"fourofkind","flush","straight"};
char A=3,B=4,C=5,D=6;
int main()
{
char welcome[]={"Welcome to Poker!."
"...where aces are low. "};
/////////////////////////////////////
//printng a welcome screen
/////////////////////////////////////////////
for(int move=0;welcome[move]!='';move++)
{
cout<<welcome[move];
for(int timer=0;timer<9999980;timer++);
if(move==7||move==10||move==18)
cout<<endl;
}
///////////////////////////////////////////
cout<<" Do you want to run the Poker program? "
<<"'y' for -yes-, any key for -no-: ";
cout<<endl;
int answer=getch();//single character imput
cout<<endl;
if(answer!='y')
return 0;
///////////////////////////////////////////////////
int t=5;
int hand1[5],hand2[5],hand3[5],hand4[5];
int handtype1,handtype2;
///////////////////////
srand( time(NULL) );
///////////////////
shuffle( deck );
///////////////////
deal(hand1,hand2,hand3,hand4);
///////////////////////
cout<<"Player One: ";
/////////////////////////
print(hand1,hand2);//Print the first hand
cout<<endl;
///////////////////////
handtype1=Ppair(hand2,hand1);//Begin hand classificatiosn process
cout<<endl;
//////////////////////////////
cout<<"Player Two: ";
/////////////////////////////////
print(hand3,hand4);//Print the second hand
cout<<endl;
cout<<endl;
///////////////////////////////////
handtype2=Ppair(hand4,hand3);
////////////////////////////////////
cout<<"Player One: Player Two:"
<<" "<<type[handtype1]
<<" "<<type[handtype2]<<endl;
////////////////////////////////////////////////////////
if(handtype1>handtype2)
cout<<" Player one Wins! ";
//////////////////////////////
if(handtype1<handtype2)
cout<<" Player two Wins! ";
///////////////////////////////
if(handtype1==handtype2)
Tie_Check(hand2,hand4,handtype1);
////////////////////////////////////
cout<<" Press 'e' to exit. "
<<"Press any key to continue.";
//////////////////////////////////////////////
clear_deck(hand1,hand2,hand3,hand4);
////////////////////////////////////////
cout<<endl;
int again=getch();
///////////////////
if(again=='e')
return 0;
////////
system("cls");
main();
//////
return 0;
}
//////////////////////////////////////////////////////
void shuffle( int wDeck[][ 13 ] )
{
int row,column;
for ( int card = 1; card <= 52; card++ ) {
do {
row = rand() % 4;
column = rand() % 13;
} while( wDeck[ row ][ column ] != 0 );
wDeck[ row ][ column ] = card;
}
}
//////////////////////////////////////////////////////////////////
void deal(int ahand[5],int bhand[5],int chand[5],int dhand[5])
{
int x=0,y=0;//counter for each hand
for ( int card = 1; card <=10; card++ )
for ( int row = 0; row <= 3; row++ )
for ( int column = 0; column <= 12; column++ )
//this will deal the hands one by ones
if ( deck[ row ][ column ] == card )
{
if(card%2==1){
ahand[x]=row;
bhand[x]=column;
x++;
}
else
{
chand[y]=row;
dhand[y]=column;
y++;
}
}
}
//\///////////////\\\\\\\\\//
void print(int hand1[5],int hand2[5])
{
for ( int num=0; num<5; num++ )
{
cout << setw( 5 )<< setiosflags( ios::right )
<< face[hand2[num]] << " of "
<< setw( 8 )<< setiosflags( ios::left )
<< suit[hand1[num]];
if(hand1[num]==0)
cout<<A;
if(hand1[num]==1)
cout<<B;
if(hand1[num]==2)
cout<<C;
if(hand1[num]==3)
cout<<D;
cout<<endl;
}
}
/////////////////////////////////////////////////
int Ppair(int phand[5],int qhand[5])
{
int frequency[13]={0},keep=0;
int track;
//first arrange the array in ascending osrder
for(int b=0;b<4;b++)
for(int c=0;c<4;c++)
if(phand[c]<phand[c+1])
{
track=phand[c+1];
phand[c+1]=phand[c];
phand[c]=track;
}
//this is to find out how mand of each faces there iss
for(int p=0;p<5;p++)
++frequency[phand[p]];
for(int p2=0;p2<=12;p2++)
{
//using frequency to check for a pair
if(frequency[p2]==2)
{
keep+=2;
}
}
if(keep==2)//if there is only one pair
{
cout<<"Player has a pair! ";
return 1;
}
//if there is no pair...
//check to two pairs
else
return Twopair(phand,qhand);
}
/////////////////////////////////////////////////////////
int Twopair(int phand[5],int qhand[5])
{
int frequency[13]={0},keep=0;
for(int p=0;p<5;p++)
++frequency[phand[p]];
for(int p2=0;p2<=12;p2++)
{
if(frequency[p2]==2)
{
keep+=2;
}
}
if(keep==4)//Only if there are two pairs
{
cout<<"Player has two pairs! ";
return 2;
}
//if there are no two pairs...
//check for three of a kind
else
return threeofkind(phand,qhand);
}
//////////////////////////////////////////////////////
int threeofkind(int phand[5],int qhand[5])
{
int frequency[13]={0},keep=0;
for(int p=0;p<5;p++)
++frequency[phand[p]];
for(int p2=0;p2<=12;p2++)
{
if(frequency[p2]==3)
{
keep+=3;
}
}
if(keep==3)//if there is three of a kind
{
cout<<"Player has three of a kind! ";
return 3;
}
else//if there is no three of a kind...
//check for four of a kind
return fourofkind(phand,qhand);
}
////////////////////////////////////////////////
int fourofkind(int phand[5],int qhand[5])
{
int frequency[13]={0},keep=0;
for(int p=0;p<5;p++)
++frequency[phand[p]];
for(int p2=0;p2<=12;p2++)
{
if(frequency[p2]==4)
{
keep+=4;
break;
}
}
if(keep==4)//if there is a four of a kind
{
cout<<"Player has four of a kind ";
return 4;
}
else //if there is no four of a kind//
//check for a flush
return flush(phand,qhand);
}
///////////////////////////////////////////////////////
int flush(int phand[5],int qhand[5])
{
int frequency[13]={0},keep=0;
for(int p=0;p<5;p++)//using the suit in this
//case to determine if
//hand is a flush
++frequency[qhand[p]];
for(int p2=0;p2<=12;p2++)
{
keep=frequency[p2];
if(keep==5)
{
break;
}
}
if(keep==5) //if there is a flush
{
cout<<"Player has a flush! ";
return 5;
}
else//if there is no flush...
//check for a straight
return straight(phand,qhand);
}
//////////////////////////////////////////////////////
int straight(int phand[5],int qhand[5])
{
int frequency[5]={0},keep=0, x,y,z;
//checking to make sure that hand is not a flush
for(int suit_count=0;suit_count<5;suit_count++)
++frequency[qhand[suit_count]];
/*a straight is determined by checking:
if the next value is one more than the
previous value.....*/
for(int p=0;p+1<5;p++)
{
x=phand[p]; y=phand[p+1];
/*...if the next value is not one less than
the previous value, 'z' will be assingned
the value '1' to indocate that there is no 'straight'*/
if(x+1!=y||frequency[p]==5)
{
z=1;
break;
}
}
if(z==1)//if there is no 'straight'...
//return zero to indicate that...
//there was no match for any
//hand type
{
return 0;
}
else//if there is a a 'straight'...
//...return '6' to indicate so
{
cout<<"Player has a straight! ";
return 6;
}
}
/////////////////////////////////////////////////////
//clearing the deck and hands
void clear_deck(int Hand1[5],int Hand2[5],
int Hand3[5],int Hand4[5])
{
for(int a=0;a<=3;a++)
for(int b=0;b<=12;b++)
deck[a][b]=0;
for(int c=0;c<5;c++){
Hand1[c]=0;Hand2[c]=0;
Hand3[c]=0;Hand4[c]=0;}
}
/////////////////////////////////////////////////////
int Tie_Check(int hand2[5],int hand4[5],int ht)
{
//local functions
void pair_arrange(int[],int);
int tie_breaker(int[],int[],int);
int sum=0,sum2=0;
/*if the hands have no match or
the hands are both 'straights',
invoke the tie breaker function
without the need to arrainge the
hands in pairs as there should be no pairs
in both hands*/
if(ht==0||ht==6)tie_breaker(hand2,hand4,ht);
else
{
pair_arrange(hand2,ht);
pair_arrange(hand4,ht);
/*the pair arrange function will
multiply the first two pairs and
assign it to the first element in the array.
So the following first pair to determine a winner */
if(hand2[0]>hand4[0])cout<<"Player One Wins!";
if(hand2[0]<hand4[0])cout<<"Player Two Wins!";
if(hand2[0]==hand4[0])
tie_breaker(hand2,hand4,ht);
}
return 0;
}
//////////////////////////////////////////////////////
void pair_arrange(int h2[],int tp)
{
int ar[5]={0},ar2[5]={0},count=2;
////////////////////////////
int frequency[13]={0},
a,aa,keep=0;
//////////////////////
for(int q=0;q<5;q++)
{
++frequency[h2[q]];
}
if(tp==1)//if the hand is a pair
//the code will group it accoidingly
{
for(a=0;a<13;a++)
{
if(frequency[a]==2)
{
ar[0]=a*2;//take the pair, multiply it by two
//and assign it to the first
//element in the array...
break;
}
}
//...then take the rest and
//assign them to each folowing element
int y=1;
for(int b=0;b<a;b++)
{
if(frequency[b]==1)
{
ar[y]=b;
y++;
}
}
}
else
///////////////////////////////
if(tp==2||tp==3)//if the hand is a 'two pair' or 'three of a kind'
//the subscript will be multiplied by three
{
if(tp==3)
count=3;
for(aa=0;aa<13;aa++)
{
if(frequency[aa]==count)
{
ar2[0]=aa*count;
break;
}
}
//////////////////////////////
//this is not necessary if
//the hand is a three-of-kind hand
if(tp!=3)
{
int yy=1;
for(int bb=aa+1;yy<=1;bb++)
{
if(frequency[bb]==2)
{
ar[1]=bb*2;//this is for the two pari hand
break;
}
}
}
//////////////////////////////
int con=2;
if(tp==3)
con=1;
for(int bb=0;bb<13;bb++)
{
if(tp==3&&con==3)//if hand is a 'three-of-kind'
//the the loop needs to break
//at two
break;
if(frequency[bb]==1)
{
ar[con]=bb;
if(tp==3)con++;
}
}
}
cout<<endl;
///////////////////////////////
//copying the values back to argument 'h2'...
//the array containing a hand-no suits, only faces
for(int copy=0;copy<5;copy++)
{
h2[copy]=ar[copy];
}
}
///////////////////////////////////////////////////////////////
//this function determines the winner if the pairs are tied
int tie_breaker(int h2[],int h4[],int ty)
{
int b,add,wild=1;
if(ty==1)add=4;if(ty==2)add=3;
if(ty==3)add=3;if(ty==4)add=2;
if(ty==0||ty==6){wild=0;add=5;}
//comparing each value elememt by element
//if the element in one hand is equal to the corresponding
//element in the other hand, the iteration will
//continue until there is one that is greater than
//of less than to determine the winner.
//if the iteration terminates and no winner is found,
//the game is a tie.
for(b=wild;b<add;b++)
{
if(h2[b]>h4[b])
{
cout<<" Player One Wins! ";
return 0;
}
if(h2[b]<h4[b])
{
cout<<" Player Two Wins! ";
return 0;
}
}
if(b==add)
{
cout<<" Tie Game! Both Players have the same hand ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.