作业一:扑克游戏11点

1、生成一副扑克牌(自己设计扑克牌的结构,小王和大王可以分别用14、15表示 )

2、3个玩家(玩家也可以自己定义)
3、发牌规则
默认先给用户发一张牌,其中 J、Q、K、小王、大王代表的值为0.5,其他就是则就是当前的牌面值。
用户根据自己的情况判断是否继续要牌。
要,则再给他发一张。(可以一直要牌,但是如果自己手中的牌总和超过11点,你的牌就爆掉了(牌面变成0))
不要,则开始给下个玩家发牌。(没有牌则则牌面默认是0)
如果用户手中的所有牌相加大于11,则表示爆了,此人的分数为0,并且自动开始给下个人发牌。

4、最终计算并获得每个玩家的分值

必备技术点:随机抽排
import random

total_poke_list = [("红桃", 1), ("黑桃", 2), ......,("大王", 15), ("小王", 14)]

# 随机生成一个数,当做索引。
index = random.randint(0, len(total_poke_list) - 1)
# 获取牌
print("抽到的牌为:", total_poke_list[index])
# 踢除这张牌
total_poke_list.pop(index)

print("抽完之后,剩下的牌为:", total_poke_list)
# 全套扑克牌

# 全套扑克牌
color_list = ["红桃", "黑桃", "方片", "梅花"]
color_list = ["红桃", "黑桃", "方片", "梅花"]
num_list = []
for num in range(1, 14):
    num_list.append(num)
total_poke_list = []
for color in color_list:
    for num in num_list:
        text = color,num
        total_poke_list.append(text)
total_poke_list.append(("小王",14))
total_poke_list.append(("大王",15))
print(total_poke_list)


# 按照顺序抽牌
# for name in user_list:
#     print(name)
#     y = True
#     while y:
#         choose = input("是否要牌:")
#         import random
#         index = random.randint(0, len(total_poke_list) - 1)
#         total = total_poke_list[index]     #抽到的卡牌为total
#         print("抽到的牌为:", total_poke_list[index])
#         total_poke_list.pop(index)
#         print("抽完之后,剩下的牌为:", total_poke_list)
#         if choose == "n":
#             print("此轮抽牌结束")
#             break



user_list = ["张三","李四","王五"]
result = {
    "张三":0,
    "李四":0,
    "王五":0
}
# total_poke_list = [("红桃", 1), ("黑桃", 2), ......,"大王”,“小王”]

import random
for name in user_list:
    print(name)

    index = random.randint(0, len(total_poke_list) - 1)
    total = total_poke_list[index]  # 抽到的卡牌为total
    num = int(total[1])
    print("抽到的牌为:", total_poke_list[index])
    total_poke_list.pop(index)
    print("抽完之后,剩下的牌为:", total_poke_list)

    # num1为当前的点数
    if num < 11:
        num1 = int(num)
    else:
        num1 = 0.5

    print("此张牌的点数为:")
    print(num1)

    scorse1 = num1
    scorse = 0 + scorse1


    while True:
        choose = input("是否继续要牌:")
        if choose == "n":
            print("此轮抽牌结束")
            print(input("按任意键查看当前点数情况"))
            result[name] = scorse
            print(result)
            break

        if  choose != "n":
            index = random.randint(0, len(total_poke_list) - 1)
            total = total_poke_list[index]  # 抽到的卡牌为total.
            print("抽到的牌为:", total_poke_list[index])
            total_poke_list.pop(index)
            print("抽完之后,剩下的牌为:", total_poke_list)
            num = int(total[1])
            if num < 11:
                num1 = int(num)
            else:
                num1 = 0.5

            scorse = scorse + num1
            print("此张牌的点数为:")
            print(num1)
            print("当前点数共:")
            print(scorse)
            if scorse > 11:
                scorse = 0
                print("总点数超过11,当前点数为0")
                print(scorse)
                break

        result[name] = scorse
        print(result)

猜你喜欢

转载自blog.csdn.net/weixin_46268244/article/details/131385869