I need help with the following question: Write a program to score five card poke
ID: 3558130 • Letter: I
Question
I need help with the following question:
Write a program to score five card poker hands into one of the following categories: nothing, one pair, two pairs, three of a kind , straight(in order with no gaps), flush(all the same suit, for example, all spades),full house(one pair and three of a kind), four of a kind, straight flush(both a straight and a flush). use two arrays, one to hold the value of the card and one to hold the suit. Include a loop that allows the user to continue to score more hand until the user says the program should end.
I found the solution elsewhere but I can't figure out how to make the hand user inputed with strings. Any help would be greatly appreciated.
Here is the code:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <ctime>
#include <iomanip>
using namespace std;
void shuffle(int[],int[]);
void dealcards(int[],int[],int[],int[]);
bool checkflush(int[]);
bool checkstraight(int[]);
void same(int [],bool &,bool &,bool &,bool &,bool&);
int main()
{
int i,j,flushes,decks[52],deckn[52];
int hands[5],handn[5],scored,count;
bool flush,straight;
bool pair,pair2,three,four,nothing;
string suit[4]={"hearts","diamonds","spades","clubs"};
string card[13]={"ace","2","3","4","5","6","7","8","9","10","jack","queen","king"};
int deal;
char yesno='Y';
srand(time(0));
do
{
scored=0;
shuffle(decks,deckn);
cout<<"deck-after shuffled: ";
for (j=0;j<52;j++)
cout << card[deckn[j]] << " " << suit[decks[j]] << endl;
cout << endl;
dealcards(hands,handn,decks,deckn);
cout<<"Hand: ";
for(j=0;j<5;j++)
cout<<card[handn[j]]<<" "<<suit[hands[j]]<<endl;
cout<<endl;
flush=checkflush(hands);
straight=checkstraight(handn);
same(handn,nothing,pair,pair2,three,four);
cout<<" Scoring: ";
if(straight&&flush)
cout<<"Straight Flush ";
else if(pair&&three)
cout<<"Full house ";
else if(straight)
cout<<"Straight ";
else if(flush)
cout<<"Flush ";
else if(four)
cout<<"Four of a kind ";
else if(three)
cout<<"Three of a kind ";
else if(pair2)
cout<<"Two pairs ";
else if(pair)
cout<<"One pair ";
else
cout<<"Nothing ";
cout<<" Play again (Y/N)?";
cin>>yesno;
}while(toupper(yesno)=='Y');
system("pause");
return 0;
}
bool checkstraight(int handn[])
{
int i,j,t;
for(i=0;i<4;i++)
for(j=i+1;j<5;j++)
if(handn[i]>handn[j])
{
t=handn[i];
handn[i]=handn[j];
handn[j]=t;
}
for(i=1;i<5;i++)
if(handn[i]!=handn[i-1]+1)
return false;
return true;
}
void same(int handn[],bool ¬hing,bool &pair,bool&pair2,bool &three,bool &four)
{
bool used[13]={false};
nothing=false;
pair=false;
pair2=false;
three=false;
four=false;
int b[13]={0};
int i,j;
for (i = 0; i < 5; i++)
{
if(!used[handn[i]])
{
int count = 1;
for(int j = i+1; j < 5; j++)
{
if(handn[j] == handn[i])
count++;
}
used[handn[i]]=true;
b[handn[i]]=count;
}
}
cout<<endl;
for(i=0;i<13;i++)
if(b[i]==4)
{
four=true;
return;
}
else if(b[i]==3)
{
three=true;
for(j=0;j<13;j++)
if(b[j]==2)
pair=true;
return;
}
else if(b[i]==2)
{
pair=true;
for(j=0;j<13;j++)
if(b[j]==3)
three=true;
for(j=i+1;j<13;j++)
if(b[j]==2)
{
pair2=true;
return;
}
return;
}
nothing=true;
return;
}
void dealcards(int hands[],int handn[],int decks[],int deckn[])
{
int i,j,cards=0;
for(i=0;i<5;i++)
{
hands[i]=decks[cards];
handn[i]=deckn[cards];
cards++;
}
return;
}
bool checkflush(int hands[])
{
int j,count=0,type,handcount;
type=hands[0];
handcount=1;
for(j=1;j<5;j++)
if(hands[j]==type)
handcount++;
if(handcount==5)
return true;
else
return false;
}
void shuffle(int decks[],int deckn[])
{
bool cards[4][13];
int i,j,num,type;
for(i=0;i<4;i++)
for(j=0;j<13;j++)
cards[i][j]=false;
for(j=0;j<52;j++)
{
do
{
num=rand()%13;
type=rand()%4;
}while(cards[type][num]);
decks[j]=type;
deckn[j]=num;
cards[type][num]=true;
}
return;
}
Explanation / Answer
//HEADERFILE SECTION
#include <iostream>
using namespace std;
#include <cstdlib>
#include <cctype>
// enumarated array declaration and initializition.
enum Hand_Value{ NOTHING, ONE_PAIR, TWO_PAIRS, THREE_OF_KIND,STRAIGHT, FLUSH,FULL_HOUSE,FOUR_OF_KIND,STRAIGHT_FLUSH};
enum Card_Suite { CLUBS, DIAMONDS, HEARTS, SPADES };
enum Face_Value { TWO, THREE, FOUR, FIVE, SIX, SEVEN,EIGHT,NINE, TEN, JACK, QUEEN, KING, ACE };
//card structure definition.
struct Card
{
Card_Suite suite;
Face_Value face;
};
//function prototypes.
void sort_hand ( Card hand[], int number_used );
//Prompts for and fetches a hand from standard input.
void input ( Card hand[] );
int has_four(Card hand[]);
int has_three (Card hand[]);
int is_full_house ( Card hand[] );
int has_two_pair( Card hand[]);
int has_one_pair( Card hand[]);
int is_straight (Card hand[]);
int is_flush( Card hand[] );
int Hand_Value evaluate(Card hand[]);
void output_hand( Card hand[] );
void report_hand_value( Card hand[]);
void output_hand( Card hand[] );
void sort_hand(Card a[], int number_used);
void swap_values(Card& v1, Card& v2);
int index_of_smallest(const Card a[], int start_index,int number_used);
//main function to start up program.
int main()
{
//variable declarations.
using namespace std;
char ans;
Card hand[5];
//Display help menu.
cout << "Poker Hand Evaluation Program - " << endl
<< "Please Enter a 5 card poker hand, "
<< "NOTHING, ONE_PAIR, TWO_PAIRS, THREE_OF_KIND, "
<< "STRAIGHT FLUSH, FULL_HOUSE" << endl
<< "FOUR_OF_KIND, or STRAIGHT_FLUSH" << endl
<< "Encode Clubs as c, Diamonds as d, Hearts as h, "
<< " Spades as s" << endl
<< "Enter the value as 2-9, t, j, q, k a, "
<< " Upper case OK " << endl << endl;
do
{
input ( hand );
cout << " Sorted hand is: " << endl;
output_hand( hand );
cout << "value of hand is: " ;
report_hand_value( hand );
cout << endl << endl;
cout << "Y/y continues, other halts." << endl << endl;
cin >> ans;
}while ( 'Y' == ans || 'y' == ans );
return 0;
system("pause");
}//end main
//report_hand_value function definition.
void report_hand_value( Card hand[])
{
switch( evaluate( hand ) )
{
case NOTHING: cout << "Nothing";
break;
case ONE_PAIR: cout << "One Pair";
break;
case TWO_PAIRS: cout << "Two Pair";
break;
case THREE_OF_KIND: cout << "Three of a Kind";
break;
case STRAIGHT: cout << "Straight";
break;
case FLUSH: cout << "Flush";
break;
case FULL_HOUSE: cout << "Full House";
break;
case FOUR_OF_KIND: cout << "Four of a Kind";
break;
case STRAIGHT_FLUSH: cout << "Straight Flush";
break;
default:cout << " Something is very wrong. Bad value ";
break;
}
}
//Hand_Value evaluate function definition.
Hand_Value evaluate( Card hand[] )
{
Hand_Value value;
if ( is_straight(hand) )
if (is_flush( hand ))
return STRAIGHT_FLUSH;
if ( has_four(hand) )
return FOUR_OF_KIND;
if (is_full_house(hand) )
return FULL_HOUSE;
if (is_flush(hand) )
return FLUSH;
if (is_straight(hand) )
return STRAIGHT;
if ( has_three(hand) )
return THREE_OF_KIND;
else if ( has_two_pair(hand) )
return TWO_PAIRS;
else if ( has_one_pair(hand) )
return ONE_PAIR;
else return NOTHING;
}
//get_face function definition.
void get_face ( Face_Value & value)
{
using namespace std;
char ch;
cin >> ch;
switch( ch )
{
case '2': value = TWO;
break;
case '3': value = THREE;
break;
case '4': value = FOUR;
break;
case '5': value = FIVE;
break;
case '6': value = SIX;
break;
case '7': value = SEVEN;
break;
case '8': value = EIGHT;
break;
case '9': value = NINE;
break;
case 't': value = TEN;
break;
case 'j': value = JACK;
break;
case 'q': value = QUEEN;
break;
case 'k': value = KING;
break;
case 'a': value = ACE;
break;
default: cout << "Bad face value. Aborting!";
exit(1);
}
}
//put_face function definition.
void put_face ( Face_Value& face)
{
using namespace std;
switch ( face )
{
case TWO: cout << 2;
break;
case THREE: cout << 3;
break;
case FOUR: cout << 4;
break;
case FIVE: cout << 5;
break;
case SIX: cout << 6;
break;
case SEVEN: cout << 7;
break;
case EIGHT: cout << 8;
break;
case NINE: cout << 9;
break;
case TEN: cout << 10;
break;
case JACK: cout << "Jack";
break;
case QUEEN: cout << "Queen";
break;
case KING: cout << "King";
break;
case ACE: cout << "Ace";
break;
default: cout << "Bad card face Aborting";
exit(2);
}
}
//Prompts for and fetches a hand from standard input and returns hand sorted by value.
void input ( Card hand[] )
{
using namespace std;
char suite;
Face_Value face;
for( int i = 0; i <= 4; i++)
{
cout << "Card " << i+1 << " suite: ";
cin >> suite;
cout << " " << "suite entered: " << suite << " " ;
cout << ", face: " ;
get_face( face );
cout << " face entered: ";
put_face( face );
cout << endl;
suite = tolower( suite );
hand[i].face = face;
switch ( suite )
{
case 'c': hand[i].suite = Card_Suite(0);
break;
case 'd': hand[i].suite = Card_Suite(1);
break;
case 'h': hand[i].suite = Card_Suite(2);
break;
case 's': hand[i].suite = Card_Suite(3);
break;
default: cout << "bad suite, aborting" << endl;
exit(1);
}
}
sort_hand( hand, 5 );
}
//returns true if the card values are in sequence.
int is_straight (Card hand[])
{
if(hand[0].face + 1 == hand[1].face
&& hand[0].face + 2 == hand[2].face
&& hand[0].face + 3 == hand[3].face
&& hand[0].face + 4 == hand[4].face )
return 1;
else
return 0;
}
//is_flush function definition.
int is_flush( Card hand[] )
{
if(hand[0].suite == hand[1].suite
&& hand[1].suite == hand[2].suite
&& hand[2].suite == hand[3].suite
&& hand[3].suite == hand[4].suite )
return 1;
else
return 0;
}
//has_four function definition.
int has_four(Card hand[])
{
//end of the hand!
if(hand[0].face == hand[1].face
&&hand[1].face == hand[2].face
&&hand[2].face == hand[3].face
||hand[1].face == hand[2].face
&&hand[2].face == hand[3].face
&&hand[3].face == hand[4].face )
return 1;
else
return 0;
}
//has_three function definition.
int has_three (Card hand[])
{
if(hand[0].face == hand[1].face
&&hand[1].face == hand[2].face
||hand[1].face == hand[2].face
&&hand[2].face == hand[3].face
||hand[2].face == hand[3].face
&&hand[3].face == hand[4].face )
return 1;
else
return 0;
}
//is_full_house function definition.
int is_full_house ( Card hand[] )
{
if(hand[0].face == hand[1].face
&&hand[1].face == hand[2].face
&&hand[3].face == hand[4].face
||hand[0].face == hand[1].face
&&hand[2].face == hand[3].face
&&hand[3].face == hand[4].face )
return 1;
else
return 0;
}
//has_two_pair function definition.
int has_two_pair( Card hand[])
{
if(hand[0].face == hand[1].face && //odd card is [4]
hand[2].face == hand[3].face||
hand[0].face == hand[1].face && //odd card is [2]
hand[3].face == hand[4].face||
hand[1].face == hand[2].face && //odd card is [0]
hand[3].face == hand[4].face )
return 1;
else
return 0;
}
//has_one_pair function definition.
int has_one_pair( Card hand[])
{
if ( hand[0].face == hand[1].face ||
hand[1].face == hand[2].face ||
hand[3].face == hand[4].face )
return 1;
else
return 0;
}
//lists hand in format suite, value.
void output_hand( Card hand[] )
{
using namespace std;
for (int i = 0; i < 5; i++ )
{
switch( hand[i].suite )
{
case CLUBS: cout << "Clubs ";
break;
case DIAMONDS: cout << "Diamonds";
break;
case HEARTS: cout << "Hearts ";
break;
case SPADES: cout << "Spades ";
break;
default:
cout << "something is WRONG - no such suite"<< endl;
exit(1);
}
cout << ' ';
put_face( hand[i].face );
cout << endl;
}
}
//sort_hand function definition.
void sort_hand(Card a[], int number_used)
{
int index_of_next_smallest;
for (int index = 0; index < number_used - 1; index++)
{//Place the correct value in a[index]:
index_of_next_smallest =
index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
}
}
//swap_values function definition.
void swap_values(Card& v1, Card& v2)
{
Card temp;
temp = v1;
v1 = v2;
v2 = temp;
}
//index_of_smallest function definition.
int index_of_smallest(const Card a[], int start_index,
int number_used)
{
Face_Value min = a[start_index].face;
int index_of_min = start_index;
for (int index = start_index + 1; index < number_used;
index++)
if (a[index].face < min)
{
min = a[index].face;
index_of_min = index;
}
return index_of_min;
}
Sample output:
Poker Hand Evaluation Program
Please Enter a 5 card poker hand, NOTHING, ONE_PAIR, TWO_PAIRS, THREE_OF_KIND, STRAIGHT FLUSH, FULL_HOUSE
FOUR_OF_KIND, or STRAIGHT_FLUSH
Encode Clubs as c, Diamonds as d, Hearts as h, Spades as s
Enter the value as 2-9, t, j, q, k a, Upper case OK
Card 1 suite: h
suite entered: h , face: 8
face entered: 8
Card 2 suite: s
suite entered: s , face: 8
face entered: 8
Card 3 suite: d
suite entered: d , face: 5
face entered: 5
Card 4 suite: h
suite entered: h , face: 5
face entered: 5
Card 5 suite: s
suite entered: s , face: 5
face entered: 5
Sorted hand is:
Diamonds 5
Hearts 5
Spades 5
Spades 8
Hearts 8
value of hand is: Full House
Y/y continues other halts.