I'm tying to make a card game. What I'm stuck on is dealing the cards. What I've done is make a dict with each card and given it a value because some are worth more than others. What I have in mind is dividing the dictionary into 4 parts, or make 4 copies of each dictionary and then delete 39 cards from each of them (leaving 13 cards for each person). Is this even possible or am I going about this in the wrong way?
from random import randint deck = {} def makeDeck(deck): suit = ['Club', 'Spade', 'Heart', 'Diamond'] whichSuit = 0 whichNum = 2 count = 1 while count != 52: if whichNum == 11: whichNum = 'Jack' if whichNum == 12: whichNum = 'Queen' if whichNum == 13: whichNum = 'King' if whichNum == 14: whichNum = 'Ace' deck[str(whichNum)+' '+suit[whichSuit]] = count count += 1 if whichNum == 'Jack': whichNum = 11 if whichNum == 'Queen': whichNum = 12 if whichNum == 'King': whichNum = 13 if whichNum == 'Ace': whichNum = 14 whichNum += 1 if count == 13 or count == 26 or count == 39: whichSuit += 1 whichNum = 2 def dealCards(deck): me = deck comp1 = deck comp2 = deck comp2 = deck
(Sorry if the code is wrong, this is my first post, Thanks)
Sounds like a great occasion to use classes! I would do it like this:
from random import shuffle class Cards: def __init__(self): values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] suites = ['H', 'S', 'C', 'D'] self.deck = [j + i for j in values for i in suites] def shuffle(self): shuffle(self.deck) def deal(self, n_players): self.hands = [self.deck[i::n_players] for i in range(0, n_players)] c = Cards() print c.deck c.shuffle() print c.deck c.deal(4) print c.hands
I'm not very experienced with the dictionary functions in Python but what I would do is use card objects and set lists with shuffle.
from random import shuffle class Card: def __init__(self,suit,num): self.suit = suit self.num = num deck = list() suits = ['Diamond', 'Heart', 'Spade', 'Club'] nums = ['2','3','4','5','6','7','8','9','10','J','Q','K','A'] for suit in suits: #This is the code that actually makes a deck for num in nums: deck.append(Card(suit,num)) shuffle(deck) for number in range(13): for player in range(4): #deal cards here using deck.pop() print(deck.pop().num) #just to prove it works randomly =P
I hope that answers your question (because like this is your first question this is my first answer)
Edit: Oops sets is deprecated. Using the built-in set instead.
Edit2: And set.pop() isn't truly random it appears from reading further, just arbitrary. Boy is my face red.
One option for you would be simply to use python's built-in function random.shuffle
. Don't bother with dictionaries; just create a list of cards and shuffle it whole:
>>> import random >>> ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'K', 'Q'] >>> suits = ['C', 'D', 'H', 'S'] >>> cards = [[rank, suit] for rank in ranks for suit in suits] >>> random.shuffle(cards) >>> cards [['J', 'S'], ['2', 'S'], ['3', 'S'], ['9', 'S'], ['9', 'D'], ['5', 'S'], ['8', 'H'], ['A', 'C'], ['4', 'D'], ['Q', 'H'], ['2', 'C'], ['Q', 'D'], ['7', 'H'], ['4', 'C'], ['7', 'S'], ['6', 'C'], ['K', 'H'], ['6', 'S'], ['9', 'C'], ['9', 'H'], ['A', 'H'], ['J', 'C'], ['2', 'D'], ['J', 'H'], ['3', 'H'], ['4', 'H'], ['8', 'C'], ['Q', 'S'], ['10', 'S'], ['A', 'S'], ['K', 'S'], ['5', 'D'], ['10', 'D'], ['8', 'D'], ['7', 'C'], ['5', 'C'], ['Q', 'C'], ['3', 'D'], ['8', 'S'], ['6', 'H'], ['A', 'D'], ['2', 'H'], ['6', 'D'], ['K', 'D'], ['10', 'C'], ['5', 'H'], ['4', 'S'], ['K', 'C'], ['7', 'D'], ['10', 'H'], ['3', 'C'], ['J', 'D']]
If you need to roll your own, consider the Fisher-Yates shuffle. It's super simple.
At the risk of stating the dreadfully obvious, once you have a shuffled list, you can simply deal it by slicing it like so:
>>> hand1 = cards[0:13] >>> hand2 = cards[13:26] # ...and so on...
Or in whatever more complicated way you need. (However, note that there's no need to cycle through the hands or anything like that; since it's already random, simple slicing will suffice.)
You can distribute
cards among n players using the more_itertools
library.
Playing Cards
Output
It looks like player 1 has 2 pair.
What is this tool doing?
more_itertools.distribute
equally distributes items from an iterable among n sub-groups.
Modified example from the docs:
>>> n, iterable = 3, [1, 2, 3, 4, 5, 6, 7] >>> children = distribute(n, iterable) >>> [list(c) for c in children] [[1, 4, 7], [2, 5], [3, 6]]
more_itertools
is a third-party package comprising itertools recipes and many other useful tools.