python 发牌小游戏代码
时间: 2023-11-10 15:06:14 浏览: 89
以下是Python发牌小游戏的代码,其中包括Card类、Hand类和Poke类:
```python
import random
class Card:
def __init__(self, suit, face):
self.suit = suit
self.face = face
def __str__(self):
return self.suit + self.face
class Hand:
def __init__(self):
self.cards = []
def __str__(self):
if self.cards:
return " ".join(map(str, self.cards))
return ""
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)
class Poke:
def __init__(self):
self.cards = []
for suit in ["♠", "♣", "♥", "♦"]:
for face in range(1, 14):
card = Card(suit, str(face))
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards)
def deal(self, hands, per_hand=13):
for rounds in range(per_hand):
for hand in hands:
if self.cards:
top_card = self.cards[0]
self.cards.remove(top_card)
hand.add(top_card)
if __name__ == "__main__":
poke = Poke()
poke.shuffle()
hand1 = Hand()
hand2 = Hand()
hand3 = Hand()
hand4 = Hand()
hands = [hand1, hand2, hand3, hand4]
poke.deal(hands)
for i, hand in enumerate(hands):
print("牌手{}的牌:{}".format(i+1, hand))
```
阅读全文