Help with checking for a straight

closed account (SETp4iN6)
I'm have a small issue with evaluating a straight in a seven card hand, where you want to get the highest 5 card straight.

I can get highest 5 card straight as long as there is not a pair in the hand so for example:

hole cards: 2h,3d
community cards:4h, 5s, 6s, 8d, 9d,
Output: 2h,3d,4h,5s,6s

because there is no pair within the straight it counts and outputs just fine. The problem occurs when there is a pair within the straight for example:

hole cards: 2h, 3d,
community cards: 4h, 4c, 5s, 6s, 8d
OutPut: 2h,3d,4h,4c,5s

so it is counting the pair in the straight, I need a way to skip past one of the paired cards and select only one. This happens with 3 of a kind as well.

It makes complete sense to me why it is doing this, but for the life of me I cant think of a way to fix it.

Here is the 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
bool Player::checkForStraight()
{
//Loops through the ranks from greatest to least 0 is NONE is the enum class
for (int i = FACES; i >= 1; i--)
{
    //Checks to see if a straight exists (_checkNumfaces is an array[13] to
    //hold the amount of each card
    if ((_checkNumFaces[i - 1] >= 1) && (_checkNumFaces[i - 2] >= 1) &&
        (_checkNumFaces[i - 3] >= 1) && (_checkNumFaces[i - 4] >= 1) &&
        (_checkNumFaces[i - 5] >= 1))
    {
        //If a straight exists loop through the cards(sorted in another 
        //function from lowest to highest
        for (int j = 6; j >= 0; j--)
        {
                //if the face matches the enum value of i then we have a
                //straight there down because I checked if the straight
                //existed already
                if ((*p_playerHand[j])->face == (Face)(i))
                {
                    //PlayerHighCards out of the seven
                    p_playerHighFive[4] = p_playerHand[j];
                    p_playerHighFive[3] = p_playerHand[j - 1];
                    p_playerHighFive[2] = p_playerHand[j - 2];
                    p_playerHighFive[1] = p_playerHand[j - 3];
                    p_playerHighFive[0] = p_playerHand[j - 4];
                    return true;
                }
            }
    }
}
return false;
}


Sorry about the shoddy formatting at the bottom but its only curly braces and they are fine in my code.

I know how to check for A,2,3,4,5 straight I just havnt coded yet, I am just really stumped on how to move past one of the pairs that I was on about. Its not just pairs, this occurs with 3 of a kind as well(so if theres 3 of a kind in the straight) or more than one pair for that matter so 2 pairs. Doesnt happen outside of this because theres not physically enough cards to be concerned about it.

Sorry if my code is not that elegant and if there's a better way then let me know. I havnt learned templates or the stl yet as I have only just started the language so a solution not using these would be great.

I tried to be as descriptive as possible but, im new to this so if you have any questions just ask :)

Thanks in advance, Paul
Can't you just copy the hand to a temporary and remove duplicates there, then push from that temporary into p_playerHighFive instead of directly from p_playerHand?
Last edited on
closed account (SETp4iN6)
Fu*k why didn't I think of that lol
closed account (SETp4iN6)
Ill post back with the results.
closed account (SETp4iN6)
went with this instead
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
for (int i = FACES; i >= 1; i--)
	{
		if ((_checkNumFaces[i - 1] >= 1) && (_checkNumFaces[i - 2] >= 1) &&
                    (_checkNumFaces[i - 3] >= 1) && (_checkNumFaces[i - 4] >= 1) &&
                    (_checkNumFaces[i - 5] >= 1))
		{
			for (int j = 6; j >= 0; j--)
			{
				short k = 4;
				short l = 0;
				if ((*p_playerHand[j])->face == (Face)(i))
				{
					p_playerHighFive[k] = p_playerHand[j - l];
					k--;
					l++;

					while (k >= 0)
					{
						if ((*p_playerHighFive[k + 1])->face == (*p_playerHand[j - l])->face)
						{
							l++;
							continue;
						}

						p_playerHighFive[k] = p_playerHand[j - l];
						k--;
						l++;

					}
					
					return true;
				}
			}
		}
	}
	return false;
Last edited on
Topic archived. No new replies allowed.