June
June

Reputation: 1

What is optimal algorithm of "random card distribution (Poker game)"?

What is the optimal algorithm of "random card distribution (Poker game)"?

I'd like to build a program that can distribute playing cards evenly with respect to their numbers and suits to the 4 players in a Poker game. In other words, all 4 players should have a straight or straight flush at the same turn. But, how do we build the algorithm for that?

This is how I think it can be approached:

  1. Set i = suits (e.g., 1 for heart 2 for spades) j = number (ace = 1, king =13)
  2. Make a matrix deck (Like [H1, H2, ... H13]...[S1, S2, ... S13])
  3. Randomly choose the first card (random i1 and j1), save to player 1's deck card = deck[i1, j1] player1 = np.append(card)
  4. Randomly choose second card (random i2 and j2)
  5. If i2 = i1, rechoose i, if not, save the card to player 2 deck
  6. Continue until 4th card
  7. For 5th turn, if i5 = i1 and j5 = j1+1 or j1-1, save the card to player 1 deck. if not rechoose j or i.
  8. Continue until 8th card
  9. For the 9th turn, if i9 = i1 and j9 = j1+1 or j1-1 or j5+1 or j5-1, save the card to player 1 deck. if no rechoose j or i

However, this algorithm is time-consuming, because I need to introduce a new way to check every 4 turns whether it has a close number of previous cards that the player already has. Is there any way to make this simpler, such as to check the card having a close number of previous cards for making a straight flush?

Upvotes: 0

Views: 644

Answers (1)

Kuba
Kuba

Reputation: 3056

The way to approach is the same as if you wanted to deal cards in real life. Here is an example in python

Build a deck of cards:

import itertools
deck = ["".join(c) for c in itertools.product("AKQJT98765432", "cdhs")]
# ['Ac', 'Ad', 'Ah', 'As', 'Kc', 'Kd', ....

Shuffle the deck

import random
random.shuffle(deck)
# ['3c', '5c', '7h', 'Qs', 'Ts', '8s', 'Jd', ...

Deal cards to 4 players

players = []
for i in range(4):
    cards = [deck.pop(), deck.pop()]
    players.append(cards)
#  [['Td', 'Jd'], ['7c', '5c'], ['Js', 'Ks'], ['6c', '8d']]

Upvotes: 0

Related Questions