Python写一个程序,实现发牌、比大小判断输赢。 #### 游戏规则: 一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。 有以下几种牌: 豹子:三张一样的牌,如3张6. 顺金:又称同花顺,即3张同样花色的顺子, 如红桃 5、6、7 顺子:又称拖拉机,花色不同,但是顺子,如红桃5、方片6、黑桃7,组成的顺子 对子:2张牌一样 单张:单张最大的是A 这几种牌的大小顺序为, **豹子>顺金>顺子>对子>单张** #### 需程序实现的点: 1. 先生成一付完整的扑克牌 2. 给5个玩家随机发牌 3. 统一开牌,比大小,输出赢家是谁
时间: 2024-03-11 13:46:43 浏览: 156
好的,以下是实现该游戏规则的Python程序:
```python
import random
# 生成一副扑克牌
cards = []
for i in range(2, 15):
for j in ['♥', '♠', '♦', '♣']:
cards.append(str(i) + j)
cards.remove('11♣')
cards.remove('11♥')
cards.remove('11♦')
cards.remove('11♠')
cards.remove('12♣')
cards.remove('12♥')
cards.remove('12♦')
cards.remove('12♠')
cards.remove('13♣')
cards.remove('13♥')
cards.remove('13♦')
cards.remove('13♠')
cards.remove('14♣')
cards.remove('14♥')
cards.remove('14♦')
cards.remove('14♠')
# 定义比大小函数
def compare(cards):
nums = [int(card[:-1]) for card in cards] # 取出牌面数字
suits = [card[-1] for card in cards] # 取出花色
if len(set(nums)) == 1: # 豹子
return 1, nums[0]
if len(set(suits)) == 1: # 同花顺
if max(nums) - min(nums) == 2:
return 2, max(nums)
if len(set(nums)) == 3: # 顺子、三张不同的牌
nums.sort()
if nums[2] - nums[0] == 2:
return 3, nums[2]
if len(set(nums)) == 2: # 对子、三张牌中有两张一样
nums.sort()
if nums[0] == nums[1] or nums[1] == nums[2]:
return 4, nums[1]
# 单张
return 5, max(nums)
# 发牌
players = []
for i in range(5):
player_cards = random.sample(cards, 3)
players.append(player_cards)
for card in player_cards:
cards.remove(card)
# 开牌
winner = 0
for i, player in enumerate(players):
print("Player", i+1, ":", player)
result = compare(player)
if result[0] < compare(players[winner])[0] or (result[0] == compare(players[winner])[0] and result[1] > compare(players[winner])[1]):
winner = i
print("Winner is Player", winner+1)
```
该程序先生成了一副完整的扑克牌,然后随机给5个玩家发牌,并比较大小,输出赢家是谁。
阅读全文