Advertisement

poker game

Started by November 01, 2024 12:29 AM
158 comments, last by pbivens67 1 hour, 29 minutes ago

should I use a switch statement or a if statement to deal cards to the dealer here is my shuffling code.

			int shuffled_state[52];
			vector<int> indices =
			{ 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,       // an ordered int container so we have some data to manipulate
			   30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
			   40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
			   50, 51, 52
			};

			unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // set up 
			default_random_engine rng(seed);                                        // simular to srand(0)
			shuffle(indices.begin(), indices.end(), rng);                           // standard library container shuffle (entire deck is mixed up in one line of code)
			for (int i = 0; i < 52; ++i)
			{
				shuffled_state[i]=indices[i];            // demonstrating copy vector content to regular array (optional)
				
			
			}

Try both, and see what you like best.

Advertisement

Agreed. At this point, it hardly matters since we're only talking about 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.

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
};                                                        |     };

Dev careful. Pixel on board.

I have decided to use switch statements they seem a little easier

I am having trouble following this thread is says poker but some code looks like its for blackjack.

Welcome to the club. I noticed that also, after the fact. The difference is almost meaningless.

How's your design coming Phil? Did you get the custom slider figured out?

Dev careful. Pixel on board.

Advertisement

I scrapped the custom slider idea, and I am working on a poker game instead. yes, they are similar designs.

here is my poker game so far

I want to print out the value of each hand but I am unsure of how to begin , I am able to print text to the screen. ,

Does this help? (for a hand)
Or are you asking for ideas of giving a value per card? (we'll have to see your current source if this is the case)

ref: https://gamedev.stackexchange.com/questions/49302/determining-poker-hands

#include <iostream>

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; // deal with much later in discussion 
    hand.rank = 0;

    for (int i = 0; i < 5; 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,10,3,3}, 0, 0 };
    
    rankHand(player_hand);
    if (player_hand.rank == 0)
    {
        // todo: straight
        // todo: flush
        // todo: straight flush
        // todo: royal straight flush
    }

    std::cout << player_hand.rank;
    return 0;
}

Dev careful. Pixel on board.

Advertisement