这是使用python完成的德州扑克游戏。包括了洗牌,发牌,验证牌型。
# 德州扑克
# 设计一个程序:
# (1)洗牌功能
# (2)发牌功能
# (3)验证手牌
# 皇家同花顺(同一个花色五张连牌 A K Q J 10)
# 同花顺(同一个花色 7 8 9 10 J)
# 四条 (x x x x + y)
# 满堂红(x x x + y y)
# 同花 (5张同一花色)
# 顺子 (不同花色组成的顺子)
# 三条 (x x x + y z)
# 两对 (x x y y + z)
# 一对 (x x y z c)
# 普通牌
# 52张牌, 包含4种花色(红心, 黑心, 方块, 梅花)
# 每种花色13张(A 2 3 4 5 6 7 8 9 10 J Q K) 1 2 3 4 5 6 7 8 9 10 11 12 13
import random
class D:
dict={10:'同花顺',}
class Card:
def __init__(self):
#一张卡牌是由花色加点数构成的
self.__color = ''
self.__value = 1
#属性,不是方法。
@property
def color(self):
return self.__color
@color.setter
def color(self,value):
if isinstance(value,str):#判断value是不是字符串
self.__color = value
@property
def value(self):
return self.__value
@value.setter
def value(self,value):
if isinstance(value,int) and (value <1 and value >= 14):
self.__value = 1
else:
self.__value = value
def __str__(self):
#重写该方法的目的是为了输出对象的时候输出我们想要的信息
#输出扑克的花色和点数而不是地址
#红桃1 红桃11 红桃12 红桃13
#构造特殊点数的映射关系
special_point = {1:'A',11:'J',12:'Q',13:'K'}
#判断特殊点数
if self.value in special_point:
#替换
str_value = special_point[self.__value]
else:
str_value = str(self.__value)
return self.color + str_value + " "
class Poke:
def __init__(self):
"""初始化一副扑克"""
#花色
poke_color = ['♥','♠','♦','♣']
#点数
poke_point = [value for value in range (1,14)]
#存储牌
self.poke = []
#定义索引
index=0
#构造扑克
for i in range(4):
for j in range(13):
#1.创建card
card = Card()
#2.将生成的card装入扑克
self.poke.append(card)
#3.设置card的花色和点数
self.poke[index].color = poke_color[i]
self.poke[index].value = poke_point[j]
#4.索引递增
index += 1
def show(self,poke):
""""显示所有的扑克"""
index = 0
for card in poke:
if index % len(poke) == 0:
print("")
print(card,end=' ')
index += 1
print("")
def shuffle(self):
#洗牌
random.shuffle(self.poke)
def deal_poke(self,start=0,end=5,step=1):
#发牌
return self.poke[start:end:step]
class Player:
""""玩家类"""
def __init__(self,player_id,player_name,player_money):
self.player_id=player_id
self.player_name=player_name
self.player_money=player_money
#玩家获取的手牌
self.hand_card = []
#是否为同一花色
self.is_samecolor = False
#是否为顺子
self.is_straight = False
def hand_poke(self,card):
#玩家持有的手牌
self.hand_card = card
def judge_poke(self):
#验证牌型
#1.将card拆分(花色和点数分别拆出来)
hand_colors = [x.color for x in self.hand_card]
hand_values = [x.value for x in self.hand_card]
#[方块 红桃 梅花 梅花 黑桃]=>set [6 Q J 4 2]
hand_colors_set = set(hand_colors)
hand_values_set = set(hand_values)
if len(hand_colors_set) == 1:
#判断是否为同一花色
self.is_samecolor = True
if len(hand_values_set) == 5 and max(hand_values_set) - min(hand_values_set) == 4:
#判断是否为顺子
self.is_straight = True
if self.is_straight and self.is_samecolor:
print("同花顺")
return 10
elif self.is_samecolor:
print("同花")
elif self.is_straight:
print("顺子")
elif len(hand_values_set) == 5:
print("基础牌")
elif len(hand_values_set) == 4:
print("一对子")
else:
#四条 满堂红 三条 两对
#四条:4 4 4 4 5 =>(4,5) =>[4,1] 4 4 4 3 3=>(4,3) =>[3,2]
#不能根据长度来判断
counts = [hand_values.count(x) for x in hand_values_set ]
if max(counts) == 4:
print("四条")
elif max(counts) == 3 and len(counts)==2:
print("满堂红")
elif max(counts) == 3:
print("三条")
else:
print("两对")
if __name__ == "__main__":
#1.创建poke对象
p = Poke()
#2.输出所有扑克 or 洗牌
p.shuffle()
#3.发牌
hand_card = p.deal_poke()
#4.显示手牌情况
p.show(hand_card)
#5.验证牌型
#6.创建玩家
player = Player('001','lucy','100000')
player.hand_poke(hand_card)
player.judge_poke()
#p.show_all()