HELP! What is wrong with my function?
Dec 11, 2011 at 12:28am UTC
Poker Hand. Need to double check why my function is giving me false positives. For example, when I have a 10(diamonds), 7(hearts), Jack(spades), 10(spades), 3(diamonds) I get "Three of a Kind". I sort the array based on value before sending to function.
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
int Game::HandCheck( void )
{
//jack or better
for ( int i = 0; i < 5; i++ )
{
if ( tempHand[ i ].GetValue() >= 11 || tempHand[ i ].GetValue() == 1 )
{
for ( int j = i + 1; j < 5; j++ )
{
if ( tempHand[ i ].GetValue() == tempHand[ j ].GetValue() )
f_handNum = 1;
}
}
}
//two pair
if ( tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() && tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() ||
tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() && tempHand[ 3 ].GetValue() == tempHand[ 4 ].GetValue() ||
tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() && tempHand[ 3 ].GetValue() == tempHand[ 4 ].GetValue() )
f_handNum = 2;
//three of a kind
if ( tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() && tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() ||
tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() && tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() ||
tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() && tempHand[ 3 ].GetValue() == tempHand[ 4 ].GetValue() )
f_handNum = 3;
//straight
if ( tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() + 1 && tempHand[ 0 ].GetValue() == tempHand[ 2 ].GetValue() + 2 &&
tempHand[ 0 ].GetValue() == tempHand[ 3 ].GetValue() + 3 && tempHand[ 0 ].GetValue() == tempHand[ 4 ].GetValue() + 4 ||
tempHand[ 0 ].GetValue() == 1 && tempHand[ 1 ].GetValue() == 11 && tempHand[ 1 ].GetValue() == 12 && tempHand[ 1 ].GetValue() == 13 )
f_handNum = 4;
//FLUSH
if ( tempHand[ 0 ].GetSuit() == tempHand[ 1 ].GetSuit() && tempHand[ 1 ].GetSuit() == tempHand[ 2 ].GetSuit() &&
tempHand[ 2 ].GetSuit() == tempHand[ 3 ].GetSuit() && tempHand[ 3 ].GetSuit() == tempHand[ 4 ].GetSuit() )
f_handNum = 5;
//FULL HOUSE
if ( tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() && tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() &&
tempHand[ 3 ].GetValue() == tempHand[ 4 ].GetValue() || tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() &&
tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() && tempHand[ 3 ].GetValue() == tempHand[ 4 ].GetValue() )
f_handNum = 6;
//FOUR OF A KIND
if ( tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() && tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() &&
tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() || tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() &&
tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() && tempHand[ 3 ].GetValue() == tempHand[ 4 ].GetValue())
f_handNum = 7;
//STRAIGHT FLUSH
if ( tempHand[ 0 ].GetSuit() == tempHand[ 1 ].GetSuit() && tempHand[ 1 ].GetSuit() == tempHand[ 2 ].GetSuit() &&
tempHand[ 2 ].GetSuit() == tempHand[ 3 ].GetSuit() && tempHand[ 3 ].GetSuit() == tempHand[ 4 ].GetSuit() )
{
if ( tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() + 1 && tempHand[ 0 ].GetValue() == tempHand[ 2 ].GetValue() + 2 &&
tempHand[ 0 ].GetValue() == tempHand[ 3 ].GetValue() + 3 && tempHand[ 0 ].GetValue() == tempHand[ 4 ].GetValue() + 4 ||
tempHand[ 0 ].GetValue() == 1 && tempHand[ 1 ].GetValue() == 11 && tempHand[ 1 ].GetValue() == 12 && tempHand[ 1 ].GetValue() == 13 )
f_handNum = 8;
}
//ROYAL FLUSH
if ( tempHand[ 0 ].GetSuit() == tempHand[ 1 ].GetSuit() && tempHand[ 1 ].GetSuit() == tempHand[ 2 ].GetSuit() &&
tempHand[ 2 ].GetSuit() == tempHand[ 3 ].GetSuit() && tempHand[ 3 ].GetSuit() == tempHand[ 4 ].GetSuit() )
{
if ( tempHand[ 0 ].GetValue() == 1 && tempHand[ 1 ].GetValue() == 11 && tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() + 1 &&
tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() + 1 && tempHand[ 3 ].GetValue() == tempHand[ 4 ].GetValue() + 1 )
f_handNum = 9;
}
return f_handNum;
}
Dec 11, 2011 at 5:08am UTC
What is the value of an uninitialized variable?
Dec 13, 2011 at 5:47pm UTC
1 = Ace. 2-10 11-13 = Face Cards
Dec 13, 2011 at 7:41pm UTC
Because C++ operators "&& and "||" are from left to right .. Your IF statement is
acctually true at line 4-5(which stops there)
1 2 3 4 5 6 7
if ( tempHand[ 0 ].GetValue() == tempHand[ 1 ].GetValue() &&
tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() ||
tempHand[ 1 ].GetValue() == tempHand[ 2 ].GetValue() &&
tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() ||
tempHand[ 2 ].GetValue() == tempHand[ 3 ].GetValue() &&
tempHand[ 3 ].GetValue() == tempHand[ 4 ].GetValue() )
f_handNum = 3;
I would assume you sorted your array increasing order.
Original = 10,7,11(JACK),10,3 .. Sorted= 3,7,10,10,11(JACK)
Then the if statement is looking like this:
1 2 3
if ((3==7 && 7==10) (7==10 || 7==10) (7==10 && 10==10) (10==10 || 10==10) STOPS..)
false false false true
f_handnum = 3;
I Hope this helps.
Last edited on Dec 14, 2011 at 5:37am UTC
Topic archived. No new replies allowed.