python写一个程序,实现发牌、比大小判断输赢。 #### 游戏规则: 一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。 有以下几种牌: 豹子:三张一样的牌,如3张6. 顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7 顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子 对子:2张牌一样 单张:单张最大的是A 这几种牌的大小顺序为, **豹子>顺金>顺子>对子>单张** #### 需程序实现的点: 1. 先生成一付完整的扑克牌 2. 给5个玩家随机发牌 3. 统一开牌,比大小,输出赢家是谁
时间: 2024-03-09 11:47:29 浏览: 142
Python 扑克牌小游戏 十点半 (学堂在线-Python程序设计基础) 任务1-10
好的,以下是实现发牌、比大小判断输赢的 Python 代码:
```python
import random
# 生成一副完整的扑克牌
poker = []
for i in range(1, 14):
for j in ['♠', '♥', '♣', '♦']:
if i == 1:
poker.append('A' + j)
elif i == 11:
poker.append('J' + j)
elif i == 12:
poker.append('Q' + j)
elif i == 13:
poker.append('K' + j)
else:
poker.append(str(i) + j)
# 定义比较大小的函数
def compare(cards):
if cards[0][0] == cards[1][0] == cards[2][0]:
return '豹子'
elif cards[0][1] == cards[1][1] == cards[2][1]:
nums = [int(cards[0][0]), int(cards[1][0]), int(cards[2][0])]
nums.sort()
if nums[2] - nums[1] == 1 and nums[1] - nums[0] == 1:
return '顺金'
else:
return '同花'
else:
nums = [int(cards[0][0]), int(cards[1][0]), int(cards[2][0])]
nums.sort()
if nums[2] - nums[1] == 1 and nums[1] - nums[0] == 1:
return '顺子'
elif nums[0] == nums[1] or nums[1] == nums[2]:
return '对子'
else:
return '单张'
# 随机给5个玩家发牌
players = []
for i in range(5):
player = []
for j in range(3):
card = random.choice(poker)
poker.remove(card)
player.append(card)
players.append(player)
# 输出每个玩家的牌
for i in range(5):
print('玩家', i+1, '的牌是:', players[i])
# 比较大小,输出赢家
winner = 1
max_card = players[0][:]
for i in range(1, 5):
result1 = compare(max_card)
result2 = compare(players[i])
if result2 == '豹子':
winner = i+1
break
elif result2 == '顺金' and result1 != '豹子':
winner = i+1
max_card = players[i][:]
elif result2 == '顺子' and result1 != '豹子' and result1 != '顺金':
winner = i+1
max_card = players[i][:]
elif result2 == '对子' and result1 != '豹子' and result1 != '顺金' and result1 != '顺子':
winner = i+1
max_card = players[i][:]
elif result2 == '单张' and result1 != '豹子' and result1 != '顺金' and result1 != '顺子' and result1 != '对子':
nums1 = [int(max_card[0][0]), int(max_card[1][0]), int(max_card[2][0])]
nums2 = [int(players[i][0][0]), int(players[i][1][0]), int(players[i][2][0])]
nums1.sort()
nums2.sort()
if nums2[2] > nums1[2]:
winner = i+1
max_card = players[i][:]
print('赢家是玩家', winner, ',其牌是:', max_card)
```
注意:为了方便比较大小,我们在牌面前加上了数字,如'A♠'表示A,'K♠'表示K,以此类推。输出结果中也是带有数字的牌面,如'玩家 1 的牌是: ['A♠', 'J♠', '6♦']'。
阅读全文