那么我们要实现的功能,就是以下几个:
suit_cards =['黑桃','红桃','方块','梅花']# 花色
check_number =['2','3','4','5','6','7','8','9','10','J','Q','K','A']# 点数# 嵌套循环生成扑克牌# poker_list = [f"{suit}{check}" for suit in suit_cards for check in check_number]
poker_list =[]for suit in suit_cards:for check in check_number:
poker_list.append(f"{suit}{check}")
random.shuffle(poker_list)# 洗牌
功能:
random.shuffle()
对扑克牌列表进行随机洗牌,确保发牌的随机性。poker_list
,其实集合set
也可以,不过集合有随机性,还是喜欢都在掌握中的感觉,而且列表方便管理。player =['player1','player2','player3','player4','player5']# 玩家列表# player_poker = {p: [] for p in player} # 初始化每个玩家的手牌为空列表(字典推导式)
player_poker ={}# 初始化一个空字典for p in player:# 遍历每个玩家
player_poker[p]=[]# 为每个玩家赋值一个空列表,表示其手牌,player_poker是个字典,键是用户,列表是值(也就是玩家手牌)# 确保牌的数量足够分发iflen(poker_list)<len(player)*3:raise ValueError("牌堆中牌的数量不足以分发给所有玩家!")# 每人发三张牌for i inrange(3):# 每人发3轮for p in player:# 嵌套循环,player中有5个,一共循环15次
poker = poker_list.pop()# 从牌堆顶部发一张牌
player_poker[p].append(poker)
功能:
player_poker
中,结构如下:{'player1':['黑桃K','方块Q','红桃A'],'player2':['梅花7','黑桃8','红桃10'],...}
for player, pokers in player_poker.items():# items是字典的方法,返回一个可迭代对象,每个元素是一个键值对print(f"{player}的牌是:{', '.join(pokers)}")
功能:
', '.join(['黑桃A', '红桃K', '方块Q']) # 结果: '黑桃A, 红桃K, 方块Q'
输出示例:
player1的牌是:黑桃K, 方块Q, 红桃A
player2的牌是:梅花7, 黑桃8, 红桃10
...
point_rank ={point: i for i, point inenumerate(check_number, start=2)}# 字典推导式(i,point)对
这里解释一下:point是键,i是值, 可以去看一下字典推导式的结构组成
功能:
point_rank
:定义点数的大小,数值越大点数越高。例如,“A” 的优先级最大,值为 14。{'2':2,'3':3,...,'K':13,'A':14}
defpoker_type(cards):#这个cards是列表,也就是前面键值对中的值
points =sorted([card[2:]for card in cards], key=lambda x: point_rank[x])# 提取点数并按大小排序
suits =[card[:2]for card in cards]# 提取花色(列表推导式),索引前两个字符# 判断是否为豹子if points[0]== points[1]== points[2]:return'豹子', points
# 判断是否为同花顺
is_straight =(point_rank[points[2]]- point_rank[points[1]]==1and \
point_rank[points[1]]- point_rank[points[0]]==1)or points ==['2','3','A']iflen(set(suits))==1and is_straight:# len(set(suit))==1判断一个列表或字符串中的所有元素是否相同return'同花顺', points
# 判断是否为顺子if is_straight:return'顺子', points
# 判断是否为同花iflen(set(suits))==1:return'同花', points
# 判断是否为对子if points[0]== points[1]or points[1]== points[2]or points[0]== points[2]:return'对子', points
# 如果都不是,返回单张return'单张', points
功能:
根据玩家的三张牌,判断牌型并返回 牌型名称 和 点数列表。
hand_rankings =['单张','对子','顺子','同花','同花顺','豹子']
功能:
defcustom_key(item):
hand_rank = hand_rankings.index(item[1][0])
point_ranks =sorted([point_rank[point]for point in item[1][1]], reverse=True)if hand_rank ==1:
second_card_rank = point_ranks[1]
first_card_rank = point_ranks[0]return(hand_rank, second_card_rank, first_card_rank)return(hand_rank, point_ranks)# 计算每个玩家的牌型
player_hands ={player: poker_type(cards)for player, cards in player_poker.items()}# 找出最大牌
winner =max(player_hands.items(),key=custom_key)
player_hands 被假设为一个字典,其中的值是含有两个元素的元组,例如:
player_hands = {
“player1”: (“豹子”, [“A”, “A”, “A”]),
“player2”: (“顺子”, [“K”, “Q”, “J”]),
}
功能:
print("\n牌局结果:")for player,(hand_type, points)in player_hands.items():print(f"{player} 的牌型: {hand_type} ({', '.join(points)})")print(f"赢家是: {winner[0]},牌型: {winner[1][0]} ({', '.join(winner[1][1])})")
功能:
输出示例:
player1 的牌型: 顺子 (10, J, Q)
player2 的牌型: 对子 (8, 8, K)
player3 的牌型: 单张 (7, 9, A)
player4 的牌型: 同花 (4, 6, J)
player5 的牌型: 豹子 (K, K, K)
赢家是: player5,牌型: 豹子 (K, K, K)
import random
# 定义扑克牌花色和点数
suit_cards = ['黑桃', '红桃', '方块', '梅花']
check_number = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
# 生成扑克牌
poker_list = [f"{suit}{check}" for suit in suit_cards for check in check_number]
random.shuffle(poker_list) # 洗牌
# 玩家和玩家手牌
player = ['player1', 'player2', 'player3', 'player4', 'player5']
player_poker = {p: [] for p in player}
# 发牌,每人发三张
if len(poker_list) < len(player) * 3:
raise ValueError("牌堆中牌的数量不足以分发给所有玩家!")
for i in range(3):
for p in player:
poker = poker_list.pop()
player_poker[p].append(poker)
# 打印每个玩家的牌
for player, pokers in player_poker.items():
print(f"{player}的牌是:{', '.join(pokers)}")
# 定义点数优先级
point_rank = {point: i for i, point in enumerate(check_number, start=2)}
# 判断牌型
def poker_type(cards):
points = sorted([card[2:] for card in cards], key=lambda x: point_rank[x])
suits = [card[:2] for card in cards]
# 判断是否为豹子
if points[0] == points[1] == points[2]:
return '豹子', points
# 判断是否为同花顺
is_straight = (point_rank[points[2]] - point_rank[points[1]] == 1 and \
point_rank[points[1]] - point_rank[points[0]] == 1) or points == ['2', '3', 'A']
if len(set(suits)) == 1 and is_straight:
return '同花顺', points
# 判断是否为顺子
if is_straight:
return '顺子', points
# 判断是否为同花
if len(set(suits)) == 1:
return '同花', points
# 判断是否为对子
if points[0] == points[1] or points[1] == points[2] or points[0] == points[2]:
return '对子', points
# 否则为单张
return '单张', points
# 定义牌型优先级
hand_rankings = ['单张', '对子', '顺子', '同花', '同花顺', '豹子']
# 比较两手牌的大小
def custom_key(item):
hand_rank = hand_rankings.index(item[1][0])
point_ranks = sorted([point_rank[point] for point in item[1][1]], reverse=True)
if hand_rank == 1:
second_card_rank = point_ranks[1]
first_card_rank = point_ranks[0]
return (hand_rank, second_card_rank, first_card_rank)
return(hand_rank, point_ranks)
# 计算每个玩家的牌型
player_hands = {player: poker_type(cards) for player, cards in player_poker.items()}
winner = max(player_hands.items(),key=custom_key)
# 打印结果
print("\n牌局结果:")
for player, (hand_type, points) in player_hands.items():
print(f"{player} 的牌型: {hand_type} ({', '.join(points)})")
print(f"赢家是: {winner[0]},牌型: {winner[1][0]} ({', '.join(winner[1][1])})")
提示:请勿发布广告垃圾评论,否则封号处理!!