Problems making python map program.

I am tying a card game. I'm stuck on cards. What I did was to make a voice recorder with each card and give it meaning, because some of them cost more than others. I mean to divide the dictionary into 4 parts or make 4 copies of each dictionary, and then remove 39 cards from each of them (leaving 13 cards for each person). Is this possible, or am I going to do it wrong?

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 incorrect, this is my first post, thanks)

+3
source share
4 answers

I am not very versed in dictionary functions in Python, but what I would do is use map objects and set shuffled lists.

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        

, ( , , )

: . .

Edit2: set.pop() , , . - .

+6

, ! :

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
+6

- python random.shuffle. ; :

>>> 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']]

, Fisher-Yates shuffle. .

, , , :

>>> hand1 = cards[0:13]
>>> hand2 = cards[13:26]
# ...and so on...

, . ( , , - , , .)

+1

distribute n , more_itertools.

import itertools as it

import more_itertools as mit


# Build a Deck
suits = "β™₯♠♣♦"
ranks = list(range(2, 11)) + list("JQKA")
cards = list(it.product(suits, ranks))
print("Number of cards:", len(cards))
# Out: Number of cards: 52

# Shuffle and Distribute
players = 5
random.shuffle(cards)
hands = [list(hand) for hand in list(mit.distribute(players, cards))]
hands[0]                                               # player 1

[('β™₯', 'A'),
('β™₯', 6),
('♦', 9),
('β™ ', 'A'),
('β™₯', 7),
('β™ ', 8),
('♣', 10),
('♦', 'K'),
('β™₯', 4),
('β™ ', 4),
('β™ ', 'Q')]

, 1 2 .


?

more_itertools.distribute n .

:

>>> 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 , itertools recipes .

0

All Articles