Reputation: 1
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:
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
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