我想要打一场纸牌游戏。我坚持的是处理卡片。我所做的就是用每张卡片制作一张单词并给它一个值,因为有些东西比其他卡片更有价值。我想到的是将字典分为4个部分,或者每个字典制作4个副本,然后从每个字典中删除39张卡片(每个人留下13张卡片)。这甚至可能还是我以错误的方式解决这个问题?
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
(对不起,如果代码错了,这是我的第一篇文章,谢谢)
答案 0 :(得分:7)
听起来像是一个使用课程的好机会!我会这样做:
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
答案 1 :(得分:6)
我对Python中的字典函数不太熟悉,但我要做的是使用卡片对象和 set 列表与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
我希望能回答你的问题(因为这是你的第一个问题,这是我的第一个答案)
编辑:不推荐使用Oops集。改为使用内置集。
Edit2:并且set.pop()并不是真正随机的,它从进一步阅读中出现,只是随意的。男孩脸红了。
答案 2 :(得分:1)
您可以选择使用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...
或者以您需要的任何更复杂的方式。 (但是,请注意,没有必要循环通过手或类似的东西;因为它已经是随机的,简单的切片就足够了。)
答案 3 :(得分:0)
您可以使用more_itertools
库在{n}个玩家中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')]
输出
>>> 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]]
看起来玩家1有2对。
这个工具在做什么?
more_itertools.distribute
平均分配来自n个子组中的可迭代项目。
来自the docs的修改示例:
more_itertools
sessionid
是第三方软件包,其中包含itertools recipes和其他许多useful tools。
答案 4 :(得分:0)
我也有类似的问题,在这里检查。我必须创建一个程序,该程序创建并洗牌,然后发牌。我已经检查了所有示例,并且Benjamin here提供的示例结构合理,为我提供了继续的基础。
我在这里分享练习的完整解决方案:
from random import shuffle
class Cards:
def __init__(self):
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
suites = ['♥', '♠', '♣', '♦']
self.deck = [j + i for j in ranks for i in suites]
self.deal = []
def shuffle(self):
shuffle(self.deck)
def deal_out(self,num_cards, num_players):
deal = [[0 for x in range(num_cards)] for y in range(num_players)]
for i in range(num_cards):
for k in range(num_players):
deal[k][i]=self.deck.pop()
self.deal = deal
#declare number of players and cards to be dealt
num_cards = int(input("Please enter a number of cards to be dealt: "))
num_players = int(input("Please enter a number of players: "))
#create new game with new deck
first_game = Cards()
print('\nDeck:\n')
print(first_game.deck)
#shuffle cards
first_game.shuffle()
print('\nShuffled Deck:\n')
print(first_game.deck)
#deal cards
first_game.deal_out(num_cards, num_players)
#Print the dealt cards
print('\nDealt cards:\n')
print(first_game.deal)
#Print cards left after the deal
print('\nCards left in the deck after the deal:\n')
print(first_game.deck)
Please enter a number of cards to be dealt: 4
Please enter a number of players: 5
Deck:
['A♥', 'A♠', 'A♣', 'A♦', '2♥', '2♠', '2♣', '2♦', '3♥', '3♠', '3♣', '3♦', '4♥', '4♠', '4♣', '4♦', '5♥', '5♠', '5♣', '5♦', '6♥', '6♠', '6♣', '6♦', '7♥', '7♠', '7♣', '7♦', '8♥', '8♠', '8♣', '8♦', '9♥', '9♠', '9♣', '9♦', '10♥', '10♠', '10♣', '10♦', 'J♥', 'J♠', 'J♣', 'J♦', 'Q♥', 'Q♠', 'Q♣', 'Q♦', 'K♥', 'K♠', 'K♣', 'K♦']
Shuffled Deck:
['6♥', '5♠', '6♣', '8♦', '10♠', '2♣', '4♦', '6♦', 'J♦', '3♥', '8♥', 'K♥', '10♦', 'A♦', '10♣', '2♥', '3♣', '4♣', '10♥', '8♠', '9♦', '3♦', 'A♣', '9♥', 'Q♦', '5♥', 'Q♠', '9♣', 'K♦', '7♥', 'J♥', '7♠', '2♦', '7♣', 'A♠', 'J♣', '4♠', '5♣', '9♠', '7♦', '2♠', 'Q♥', 'J♠', 'A♥', '4♥', '5♦', 'K♠', '8♣', '3♠', 'Q♣', '6♠', 'K♣']
Dealt cards:
[['K♣', 'K♠', 'Q♥', '4♠'], ['6♠', '5♦', '2♠', 'J♣'], ['Q♣', '4♥', '7♦', 'A♠'], ['3♠', 'A♥', '9♠', '7♣'], ['8♣', 'J♠', '5♣', '2♦']]
Cards left in the deck after the deal:
['6♥', '5♠', '6♣', '8♦', '10♠', '2♣', '4♦', '6♦', 'J♦', '3♥', '8♥', 'K♥', '10♦', 'A♦', '10♣', '2♥', '3♣', '4♣', '10♥', '8♠', '9♦', '3♦', 'A♣', '9♥', 'Q♦', '5♥', 'Q♠', '9♣', 'K♦', '7♥', 'J♥', '7♠']