Advertisement

poker game

Started by November 01, 2024 12:29 AM
174 comments, last by pbivens67 6 hours, 5 minutes ago

you have got a point; boy this poker game is difficult to figure out, once I figure out this hand evaluation algorithm it should become easier to code.

I am going stick with this project until I figure it out, I can do this, wish me all the best and skill.

Advertisement

thanks for all the likes, I must be on the right track.

Is your code on GitHub?

no not yet

NubDevice said:

Agreed. At this point, it hardly matters since we're only talking about two/five cards each during the deal.
From your shuffled_state array, keep track of an index and give everyone in turn the next. So, one may also argue neither in the context of the question asked.
For example, this gets the job done: (note: bounds checking removed for clarity)

player_hand.push_back(deck.shuffled_state[deck.next_index++]);
dealer_hand.push_back(deck.shuffled_state[deck.next_index++]);
player_hand.push_back(deck.shuffled_state[deck.next_index++]);
dealer_hand.push_back(deck.shuffled_state[deck.next_index++]);
.
.
.

Right now would be a good time for pencil and paper design to help decide.
The problem space is more than just who's turn it is or whom the next card goes to.
Considering a single round of play, your program needs to transverse a number of states.
States I'll usually switch through because it feels organized. Speed and other stupid internet advice is meaningless.
These are what I came up with to perhaps jump start your own design.
Play gambling games at a casino site with the best gambling reputation.

enum class eGameState (blackjack)                         |     enum class eGameState (poker)
{                                                         |     {
    idle,                                                 |        idle,
    shuffle,                    <------ you are here      |        ante,
    wager,                                                |        animate_ante,
    animate_wager_chips,                                  |        shuffle,
    deal_hand,                  not quite here yet        |        deal,
    focus_player,                                         |        raise, (chip animate)
    player_bust,                                          |        card(s) exchange,
    player_21,                                            |        raise, (chip animate)
    animate_player_end,                                   |        call,
    focus_dealer,                                         |        run hand comparison against rule set,
    clear_table                                           |        pay/take/clear_table
};                                                        |     };

As for me the second code is better, but the first one is good too

Advertisement

I am still working on my poker game, I am trying to get my code to print out text “one pair” for one pair and “two pair” for two pair, I am getting it to print out “one pair” and “two pair” for two pairs but I only want it to print out “two pair” for two pair. It does work correctly for printing out “one pair” for one pair, I know there is a simple solution to this problem but I am just not getting it. I have worked with this code a lot.

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int> numbers = { 1, 2, 3, 2, 3 };

    // Find two pairs
    int pairCount = 0;
    for (int i = 0; i < numbers.size(); i++) {
        for (int j = i + 1; j < numbers.size(); j++) {
            if (numbers[i] == numbers[j]) {
                pairCount++;
                if (pairCount == 2) {
                    cout << "Two Pairs" << endl;
                    break;
                }
                if (pairCount==1) {
                    cout << "One Pair" << endl;
                    break;
                }
            }
        }
    }

    return 0;
}

good.
Count it out in your head or on paper fully might help.
But in this case as shown, what's wrong is you are trying to print before the work is done.

Let both the loops completely finish counting before you print.
Understand that one pair will increment the pairCount twice because it finds it on the ‘i’ loop and reverse on the ‘j’ loop.

With this arrangement, you will automatically find one-pair, two-pair, three-of-kind, four-of-kind AND full house. (neat right?)

one pair will equal 2
two pair will equal 4
three of kind will equal 6
full house will equal 8
four of kind will equal 12

When you realize that, you'll be halfway to the finish line with the evaluation part. 🙂


Dev careful. Pixel on board.

well, here is some code that works with the above arrangement listed I just have to integrate it into my game code.

#include <iostream>

using namespace std;

struct HandInfo
{
    int cards[5];                 // valid values : 2 => 14 mapping to (2,3,4,5,6,7,8,9,10,J,Q,K,A)
    int highest_card;             // used on zero ranking
    int rank;
};

void rankHand(HandInfo& hand)
{
    //   solveable by comparison   |   determine by additional steps                    
    // ============================|=================================
    // nothing        =  0         |         (highest card)
    // one pair       =  2         |
    // two pair       =  4         |
    // three of kind  =  6         |
    //                             |         (straight)
    //                             |         (flush)                        
    // full house     =  8         |
    // four of a kind = 12         |
    //                             |         (straight flush)
    //                             |         (royal straight flush)

    hand.highest_card = 0;
    hand.rank = 0;

    for (int i = 0; i < 5; i++)
    {
        if (hand.cards[i] > hand.highest_card) hand.highest_card = hand.cards[i];

        for (int j = 0; j < 5; j++)
            if (i != j && (hand.cards[j] == hand.cards[i]))
                hand.rank++;
    }
}


int main()
{
    HandInfo player_hand = { {2,2,3,3,6}, 0, 0 };

    rankHand(player_hand);
    if (player_hand.rank == 0)
    {
        // todo: straight
        // todo: flush
        // todo: straight flush
        // todo: royal straight flush
    }

    cout << player_hand.rank;
    return 0;
}

I made a poker game one time.

It turned out that it was easiest to detect first Royal Flush, and if not, then detect Straight Flush… and all the way down to High Card. You seem to be doing it in reverse. Just my two cents.

You'll also have to use what's left uncovered to fill in wild card spots when detecting hand rank. This is useful for the so-called AI. (e.g. possibly a Royal Flush, etc.).

Advertisement