Advertisement

poker game

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

OK, I've done some basic testing, and uploaded the latest version of the code. It's pretty much done.

sounds good I am also going to study bitmaps

Advertisement

Might want to check the two pair fail and the swapped KING and QUEEN defines.
Full house also fails.
Straight also fails when initialized out of order and ACE to 5 fails.

Dev careful. Pixel on board.

thx for the update

Thank you for the tips. I am working on the fixes now. I will test again tomorrow. Edit: I think that I fixed everything that you mentioned.

https://github.com/sjhalayka/poker/blob/main/main.cpp

When you're done with that you may try to break mine.

https://github.com/MarkKughler/Poker

Dev careful. Pixel on board.

Advertisement

There is one major thing that is still missing — one needs to be able to judge a hand’s worth based on how many wild card slots there are in the hand (wild card count = 5 - cards in hand count), and which cards have not been laid out face up on the table or face down in your hand.

Not an easy task. It was the hardest task when I wrote my poker game. It was not a brute force algorithm that I used… I just don’t remember how I coded it. :(

Something like this:


bool is_possible_royal_flush(const vector<card>& sorted_hand, const vector<card>& remaining_unflipped_cards)
{
	const size_t num_wildcards = MAX_NUM_CARDS_PER_HAND - sorted_hand.size();

	map<short unsigned int, size_t> value_counts;
	map<short unsigned int, size_t> suit_counts;

	for (size_t i = 0; i < sorted_hand.size(); i++)
	{
		value_counts[sorted_hand[i].value]++;
		suit_counts[sorted_hand[i].suit]++;
	}

	// Is there only one suit?
	if (suit_counts.size() != 1)
		return false;

	// Are all card values distinct?
	if (value_counts.size() != sorted_hand.size())
		return false;

	short unsigned int the_suit = suit_counts.begin()->first;

	bool found_ace = false;
	bool found_king = false;
	bool found_queen = false;
	bool found_jack = false;
	bool found_10 = false;

	for (map<short unsigned int, size_t>::const_iterator ci = value_counts.begin(); ci != value_counts.end(); ci++)
	{
		if (ci->first == ACE)
			found_ace = true;
		else if (ci->first == KING)
			found_king = true;
		else if (ci->first == QUEEN)
			found_queen = true;
		else if (ci->first == JACK)
			found_jack = true;
		else if (ci->first == 10)
			found_10 = true;
	}

	size_t num_wildcards_left = num_wildcards;

	if (false == found_ace && num_wildcards_left > 0)
	{
		card c;
		c.suit = the_suit;
		c.value = ACE;

		found_ace = is_card_in_unflipped_cards(c, remaining_unflipped_cards);

		num_wildcards_left--;
	}

	if (false == found_king && num_wildcards_left > 0)
	{
		card c;
		c.suit = the_suit;
		c.value = KING;

		found_king = is_card_in_unflipped_cards(c, remaining_unflipped_cards);

		num_wildcards_left--;
	}

	if (false == found_queen && num_wildcards_left > 0)
	{
		card c;
		c.suit = the_suit;
		c.value = QUEEN;

		found_queen = is_card_in_unflipped_cards(c, remaining_unflipped_cards);

		num_wildcards_left--;
	}

	if (false == found_jack && num_wildcards_left > 0)
	{
		card c;
		c.suit = the_suit;
		c.value = JACK;

		found_jack = is_card_in_unflipped_cards(c, remaining_unflipped_cards);

		num_wildcards_left--;
	}

	if (false == found_10 && num_wildcards_left > 0)
	{
		card c;
		c.suit = the_suit;
		c.value = 10;

		found_10 = is_card_in_unflipped_cards(c, remaining_unflipped_cards);

		num_wildcards_left--;
	}

	if (found_ace == false ||
		found_king == false ||
		found_queen == false ||
		found_jack == false ||
		found_10 == false)
	{
		return false;
	}

	return true;
}

Seems long winded.

How about we just toss in three jokers into the deck and try a solution. Would that satisfy the requirement for this exercise?

Dev careful. Pixel on board.

I unrolled the loops. That doesn’t help, I suppose.

Advertisement