CyberBit

Python+MySQL

扑克11点游戏(python代码)

import random

'''
纸牌11点游戏
规则:
1. 3个人参与,默认先给用户发一张牌,其中 J、Q、K、小王、大王代表的值为0.5,其他就是则就是当前的牌面值。
2. 随机顺序开始轮流抽牌,可以不抽,就得到点数
3. 超过11点爆炸出局,最接近11点的获胜

result={'player1':10,
        'player2':8,
        'player3':0}

'''

# 生成一副牌. The suits are called hearts, clubs, diamonds and spades.
poker_cards = []
for s in ['黑桃', '草花', '红心', '方片']:
    for n in range(1, 14):
        poker_cards.append((s, n))
poker_cards.append(('joker', 14))
poker_cards.append(('joker', 15))
# print(poker_cards)
# print(len(poker_cards))

# 生成玩家
players = []
play_num = int(input("几个人玩 ? "))
for i in range(play_num):
    players.append(input("玩家 {}:".format(i + 1)))
print(players)
result = {}

# 生成当前玩家过程
while players:
    now_player = players.pop(random.randint(0, len(players) - 1))
    now_pick = []
    while True:
        msg1 = "{player},你要抽牌还是不抽?(y/n)".format(player=now_player)
        choice = input(msg1)
        if choice.lower() == 'y':
            pick_card = poker_cards.pop(random.randint(0, len(poker_cards) - 1))
            now_pick.append(pick_card)
            if 1 <= pick_card[1] <= 10:
                card_value = pick_card[1]
            else:
                card_value = 0.5
            player_score = result.get(now_player, 0)
            player_score += card_value
            result[now_player] = player_score
            if player_score <= 11:
                msg2 = "{player},你抽到的牌是:{pick},现有牌组{cards},你的总分是 {score} .".format(player=now_player, pick=pick_card,
                                                                                   cards=now_pick,
                                                                                   score=player_score)
                print(msg2)
            else:
                player_score = 0
                result[now_player] = player_score
                msg3 = "{player},你抽到的牌是:{pick},现有牌组{cards},总分>11,你爆了,分数归0".format(player=now_player, pick=pick_card,
                                                                                  cards=now_pick, )
                print(msg3)
                break
        elif choice.lower() == 'n':
            player_score = result.get(now_player, 0)
            msg4 = "{player},你选择不抽牌,你的总分是 {score} . 等待结果吧".format(
                player=now_player, score=player_score)
            print(msg4)
            result[now_player] = player_score
            break
        else:
            print("输入错误,请重试。")

for k, v in result.items():
    msg5 = "{name} 的总分是 {score}".format(name=k, score=v)
    print(msg5)
# 结果演示:
#
# 几个人玩 ? 3
# 玩家 1:a
# 玩家 2:b
# 玩家 3:c
# ['a', 'b', 'c']
# c,你要抽牌还是不抽?(y/n)y
# c,你抽到的牌是:('草花', 12),现有牌组[('草花', 12)],你的总分是 0.5 .
# c,你要抽牌还是不抽?(y/n)y
# c,你抽到的牌是:('红心', 8),现有牌组[('草花', 12), ('红心', 8)],你的总分是 8.5 .
# c,你要抽牌还是不抽?(y/n)n
# c,你选择不抽牌,你的总分是 8.5 . 等待结果吧
# b,你要抽牌还是不抽?(y/n)y
# b,你抽到的牌是:('黑桃', 7),现有牌组[('黑桃', 7)],你的总分是 7 .
# b,你要抽牌还是不抽?(y/n)y
# b,你抽到的牌是:('草花', 5),现有牌组[('黑桃', 7), ('草花', 5)],总分>11,你爆了,分数归0
# a,你要抽牌还是不抽?(y/n)n
# a,你选择不抽牌,你的总分是 0 . 等待结果吧
# c 的总分是 8.5
# b 的总分是 0
# a 的总分是 0

 

posted on 2021-02-24 23:22  CyberBit  阅读(1637)  评论(0编辑  收藏  举报

导航