21点游戏简单开发(Python)

程序功能

实现21点游戏

 

游戏规则

1、玩家共两个角色:电脑和人类,电脑是庄家

2、游戏开始时,先给人类和电脑每个玩家分别发两张牌作为底牌,庄家底牌只漏一张

3、判断双方底牌是否直接为21点,如果其中一方为21点则直接判胜利,并在总分上加一分。如果双方都是21点,那就是平局,不得分。

4、当初始牌面上,没有直接出现21点,人类玩家根据自己的牌面大小决定是否继续要牌。如果要牌,那就在牌堆中抽一张,然后再次判断胜负。如果人类牌面的总点数超过了21点,那就直接判输

5、如果人类玩家停止要牌了,并且没有因为超过21点而被判输的情况下,则电脑要牌。电脑要牌这里,可以自己设计一个规则:

5.1 用的是,电脑一直要牌,直到比人类玩家大才停止要牌。(也可以用其他的)

6、 循环4和5的步骤

7、 完成一轮游戏的时候,可由人类玩家决定,是否继续玩下一轮

8、 牌堆中剩余的牌数不够玩一轮游戏的时候,游戏自动结束。

9、 计算规则: 2、3、4、5、6、7、8、9、10分别是正常的点数,J、Q、K都是10点

10、A比较特殊,首先把A当做1来计算,牌面总分数如果小于21,那么再把A当做11再计算一次,如果这个时候仍然小于21,那么A就当11算,如果这个时候牌面总分数大于了21,那么A就当1算。

 

游戏流程图

 

代码

import random
import numpy as np
from sys import exit

"""
1、对牌组进行洗牌
2、发牌
    2-1、初始化发牌,发两张
    2-2、中途要拍,发一张
3、计算手牌分数
4、胜负判断
5、是否继续叫牌
6、是否继续下一轮
"""


"""
初始化变量扑克牌组
"""
playing_cards = {
    "♠A": 1, "♠2": 2, "♠3": 3, "♠4": 4, "♠5": 5, "♠6": 6, "♠7": 7,
    "♠8": 8, "♠9": 9, "♠10": 10, "♠J": 10, "♠Q": 10, "♠K": 10,
    "♥A": 1, "♥2": 2, "♥3": 3, "♥4": 4, "♥5": 5, "♥6": 6, "♥7": 7,
    "♥8": 8, "♥9": 9, "♥10": 10, "♥J": 10, "♥Q": 10, "♥K": 10,
    "♣A": 1, "♣2": 2, "♣3": 3, "♣4": 4, "♣5": 5, "♣6": 6, "♣7": 7,
    "♣8": 8, "♣9": 9, "♣10": 10, "♣J": 10, "♣Q": 10, "♣K": 10,
    "♦A": 1, "♦2": 2, "♦3": 3, "♦4": 4, "♦5": 5, "♦6": 6, "♦7": 7,
    "♦8": 8, "♦9": 9, "♦10": 10, "♦J": 10, "♦Q": 10, "♦K": 10
}
poker_number = 1
poker_names = list(playing_cards.keys())
poker_list = poker_names * poker_number

# 回合数
game_round = 1

# 用于判断手中排是否有A
A_list = ["♠A", "♥A", "♣A", "♦A"]

# 总分情况(玩家: 电脑)
total_score = np.array([0, 0])


# 模块化函数
"""
对牌组进行打乱
"""
def random_pokers(rand_poker_list):
    random.shuffle(rand_poker_list)


"""
发牌,两种函数:
第一个,初始化发牌,一次给2张
第二个,后面叫牌,一次给1张
"""
def get_one_poker(input_poker_list):
    return input_poker_list.pop(random.randint(0, len(input_poker_list)-1))



def init_get_poker(input_poker_list):
    return [input_poker_list.pop(random.randint(0, len(input_poker_list)-1)),
            input_poker_list.pop(random.randint(0, len(input_poker_list)-1))]


"""
计算手中牌的分数
输入:手牌
返回值:分数
"""
def score_count(hand_poker):
    score = 0
    for i in hand_poker:
        score += playing_cards.get(i)

    # 判断有没有A
    for i in hand_poker:
        if i in A_list:
            if score + 10 <= 21:
                score = score + 10
            else:
                break

    return score

"""
胜负判断函数
输入:玩家和电脑分数
输出:输赢情况,并且返回比分,用于加入总比分
"""
def judge_win_lose(your_score, pc_score):
    if your_score > 21 and pc_score > 21:
        print("平局")
        return np.array([0, 0])
    elif your_score <= 21 and pc_score > 21:
        print("你赢了")
        return np.array([1, 0])
    elif your_score > 21 and pc_score <= 21:
        print("你输了")
        return np.array([0, 1])
    else:
        if your_score > pc_score:
            print("你赢了")
            return np.array([1, 0])
        elif your_score == pc_score:
            print("平局")
            return np.array([0, 0])
        else:
            print("你输了")
            return np.array([0, 1])


"""
是否要牌
输入:玩家输入Y/N
输出:返回状态
"""
def if_get_poker():
    if_continue = input("是否叫牌(Y/N)>>>>>>:")
    if if_continue.upper() == "Y":
        return True
    elif if_continue.upper() == "N":
        return False
    else:
        print("输入有误,请重新输入")
        if_get_poker()


"""
询问玩家是否继续
返回值:状态
"""
def continue_or_over(total_your, total_pc):
    if_continue = input("是否继续下一轮(Y/N)>>>>>>:")
    if if_continue.upper() == "Y":
        if len(poker_list) < 15:
            print("不好意思,剩余扑克数过少,只有{}张,游戏结束".format(len(poker_list)))
            print("")
            print("这盘游戏最终比分为:(玩家:电脑)>>>>{}:{}".format(total_your, total_pc))
            if total_your > total_pc:
                print("你最终赢得了胜利")
            elif total_your < total_pc:
                print("你最终输掉了游戏")
            else:
                print("和电脑五五开")

            exit(1)
        else:
            return True
    elif if_continue.upper() == "N":
        print("玩家不玩了,游戏结束")
        print("")
        print("这盘游戏最终比分为:(玩家:电脑)>>>>{}:{}".format(total_your, total_pc))
        if total_your > total_pc:
            print("你最终赢得了胜利")
        elif total_your < total_pc:
            print("你最终输掉了游戏")
        else:
            print("和电脑五五开")
        exit(1)
    else:
        print("输入有误,请重新输入")
        continue_or_over()


"""
处于一局游戏的流程
输入:牌组
返回值:该局比分
"""
def every_round(input_poker_list):
    # 初始化玩家和电脑的手牌
    your_hand_poker = []
    pc_hand_poker = []

    # 初始化两张牌
    your_init_poker = init_get_poker(input_poker_list)
    pc_init_poker = init_get_poker(input_poker_list)

    print("你的手牌是:{}和{}".format(your_init_poker[0], your_init_poker[1]))
    print("电脑的手牌是:{}和?".format(pc_init_poker[0]))

    # 判断有没有21点
    your_score = score_count(your_init_poker)
    pc_score = score_count(pc_init_poker)

    # 加入手牌
    your_hand_poker.extend(your_init_poker)
    pc_hand_poker.extend(pc_init_poker)

    if your_score == 21 or pc_score == 21:
        print("牌面有21点")
        return judge_win_lose(your_score, pc_score)
    else:
        # 玩家叫牌
        while True:
            if_get_your = if_get_poker()
            if if_get_your == True:
                new_poker = get_one_poker(input_poker_list)
                your_hand_poker.append(new_poker)
                your_score = score_count(your_hand_poker)

                print("你当前的手牌为:{}".format(your_hand_poker))

                if your_score > 21:
                    print("你的手牌超过21点,你输了")
                    print("此时电脑的手牌为:{}".format(pc_hand_poker))
                    return np.array([0, 1])
                else:
                    continue
            else:
                print("你停止叫牌")
                # 轮到电脑叫牌,要求,叫牌直到大于等于玩家分数
                while your_score > pc_score:
                    new_poker = get_one_poker(input_poker_list)
                    pc_hand_poker.append(new_poker)
                    pc_score = score_count(pc_hand_poker)
                print("此时电脑的手牌是:{}".format(pc_hand_poker))
                return judge_win_lose(your_score, pc_score)


"""
游戏开始咯
"""
input("按'回车'后,游戏正式开始")

while True:
    # 游戏提示
    print("********现在是第{}局游戏********".format(game_round))

    # 先洗牌
    random_pokers(poker_list)

    # 开始每一局, 记录当前输赢情况
    curr_score = every_round(poker_list)

    total_score = np.add(total_score, curr_score)

    # 播报当前比分
    print("当前总比分为:(玩家:电脑)>>>>{}:{}".format(total_score[0], total_score[1]))

    # print("此时牌组剩余卡数:{}".format(len(poker_list)))
    game_round = game_round + 1

    continue_or_over(total_score[0], total_score[1])
    print("")

 

效果演示:

  • 6
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值