Advertisement

poker game

Started by November 01, 2024 12:29 AM
171 comments, last by taby 2 hours, 8 minutes ago

If you're having too much trouble, then try my code. There are no pointers.

#define JACK 11
#define QUEEN 12
#define KING 13
#define ACE 14

// ...

// HAND MUST BE SORTED BEFORE CALLING THIS FUNCTION
bool is_straight(const vector<card>& sorted_hand)
{
	map<short unsigned int, size_t> value_counts;

	for (size_t i = 0; i < MAX_NUM_CARDS_PER_HAND; i++)
		value_counts[sorted_hand[i].value]++;

	// Are all 5 cards distinct?
	if (value_counts.size() == MAX_NUM_CARDS_PER_HAND)
	{
		// Handle two special cases 
		// where the 5th card value is equal to ACE
		if (sorted_hand[4].value == ACE)
		{
			if (sorted_hand[0].value == 2 &&
				sorted_hand[1].value == 3 &&
				sorted_hand[2].value == 4 &&
				sorted_hand[3].value == 5)
				return true;
			else if (sorted_hand[0].value == 10 &&
				sorted_hand[1].value == JACK &&
				sorted_hand[2].value == QUEEN &&
				sorted_hand[3].value == KING)
				return true;
		}
		else
		{
			// Handle general case where 5th card is not an ACE
			if (sorted_hand[4].value == sorted_hand[0].value + 4)
				return true;
		}
	}

	return false;
}

@taby when I comment out the above line of I get “hand” variable undefined

Advertisement

At this point, you need to upload your whole code, so that we can identify the problem.

Create a GitHub account, create a new repository, and then upload the file(s).

You're changing things unnecessarily, breaking the code, making us guess. Why would you do that? I am not a fan of your classification code, but at least Mark's example should compile (as far as I can tell).

before I do that, I want you to see that this snippet of code works but the text overwrites more text, I want the “straight” text to print out, but I don't want “high card” text to print out on top of it like it does I have a problem with my if/else if statements, yes I know I am using many if statements but at least the code compiles

			if (player_hand.rank == 0)
			{
				if (arr[0] == 1 && arr[1] == 2 && arr[2] == 3 && arr[3] == 4 && arr[4] == 5)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 2 && arr[1] == 3 && arr[2] == 4 && arr[3] == 5 && arr[4] == 6)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 3 && arr[1] == 4 && arr[2] == 5 && arr[3] == 6 && arr[4] == 7)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 4 && arr[1] == 5 && arr[2] == 6 && arr[3] == 7 && arr[4] == 8)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 5 && arr[1] == 6 && arr[2] == 7 && arr[3] == 8 && arr[4] == 9)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 6 && arr[1] == 7 && arr[2] == 8 && arr[3] == 9 && arr[4] == 10)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 7 && arr[1] == 8 && arr[2] == 9 && arr[3] == 10 && arr[4] == 11)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 8 && arr[1] == 9 && arr[2] == 10 && arr[3] == 11 && arr[4] == 12)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 9 && arr[1] == 10 && arr[2] == 11 && arr[3] == 12 && arr[4] == 13)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 10 && arr[1] == 11 && arr[2] == 12 && arr[3] == 13 && arr[4] == 1)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}

				if (flush_found == false)
				{
				surface_one = TTF_RenderText_Solid(font, text, textColor);
				SDL_BlitSurface(surface_one, NULL, gScreenSurface, &score_one);
				}
				else if (flush_found == true)
				{
	   	  		surface_thirteen = TTF_RenderText_Solid(font, text_six, textColor);
				SDL_BlitSurface(surface_thirteen, NULL, gScreenSurface, &score_one);
				}
			}

The whole code.

Are you stuck with GitHub?

Advertisement

can you look at my latest post and let me know how to resolve my if/else problem, I am sorry I am a little stuck,

Not knowing how this snippet fits into the whole and if forced to use it, I would re-write like this.
Without additional knowledge of your code, I can't help further.
But this solves part of your if logic.

bool straight_found = false;
if (rank == 0)
{
    if (arr[0] == 1 && arr[1] == 2 && arr[2] == 3 && arr[3] == 4 && arr[4] == 5)
        straight_found = true;
    if (arr[0] == 2 && arr[1] == 3 && arr[2] == 4 && arr[3] == 5 && arr[4] == 6)
        straight_found = true;
    if (arr[0] == 3 && arr[1] == 4 && arr[2] == 5 && arr[3] == 6 && arr[4] == 7)
        straight_found = true;
    if (arr[0] == 4 && arr[1] == 5 && arr[2] == 6 && arr[3] == 7 && arr[4] == 8)
        straight_found = true;
    if (arr[0] == 5 && arr[1] == 6 && arr[2] == 7 && arr[3] == 8 && arr[4] == 9)
        straight_found = true;
    if (arr[0] == 6 && arr[1] == 7 && arr[2] == 8 && arr[3] == 9 && arr[4] == 10)
        straight_found = true;
    if (arr[0] == 7 && arr[1] == 8 && arr[2] == 9 && arr[3] == 10 && arr[4] == 11)
        straight_found = true;
    if (arr[0] == 8 && arr[1] == 9 && arr[2] == 10 && arr[3] == 11 && arr[4] == 12)
        straight_found = true;
    if (arr[0] == 9 && arr[1] == 10 && arr[2] == 11 && arr[3] == 12 && arr[4] == 13)
        straight_found = true;
    if (arr[0] == 10 && arr[1] == 11 && arr[2] == 12 && arr[3] == 13 && arr[4] == 1)
        straight_found = true;

    if(straight_found == true)
    {
        surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
        SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
    }
    else if (flush_found == true) // changed boolean ordering mwk:20241224
    {
        surface_thirteen = TTF_RenderText_Solid(font, text_six, textColor);
        SDL_BlitSurface(surface_thirteen, NULL, gScreenSurface, &score_one);
    }
    else // <--- and basically "fixed" right here mwk:20241224
    {
        surface_one = TTF_RenderText_Solid(font, text, textColor);
        SDL_BlitSurface(surface_one, NULL, gScreenSurface, &score_one);
    }
}

Dev careful. Pixel on board.

I tried your code but I am still getting the same text overwrite problem the flush works fine

			bool flush_found = false;
			bitset<4> suit_bitset;
			for (int i = 0; i < 5; i++)
			{
				if (shuffled_state[i] < 14) suit_bitset.set(0);                            // clubs
				if (shuffled_state[i] > 13 && shuffled_state[i] < 27) suit_bitset.set(1);      // diamonds
				if (shuffled_state[i] > 26 && shuffled_state[i] < 40) suit_bitset.set(2);      // spades
				if (shuffled_state[i] > 39) suit_bitset.set(3);                            // hearts
			}
			if (suit_bitset.count() == 1) 
				flush_found = true;

				bool straight_found = false;
				if (player_hand.rank == 0)
				{
				if (arr[0] == 1 && arr[1] == 2 && arr[2] == 3 && arr[3] == 4 && arr[4] == 5)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 2 && arr[1] == 3 && arr[2] == 4 && arr[3] == 5 && arr[4] == 6)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 3 && arr[1] == 4 && arr[2] == 5 && arr[3] == 6 && arr[4] == 7)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 4 && arr[1] == 5 && arr[2] == 6 && arr[3] == 7 && arr[4] == 8)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 5 && arr[1] == 6 && arr[2] == 7 && arr[3] == 8 && arr[4] == 9)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 6 && arr[1] == 7 && arr[2] == 8 && arr[3] == 9 && arr[4] == 10)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 7 && arr[1] == 8 && arr[2] == 9 && arr[3] == 10 && arr[4] == 11)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 8 && arr[1] == 9 && arr[2] == 10 && arr[3] == 11 && arr[4] == 12)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 9 && arr[1] == 10 && arr[2] == 11 && arr[3] == 12 && arr[4] == 13)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}
				if (arr[0] == 10 && arr[1] == 11 && arr[2] == 12 && arr[3] == 13 && arr[4] == 1)
				{
					surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
					SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
				}

				if (straight_found == true)
				{
				surface_one = TTF_RenderText_Solid(font, text_seven, textColor);
				SDL_BlitSurface(surface_one, NULL, gScreenSurface, &score_one);
				}
				else if (flush_found == true)
				{
	   	  		surface_thirteen = TTF_RenderText_Solid(font, text_six, textColor);
				SDL_BlitSurface(surface_thirteen, NULL, gScreenSurface, &score_one);
				}
				else
				{
				surface_thirteen = TTF_RenderText_Solid(font, text, textColor);
				SDL_BlitSurface(surface_thirteen, NULL, gScreenSurface, &score_one);
				}

You missed the alteration in the first ten if statements.

Dev careful. Pixel on board.

wow it finally worked thanks for all the help

Advertisement