如何在玩家之间随机洗牌? [英] How to randomly shuffle a deck of cards among players?
问题描述
我在使用另一种功能来发卡时遇到麻烦.到目前为止,这就是我所拥有的.
I am having trouble using one function in another to deal cards. Here is what I have so far.
import random as rand
def create():
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suites = ['H', 'C', 'D', 'S']
deck = [[r + s] for s in suites for r in ranks]
return deck
def cards_dealt (num_cards, num_players, deck):
rand.shuffle(deck)
print(cards_dealt(5, 3, deck))
我了解该功能不完整.我需要num_cards作为每个玩家收到的纸牌数量,num_players是玩家数量,而deck是功能create()的纸牌字符串列表. 例如,打印语句将显示从洗牌后的牌组中获得的三张纸牌中的每张纸牌有5张.我的问题是,每当我写东西时,它都说未定义牌组.
I understand that the function is incomplete. I need num_cards to be the number of cards that each player receives, num_players to be the number of players, and deck to be the list of card strings from the function create(). For example, the print statement would reveal the 5 cards each of the three players gets from the list deck that has been shuffled. My problem is that whenever I write something, it says that deck is not defined.
推荐答案
让我提出一种面向对象的方法,在该方法中,我们将定义类Card
,Deck
和Player
.
Let me suggest an object-oriented approach where we will define the classes Card
, Deck
and Player
.
使用对象而不是纸牌列表将提供一个简洁的API来实现游戏.当您实施游戏逻辑时,也将使保持单个真相(关于每张牌的位置以及哪位玩家拥有每张牌)的难度变得更容易.
Using objects instead of lists of cards will provide a neat API to implement games. As you implement game logic, it will also make it easier to keep a single source of truth as to where each card is and which player has each card.
import random
class Card:
def __init__(self, kind, rank, deck):
self._kind = kind
self._rank = rank
self.deck = deck
self.where = None
def __repr__(self):
return 'Card(kind={}, rank={}'.format(self.kind, self.rank)
@property
def kind(self):
return self._kind
@property
def rank(self):
return self._rank
class Deck:
def __init__(self):
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
kinds = ['H', 'C', 'D', 'S']
self.cards = [Card(kind, rank, self) for kind in kinds for rank in ranks]
def deal(self, players, n=None):
if any(p.deck is not self for p in players):
raise RuntimeError('Player {} is not playing the deck'.format(p.id))
n = n if n is not None else len(self.cards)
random.shuffle(self.cards)
for i, card in enumerate(self.cards[:n * len(players)]):
card.where = players[i % len(players)]
class Player:
def __init__(self, id, deck):
self.id = id
self.deck = deck
@property
def hand(self):
return [card for card in deck.cards if card.where is self]
交易卡
deck = Deck()
players = [Player(id, deck) for id in range(3)]
deck.deal(players, n=4)
for p in players:
print(p.hand)
输出
[Card(kind=D, rank=A), Card(kind=D, rank=2), Card(kind=S, rank=5), Card(kind=S, rank=K)]
[Card(kind=S, rank=9), Card(kind=D, rank=5), Card(kind=C, rank=A), Card(kind=C, rank=Q)]
[Card(kind=C, rank=9), Card(kind=S, rank=J), Card(kind=D, rank=3), Card(kind=H, rank=9)]
玩纸牌
card.where
属性可以更新以指示卡的位置.由于它是卡位置的单个事实来源,因此会更新player.hand
属性.
Playing a card
The card.where
attribute can be updated to indicate the position of a card. Since it is the single source of truth for card position, this updates the player.hand
property.
deck = Deck()
players = [Player(id, deck) for id in range(2)]
deck.deal(players, n=1)
players[0].hand[0].where = players[1]
for p in players:
print(p.hand)
输出
[]
[Card(kind=H, rank=K), Card(kind=D, rank=2)]
更多功能
以上API提供了处理卡片和手动移动卡片的基础知识,但可以扩展以实现新功能.
More features
The above API provides the basics to deal cards and move cards from hand to hand, but can be extended to implement new features.
这篇关于如何在玩家之间随机洗牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!