用python编21点游戏_Python实现21点小游戏

# 开发者: "Wang"

# 开发时间:2019/8/1520:39

# 文件名称:twentyonegame.py

# 开发工具:PyCharm

"""

程序功能模块设计:

洗牌:将牌进行随机排列

发牌:1,初始化发牌,一次发两张

2,要牌,一次一张

计分:2到10就是正常点数,JQK 都是10, 要考虑A的特殊性

胜负判断:比较电脑和玩家手中的分数,并记录

是否要牌:

继续还是退出:

"""

# 洗牌函数,shuffle的作用是随机打乱列表

from random import shuffle

import random

# 为什么用numpy,原因是numpy数组他提供对应位置相加,不用我们自己计算

# 用在多轮游戏的分数统计

import numpy as np

# exit函数j就是退出程序的意思

from sys import exit

# 初始化扑克牌

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_name = list(playing_cards.keys())

# 几副扑克

poker_count = 1

poker_list = poker_name * poker_count

# 用于判断手中的牌是否有A,根据分数来进行选A的牌的分值是0还是1

four_a = {"黑桃A", "红桃A", "方片A", "梅花A"}

# 计分器 玩家:电脑, 初始分数都是0

total_score = np.array([0, 0])

# 记录游戏是第几个回合

game_round = 1

"""

洗牌,重新对扑克牌随机排列

"""

def random_card(poker_name_list):

shuffle(poker_name_list)

"""

计算手里牌的分数,传进来的参数是list

"""

def score_count(hand_poker):

# 声明一个变量,记录牌的总分数

poker_score = 0

# 标记, 手里是否有A

have_a = False

# 计算手中牌的分数

for k in hand_poker:

poker_score += playing_cards[k]

# 判断手中牌是否有A,然后再根据A的规则进行分数计算

for i in hand_poker:

if i in four_a:

have_a = True

else:

continue

if have_a is True:

if poker_score + 10 <= 21:

poker_score = poker_score + 10

return poker_score

"""

判断输赢的函数

"""

def who_win(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([0, 1])

elif your_score <= 21 and pc_score > 21:

print("恭喜,你赢了")

return np.array([1, 0])

elif your_score <= 21 and pc_score <= 21:

if your_score > pc_score:

print("恭喜,你赢了")

return np.array([1, 0])

elif your_score < pc_score:

print("对不起,你输了")

return np.array([0, 1])

else:

print("平局")

return np.array([0, 0])

"""

是否继续要牌

"""

def if_get_next_card():

if_continue = input("是否继续要牌:")

if if_continue.upper() == "Y":

return get_one_card()

elif if_continue.upper() == "N":

print("玩家停止叫牌")

return False

else:

print("输入错误,请重新输入")

return if_get_next_card()

"""

发牌:要牌的时候,需要从牌堆里随机抽取一张牌

"""

def get_one_card():

# 发一张牌,必须要在牌堆中删除掉这张牌

return poker_list.pop(random.randint(0, len(poker_list) - 1))

"""

一轮游戏结束之后,询问玩家是否继续

"""

def continue_or_quit():

if_next_round = input("请问还想玩下一局么?(Y/N)>>>")

if if_next_round.upper() == "Y":

if len(poker_list) < 15:

print("剩余的扑克牌太少,不能继续")

exec(1)

else:

return True

elif if_next_round.upper() == "N":

print("游戏结束,玩家退出")

exit(1)

else:

print("输入错误,请再输入")

continue_or_quit()

# 开局初始化牌,自动给玩家和电脑发两张牌

def start_game_init_two_poker(poker_database):

return [poker_list.pop(random.randint(0, len(poker_database) - 1)),

poker_list.pop(random.randint(0, len(poker_database) - 1))]

"""

每一次游戏的流程

"""

def every_round(poker_list):

# 声明一个变量,这个变量代表我们手里的扑克

your_hand_poker = []

# 声明一个变量,这个变量代表电脑手里的扑克

pc_hand_poker = []

# 首先发2张牌

you_init_poker = start_game_init_two_poker(poker_list)

pc_init_poker = start_game_init_two_poker(poker_list)

# 荷官通知 把牌放到我们手中

your_hand_poker.extend(you_init_poker)

pc_hand_poker.extend(pc_init_poker)

# 展示手中的扑克

print("玩家手中的扑克牌{}和{}".format(your_hand_poker[0], your_hand_poker[1]))

print(f"电脑手中的扑克牌:{pc_hand_poker[0]}, ?\n")

# 计算初始牌的分值

score = np.array([score_count(your_hand_poker), score_count(pc_hand_poker)])

# 判断大小, 如果有等于21点,直接判断输赢

if score[0] == 21 or score[1] == 21:

print("初始有21")

return who_win(score[0], score[1])

# 否则,判断自己手中的牌,判断是否要下一张

else:

if score[0] > 21:

return who_win(score[0], score[1])

if score[1] > 21:

return who_win(score[0], score[1])

while score[0] < 21:

get_new_poker = if_get_next_card()

if get_new_poker is not False:

# 把新的扑克防守力

your_hand_poker.append(get_new_poker)

print("玩家手中的扑克牌{}".format(your_hand_poker))

score[0] = score_count(your_hand_poker)

# 判断分数大小

if score[0] > 21:

print("你手里牌大于21")

print("电脑手里牌是{}".format(pc_hand_poker))

return who_win(score[0], score[1])

elif score[0] == 21:

return who_win(score[0], score[1])

else:

continue

# 玩家不要牌了,电脑要牌

elif get_new_poker is False:

# 电脑要牌规则,只要比玩家低就叫,直到大等于玩家

while score[1] < score[0]:

pc_poker = get_one_card()

pc_hand_poker.append(pc_poker)

score[1] = score_count(pc_hand_poker)

print("电脑手中的牌是{}".format(pc_hand_poker))

return who_win(score[0], score[1])

else:

continue

while True:

input("游戏开始,祝你好运,按回车开始")

print("游戏是第{}轮".format(game_round))

random_card(poker_list)

score = every_round(poker_list)

# 计算总比分

total_score = np.add(total_score, score)

print("本轮结束,比分是玩家{}比电脑{}".format(total_score[0], total_score[1]))

game_round += 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值