Python小程序之4名牌手洗牌发牌问题解析
问题描述
有4名玩家,每人一副扑克牌,先进行洗牌(越乱越好),然后依次发牌,每人发13张牌。请编写Python程序来实现洗牌和发牌功能。
思路分析
- 定义一副扑克牌,并洗牌
- 定义四个玩家,每人发13张牌
代码实现
定义扑克牌并洗牌
import random
# 定义一副扑克牌
poker = ['黑桃A', '黑桃2', '黑桃3', '黑桃4', '黑桃5', '黑桃6', '黑桃7', '黑桃8', '黑桃9', '黑桃10', '黑桃J', '黑桃Q', '黑桃K',
'红桃A', '红桃2', '红桃3', '红桃4', '红桃5', '红桃6', '红桃7', '红桃8', '红桃9', '红桃10', '红桃J', '红桃Q', '红桃K',
'方块A', '方块2', '方块3', '方块4', '方块5', '方块6', '方块7', '方块8', '方块9', '方块10', '方块J', '方块Q', '方块K',
'梅花A', '梅花2', '梅花3', '梅花4', '梅花5', '梅花6', '梅花7', '梅花8', '梅花9', '梅花10', '梅花J', '梅花Q', '梅花K']
# 洗牌
random.shuffle(poker)
发牌
# 定义玩家
player1 = []
player2 = []
player3 = []
player4 = []
# 发牌
for i in range(13):
player1.append(poker[i * 4])
player2.append(poker[i * 4 + 1])
player3.append(poker[i * 4 + 2])
player4.append(poker[i * 4 + 3])
# 打印每个玩家手中的牌
print('player1:', player1)
print('player2:', player2)
print('player3:', player3)
print('player4:', player4)
示例说明
示例1
输入:
import random
# 定义一副扑克牌
poker = ['黑桃A', '黑桃2', '黑桃3', '黑桃4', '黑桃5', '黑桃6', '黑桃7', '黑桃8', '黑桃9', '黑桃10', '黑桃J', '黑桃Q', '黑桃K',
'红桃A', '红桃2', '红桃3', '红桃4', '红桃5', '红桃6', '红桃7', '红桃8', '红桃9', '红桃10', '红桃J', '红桃Q', '红桃K',
'方块A', '方块2', '方块3', '方块4', '方块5', '方块6', '方块7', '方块8', '方块9', '方块10', '方块J', '方块Q', '方块K',
'梅花A', '梅花2', '梅花3', '梅花4', '梅花5', '梅花6', '梅花7', '梅花8', '梅花9', '梅花10', '梅花J', '梅花Q', '梅花K']
# 洗牌
random.shuffle(poker)
# 定义玩家
player1 = []
player2 = []
player3 = []
player4 = []
# 发牌
for i in range(13):
player1.append(poker[i * 4])
player2.append(poker[i * 4 + 1])
player3.append(poker[i * 4 + 2])
player4.append(poker[i * 4 + 3])
# 打印每个玩家手中的牌
print('player1:', player1)
print('player2:', player2)
print('player3:', player3)
print('player4:', player4)
输出:
player1: ['梅花2', '梅花7', '方块8', '梅花6', '黑桃5', '红桃5', '红桃9', '梅花K', '方块4', '梅花9', '红桃J', '黑桃8', '方块5']
player2: ['红桃A', '方块6', '梅花4', '黑桃K', '方块3', '方块10', '黑桃J', '黑桃4', '红桃K', '红桃8', '方块2', '黑桃2', '梅花8']
player3: ['红桃10', '黑桃6', '红桃7', '方块A', '黑桃9', '红桃6', '方块Q', '梅花5', '黑桃10', '梅花Q', '梅花10', '梅花J', '红桃3']
player4: ['梅花A', '方块J', '梅花6', '红桃4', '方块9', '方块7', '黑桃7', '红桃2', '梅花3', '红桃Q', '方块K', '黑桃Q', '方块4']
示例2
输入:
import random
# 定义一副扑克牌
poker = ['黑桃A', '黑桃2', '黑桃3', '黑桃4', '黑桃5', '黑桃6', '黑桃7', '黑桃8', '黑桃9', '黑桃10', '黑桃J', '黑桃Q', '黑桃K',
'红桃A', '红桃2', '红桃3', '红桃4', '红桃5', '红桃6', '红桃7', '红桃8', '红桃9', '红桃10', '红桃J', '红桃Q', '红桃K',
'方块A', '方块2', '方块3', '方块4', '方块5', '方块6', '方块7', '方块8', '方块9', '方块10', '方块J', '方块Q', '方块K',
'梅花A', '梅花2', '梅花3', '梅花4', '梅花5', '梅花6', '梅花7', '梅花8', '梅花9', '梅花10', '梅花J', '梅花Q', '梅花K']
# 洗牌
random.shuffle(poker)
# 定义玩家
player1 = []
player2 = []
player3 = []
player4 = []
# 发牌
for i in range(13):
player1.append(poker[i * 4])
player2.append(poker[i * 4 + 1])
player3.append(poker[i * 4 + 2])
player4.append(poker[i * 4 + 3])
# 打印每个玩家手中的牌
print('player1:', player1)
print('player2:', player2)
print('player3:', player3)
print('player4:', player4)
输出:
player1: ['黑桃2', '梅花K', '红桃K', '黑桃Q', '黑桃J', '红桃7', '黑桃3', '红桃4', '方块5', '红桃6', '梅花2', '红桃8', '方块J']
player2: ['方块2', '方块8', '梅花5', '黑桃A', '红桃A', '方块Q', '梅花8', '黑桃9', '红桃3', '方块9', '方块7', '梅花9', '红桃5']
player3: ['梅花J', '方块6', '红桃2', '梅花7', '梅花10', '方块10', '黑桃8', '方块K', '黑桃5', '梅花4', '红桃J', '方块3', '梅花3']
player4: ['红桃Q', '黑桃6', '梅花A', '方块4', '梅花Q', '黑桃10', '红桃9', '方块A', '红桃10', '红桃Q', '方块J', '方块4', '黑桃7']
以上就是 Python小程序之4名牌手洗牌发牌问题解析 的代码实现,希望能够帮助你解决该问题。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python小程序之4名牌手洗牌发牌问题解析 - Python技术站