Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubles making a python program to deal cards.

Tags:

python

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)

like image 996
Gnar Avatar asked Dec 04 '22 19:12

Gnar


1 Answers

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
like image 102
Benjamin Avatar answered Dec 30 '22 11:12

Benjamin