"""
模拟斗地主发牌, 一副牌(54张), 三个玩家 + 3张底牌, 最终看牌即可.
最终打印结果如下:
玩家A: ♦3, ♣4, ♥6... # 按照牌的大小升序排列.
玩家B: ......
玩家C: ......
底牌: ......
提示:
花色:['♠️', '♥️', '♦️', '♣️']
点数: 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A, 2
额外两张: 小王, 大王 ['', '']
要求:
1. 买牌(造牌)
2. 洗牌.
3. 发牌.
4. 看牌.
尽可能用 函数 做.
"""
import random
import time
# 随机置换.
# list1 = [i for i in range(1, 11)]
# random.shuffle(list1)
# print(list1)
# 1. 买牌.
# 牌盒, 键: 索引, 值: 具体的牌. 规则: 牌越小, 索引越小.
# pokers = {0:'♠️3', 1:'♥️3', 2:'♦️3', 3:'♣️3', 4:'♠️4', 5:'♥️4', 6:'♦️4', 7:'♣️4', 8:'♠️5', 9:'♥️5', 10:'♦️5', 11:'♣️5', 52:'', 53:''}
# 每个牌的编号
# poker_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 52, 53]
pokers = {}
poker_list = []
def buy_pokers():
color_list = ['♠️', '♥️', '♦️', '♣️']
number_list = ['3','4','5','6','7','8','9','10', 'J', 'Q', 'K', 'A', '2']
# 循环嵌套, 遍历, 组合牌. 最后手动处理 大小王.
tmp_list = []
for i in number_list:
for j in color_list:
tmp_list.append(j + i)
tmp_list.append('')
tmp_list.append('')
# print(tmp_list)
# 给 字典 pokers 和 列表 poker_list 赋值
for index,value in enumerate(tmp_list):
pokers[index] = value
# print(pokers)
# 2. 洗牌, 洗的是: poker_list, 牌的编号.
def shuffle_poker_list():
for i in range(0,54):
poker_list.append(i)
random.shuffle(poker_list)
# print(poker_list)
# 3. 发牌
play_A, play_B, play_C,dipai = [], [], [],[] # 3个玩家 + 底牌
def send_pokers():
# 规则: 最后三张是底牌, 剩下的牌 根据索引和3取余, 余0 => 发给玩家1, 余1 => 发给玩家2, 余2 => 发给玩家3.
# 这里: 发给每个玩家的 其实是 牌的编号, 不是具体的牌.
i = 0
while i <= 50:
if i % 3 == 0:
play_A.append(poker_list[i])
elif i % 3 == 1:
play_B.append(poker_list[i])
elif i % 3 == 2:
play_C.append(poker_list[i])
i += 1
dipai.append(poker_list[51])
dipai.append(poker_list[52])
dipai.append(poker_list[53])
# 4. 看牌.
def look_pokers(player):
# 先对玩家手中牌的编号, 升序排列.
# 根据排序后的牌的编号, 去牌盒 pokers(字典) 中找具体的牌, 打印即可.
player.sort()
show_list = []
for i in player:
show_list.append(pokers[i])
#延时看牌
for i in show_list:
time.sleep(0.1)
print(i,end='')
print()
def doudizhu():
# 买牌
buy_pokers()
# 洗牌
shuffle_poker_list()
# 发牌
send_pokers()
# 看牌
menu()
while True:
play = input('选择1-3看玩家牌,选择4看底牌,选择5退出:')
if play == '1':
look_pokers(play_A)
elif play == '2':
look_pokers(play_B)
elif play == '3':
look_pokers(play_C)
elif play == '4':
look_pokers(dipai)
elif play == '5':
break
#菜单
def menu():
print('-'*30)
print('1.play_A')
print('2.play_B')
print('3.play_C')
print('4.dipai')
print('5.退出')
# 5. 调用函数.
if __name__ == '__main__':
doudizhu()