游戏规则:一付扑克牌,去掉大小王,每个玩家发3张牌,最后比大小,看谁赢。单张最大的是A,牌的大小顺序为,豹子>顺金>顺子>对子>单张,用python写函数,返回一个扑克牌列表,里面有52项,每一项都是一个tuple。 例如:[(“♥”,“2”), (“♣”,“2”), (“♠”,”K”), (“♦”,“A”),………………….] poke_types = ["♥", "♠", "♣", "♦"] poke_nums = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"] 写一函数,判定三张扑克牌是否是同花? 写一函数,判定三张扑克牌是否是豹子? 写一函数,判定三张扑克牌是否是同花顺?
时间: 2023-08-02 22:07:44 浏览: 234
下面是三个函数的代码:
```python
poke_types = ["♥", "♠", "♣", "♦"]
poke_nums = [2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A"]
def is_same_suit(poke_list):
"""
判断三张扑克牌是否是同花
"""
suits = set([poke[0] for poke in poke_list])
return len(suits) == 1
def is_three_of_a_kind(poke_list):
"""
判断三张扑克牌是否是豹子
"""
nums = [poke_nums.index(poke[1]) for poke in poke_list]
return len(set(nums)) == 1
def is_straight_flush(poke_list):
"""
判断三张扑克牌是否是同花顺
"""
if not is_same_suit(poke_list):
return False
nums = [poke_nums.index(poke[1]) for poke in poke_list]
nums.sort()
if nums == [0, 1, 12]:
# 特殊情况,A234是最小的顺子
return True
for i in range(2, len(nums)):
if nums[i] - nums[i-1] != 1:
return False
return True
```
使用示例:
```python
poke_list = [("♥", "2"), ("♣", "2"), ("♠", "K")]
print(is_same_suit(poke_list)) # False
print(is_three_of_a_kind(poke_list)) # False
print(is_straight_flush(poke_list)) # False
poke_list = [("♥", "A"), ("♥", "A"), ("♥", "A")]
print(is_same_suit(poke_list)) # True
print(is_three_of_a_kind(poke_list)) # True
print(is_straight_flush(poke_list)) # False
poke_list = [("♦", "10"), ("♦", "J"), ("♦", "Q")]
print(is_same_suit(poke_list)) # True
print(is_three_of_a_kind(poke_list)) # False
print(is_straight_flush(poke_list)) # True
```
阅读全文