写段python 十三张牌带王,王可以变,判断是否有四个三张一样的牌
时间: 2023-07-15 13:13:44 浏览: 62
下面是使用 Python 编写的十三张牌带王,王可以变,判断是否有四个三张一样的牌的代码:
```python
# 定义牌组
cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', 'BlackJoker', 'RedJoker']
# 定义牌组点数
card_values = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14, 'BlackJoker': 15, 'RedJoker': 16}
# 生成所有可能的五张牌的组合
from itertools import combinations_with_replacement
combinations = list(combinations_with_replacement(cards, 5))
# 遍历每一种组合
for combination in combinations:
# 定义牌组点数列表
values = []
for card in combination:
# 如果是王牌,则将其点数设置为 0
if card == 'BlackJoker' or card == 'RedJoker':
values.append(0)
else:
values.append(card_values[card])
# 对牌组点数列表进行排序
values.sort()
# 定义计数器
count = 1
# 遍历每一张牌
for i in range(1, len(values)):
# 如果当前牌与前一张牌点数相同,则计数器加 1
if values[i] == values[i-1]:
count += 1
# 否则,如果计数器大于等于 3,则说明有三张牌一样,将计数器清零
else:
if count >= 3:
count = 0
break
count = 1
# 如果计数器大于等于 3,则说明有四个三张一样的牌
if count >= 3:
print(combination)
```
这段代码会输出所有有四个三张一样的牌的组合,如果没有任何输出,则说明没有符合条件的牌组。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)