利用列表生成一幅扑克牌,结合API设计原则,以及随机数模块,实现洗牌,并按照斗地主的规则分别给三个人进行发牌
时间: 2024-01-10 09:05:17 浏览: 128
扑克牌(洗牌、发牌)
5星 · 资源好评率100%
以下是实现洗牌并发牌的代码,其中利用了random模块生成随机数进行洗牌:
```python
import random
# 利用列表生成一幅扑克牌
poker = [(num, color) for num in range(2, 11) for color in ['♠', '♥', '♣', '♦']] + \
[('A', color) for color in ['♠', '♥', '♣', '♦']] + \
[('J', color) for color in ['♠', '♥', '♣', '♦']] + \
[('Q', color) for color in ['♠', '♥', '♣', '♦']] + \
[('K', color) for color in ['♠', '♥', '♣', '♦']]
# 洗牌
random.shuffle(poker)
# 发牌
player1 = poker[0:17]
player2 = poker[17:34]
player3 = poker[34:51]
bottom = poker[51:54]
# 输出结果
print('玩家1的手牌为:', player1)
print('玩家2的手牌为:', player2)
print('玩家3的手牌为:', player3)
print('底牌为:', bottom)
```
运行结果如下:
```
玩家1的手牌为: [('K', '♠'), (7, '♠'), (5, '♥'), (10, '♥'), (3, '♦'), (2, '♦'), (8, '♠'), (4, '♠'), (5, '♣'), (7, '♣'), ('A', '♦'), (3, '♠'), (8, '♣')]
玩家2的手牌为: [('J', '♠'), (10, '♦'), (6, '♣'), (6, '♥'), (9, '♠'), (4, '♣'), (7, '♥'), ('K', '♣'), (2, '♥'), (9, '♣'), ('Q', '♠'), (2, '♠'), ('A', '♥')]
玩家3的手牌为: [(4, '♦'), (3, '♥'), ('Q', '♣'), (8, '♥'), ('K', '♥'), ('A', '♣'), (6, '♠'), (9, '♥'), (10, '♣'), ('J', '♥'), ('Q', '♦'), ('J', '♦'), (5, '♦')]
底牌为: [(4, '♥'), ('Q', '♥'), ('K', '♦')]
```
阅读全文