I'm having problems separating code.
Oct 28, 2014 at 11:22pm UTC
Hello,
my teacher told us to take his program and divide the poker hand into a class and using functions. I am having a problem putting the code in a class as well as separating it into functions. If someone could please help, that would be AMAZING.
Thanks much guys,
Ryan
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
// Jim Hester
// Homework 05 Solution
// Evaluating a simplified poker hand.
#include<iostream>
#include<string>
using namespace std;
// input range
const int MIN_CARD_NORMAL_VALUE = 1;
const int MAX_CARD_NORMAL_VALUE = 13;
// numberic values of cards
const int ACE_LOW_VALUE = 1;
const int MIN_NON_FACE_CARD_VALUE = 2;
const int MAX_NON_FACE_CARD_VALUE = 10;
const int JACK_VALUE = 11;
const int QUEEN_VALUE = 12;
const int KING_VALUE = 13;
const int ACE_HIGH_VALUE = 14;
// face card names
const string JACK_NAME = "jack" ;
const string QUEEN_NAME = "queen" ;
const string KING_NAME = "king" ;
const string ACE_NAME = "ace" ;
// non-face card (number) names
const string NUMBER_2_NAME = "two" ;
const string NUMBER_3_NAME = "three" ;
const string NUMBER_4_NAME = "four" ;
const string NUMBER_5_NAME = "five" ;
const string NUMBER_6_NAME = "six" ;
const string NUMBER_7_NAME = "seven" ;
const string NUMBER_8_NAME = "eight" ;
const string NUMBER_9_NAME = "nine" ;
const string NUMBER_10_NAME = "ten" ;
int main(void )
{
// 1 Input hand
// 1.1 basic input
int card1, card2, card3;
cout << "Enter three cards in the range "
<< MIN_CARD_NORMAL_VALUE
<< " to "
<< MAX_CARD_NORMAL_VALUE
<< ", separated by spaces: " ;
cin >> card1 >> card2 >> card3;
cin.ignore(999,'\n' );
// 1.2 format check
if ( cin.fail() )
{
cin.clear();
cin.ignore(999,'\n' );
cout << "Input format error. Press ENTER to terminate: " ;
cin.ignore(999,'\n' );
return 1;
}
// 1.3 range check
if ( card1 < MIN_CARD_NORMAL_VALUE || card1 > MAX_CARD_NORMAL_VALUE
|| card2 < MIN_CARD_NORMAL_VALUE || card2 > MAX_CARD_NORMAL_VALUE
|| card3 < MIN_CARD_NORMAL_VALUE || card3 > MAX_CARD_NORMAL_VALUE )
{
cout << "Input range error. Press ENTER to terminate: " ;
cin.ignore(999,'\n' );
return 1;
}
// 2 process
// 2.1 Adjust aces from low to high cards
// (makes things much easier later)
if ( card1 == ACE_LOW_VALUE ) card1 = ACE_HIGH_VALUE;
if ( card2 == ACE_LOW_VALUE ) card2 = ACE_HIGH_VALUE;
if ( card3 == ACE_LOW_VALUE ) card3 = ACE_HIGH_VALUE;
// 2.2 sort cards
if ( card1 > card2 )
{
int temp = card1;
card1 = card2;
card2 = temp;
}
if ( card2 > card3 )
{
int temp = card2;
card2 = card3;
card3 = temp;
}
if ( card1 > card2 )
{
int temp = card1;
card1 = card2;
card2 = temp;
}
// 2.3 determine which hand type(s) apply
// (these look for a given hand criteria only - higher hands may exist)
bool is3OfAKind = card1 == card3;
bool isSimpleStraight = card2 == card1+1 && card3 == card2+1;
bool isStraightWithAceLow = card3 == ACE_HIGH_VALUE
&& card1 == ACE_LOW_VALUE+1
&& card2 == card1+1;
bool isStraight = isSimpleStraight || isStraightWithAceLow;
bool isPair = card1 == card2 || card2 == card3;
// 2.4 Key Card
// Most significant card of hand.
// 2.4.1 choose
int keyCard;
if ( is3OfAKind ) keyCard = card3; // high card
else if ( isStraightWithAceLow ) keyCard = card2; // end of straight
else if ( isStraight ) keyCard = card3; // end of straight
else if ( isPair ) keyCard = card2; // value of pair cards
else keyCard = card3; // high card
// 2.4.2 name
string keyCardName;
if ( keyCard == 2 ) keyCardName = NUMBER_2_NAME;
else if ( keyCard == 3 ) keyCardName = NUMBER_3_NAME;
else if ( keyCard == 4 ) keyCardName = NUMBER_4_NAME;
else if ( keyCard == 5 ) keyCardName = NUMBER_5_NAME;
else if ( keyCard == 6 ) keyCardName = NUMBER_6_NAME;
else if ( keyCard == 7 ) keyCardName = NUMBER_7_NAME;
else if ( keyCard == 8 ) keyCardName = NUMBER_8_NAME;
else if ( keyCard == 9 ) keyCardName = NUMBER_9_NAME;
else if ( keyCard == 10 ) keyCardName = NUMBER_10_NAME;
else if ( keyCard == JACK_VALUE ) keyCardName = JACK_NAME;
else if ( keyCard == QUEEN_VALUE ) keyCardName = QUEEN_NAME;
else if ( keyCard == KING_VALUE ) keyCardName = KING_NAME;
else if ( keyCard == ACE_HIGH_VALUE ) keyCardName = ACE_NAME;
else keyCardName = "error" ;
// 2.4.3 english support
// 2.4.3.1 article
string keyCardNameArticle;
if ( keyCardName == ACE_NAME || keyCardName == NUMBER_8_NAME )
keyCardNameArticle = "an" ;
else
keyCardNameArticle = "a" ;
// 2.4.3.2 pluralization
string keyCardNamePlural;
if ( keyCardName == NUMBER_6_NAME )
keyCardNamePlural = "es" ;
else
keyCardNamePlural = "s" ;
// 3 output hand evaluation
cout << endl;
cout << "You have " ;
if ( is3OfAKind ) cout << "three " << keyCardName << keyCardNamePlural << '.' ;
else if ( isStraight) cout << keyCardNameArticle << ' ' << keyCardName << "-high straight." ;
else if ( isPair ) cout << "a pair of " << keyCardName << keyCardNamePlural << "." ;
else cout << keyCardNameArticle << ' ' << keyCardName << '.' ;
cout << endl;
// 4 finish up
cout << endl;
cout << "Press ENTER to finish..." ;
cin.ignore(999,'\n' );
return 0;
}
Topic archived. No new replies allowed.