目录
1、 用Card类表示一张牌
# 一张牌的抽象
class Card():
"""A playing card"""
RANKS = ['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
self.suit = suit
self.is_face_up = face_up
def __str__(self):
if self.is_face_up:
rep = self.suit + self.rank
else:
rep = "XX"
return rep
def pic_oder(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(self.rank)
if self.suit == '梅':
suit_num = 1
elif self.suit == '方':
suit_num = 2
elif self.suit == '红':
suit_num = 3
else:
suit_num = 4
return (suit_num -1) *13 + FaceNum
def flip(self): # 翻牌方法
self.is_face_up = not self.is_face_up
申明的类属性:RANKS 和SUITS 是两个列表,分别代表牌的点数和花色。牌的点数和花色是确定的,点数 A,2,3,4,5,6,7,8,9,10,J,Q,K一共13种,花色只有梅花、红桃、方片、黑桃四种。故而,列表的内容是可以确定的。
构造函数:def __init__(self, rank, suit, face_up=True) ,用来实例化一个对象时初始化设置私有属性。一张牌的属性有3个:点数、花色和是否翻面。注意:face_up这个参数的缺省值是True,这样调用构造函数初始化时可以不传递face_up的值,使用默认值True。
下面这种写法好奇怪,是什么意思?下划线代表什么?
def __init__(self, rank, suit, face_up=True):
self.rank = rank
self.suit = suit
self.is_face_up = face_up
初次接触Python确实有点别扭,函数定义方式与java、c++等面向对象的编程语言略有不同,形式上有差别,根本上还是一致的,你依旧能找到函数名、参数列表和函数体。多写几次就习惯了,刚开始感觉别扭是正常的。
python的函数定义如果函数名前后都有"__",注意是两个下划线,表示这是私有方法。他不能通过对象名直接调用。python中定义函数以def开始,紧跟函数名,然后是参数列表,参数列表后面是":",这个冒号代替了其他编程语言中的{ }。
2、 用Hand表示一个牌手(玩家)
class Hand():
"""A hand of palying cards"""
def __init__(self):
self.cards = []
def __str__(self):
if self.cards:
rep = ''
for card in self.cards:
rep += str(card) + "\t"
else:
rep = "无牌"
return rep
def clear(self):
self.cards = []
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand): # 把一张牌给其他牌手
self.cards.reve(card)
other_hand.add(card)
对一个牌手来说,必定要定义一个空的列表用来存放手里的cards。
方法:1、add---增加牌;
2、clear---清空手中的牌;
3、give---把手中的牌交给其他牌手。
3、用poke类表示一幅牌,可以看做拥有全部54张牌的牌手
# from Hand import Hand
# from Card import Card
# Poke 类
from pokers.Card import Card
from pokers.Hand import Hand
class Poke(Hand):
"""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) # 打乱牌的顺序
def deal(self, hands, per_hand):
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)
else:
print('不能继续发牌了,牌已经发完了!')
Poke类是对一幅牌的抽象,它可以被看做是拥有全部54张的牌手,所以继承了Hand类。
4、发牌主程序
# 主程序
from pokers.Hand import Hand
from pokers.Poke import Poke
if __name__ == "__main__":
print('This is a module with classes for playing cards')
# 4个玩家
players = [Hand(), Hand(), Hand(), Hand()]
poke1 = Poke()
poke1.populate()
poke1.shuffle()
poke1.deal(players, 13)
# 显示4个牌手的牌
n = 1
for hand in players:
print("牌手 " , n ,end=':')
print(hand)
n = n+1
input("\nPress the enter ket to exit")
5、测试结果
This is a module with classes for playing cards
牌手 1:方A 方10 红J 红5 方3 红9 黑7 梅Q 黑K 梅2 梅3 梅J 方7
牌手 2:黑6 梅A 方2 梅6 梅9 梅K 红3 方6 黑5 红6 黑Q 红2 方4
牌手 3:梅4 方5 红K 黑2 红8 红A 梅5 红10 方Q 梅8 黑J 梅10 黑8
牌手 4:红7 黑A 黑4 黑10 黑9 红Q 方J 方8 梅7 红4 方9 黑3 方K