#Cards Module
#1.Card类
class Card():
PANKS=['A','2','3','4','5',
'6','7','8','9','10',
'J','Q','K',]
SUITS=['梅','方','红','黑']
def __init__(self,rank,suit,face_up=True):
self.rank=rank #指的是牌面的数字1-13
self.suit=suit #suit指的是花色
self.is_face_up=face_up #是否显示牌面正面,Ture为正面,False为背面
#b.重写print()方法
def __str__(self):
if self.is_face_up: #重新打印一张牌的信息
rep=self.suit+self.rank
else:
rep="xx"
return rep
def flip(self):
self.is_face_up = not self.is_face_up
def pic_orde(self):
if self.rank=='A':
FaceNum=1
elif self.rank=='J':
FaceNum=11
elif self.rank=='Q':
FaceNum=12
elif self.rank=='K':
FaceNum=13
else:
FaceNum=int(salf.rank)
if self.suit=='梅':
Suit=1
elif self.suit=='方':
Suit=2
elif self.suit=='红':
Suit=3
else:
suit=4
return (Suit - 1) * 13 + FaceNum
#2.Hand类
class Hand():
#a.构造函数即初始化
""" A hand of playing cards. """
def __init__(self):
self.cards = []
#b.重写print()方法
def __str__(self):
if self.cards:
rep = ""
for card in self.cards:
rep += str(card) + "\t"
else:
rep = "无牌"
return rep
#c.清空牌手手里的牌
def clear(self):
self.cards = []
#d:增加牌手手里的牌
def add(self, card):
self.cards.append(card)
#e:把一张牌给别的牌手
def give(self,card, other_hand):
self.cards.remove(card)
other_hand.add(card)
#3.Poke类:
class Poke(Hand):
#a.#生成一副牌
""" A deck of playing cards. """
def populate(self):
for suit in Card.SUITS:
for rank in Card.RANKS:
self_add(Card(rank, suit))
#.洗牌
def shuffle(self): #洗牌
import random #导入随机库
random.shuffle(self.cards) #打乱牌的顺序
#c.发牌,发给玩家,没人默认13张牌
def deal(self, hands, per_hand = 13):
for rounds in range(per_hand):
for hand in hands:
if self.cards:
top_card = self.card[0]
self.cards.remove(top_card)
hand.add(top_card)
#self.give(top_card, hand)
else:
print("can't continue deal. out of cards!")