使用Python如何制作简单的21点小游戏
Admin 2022-07-19 群英技术资讯 1386 次浏览
这篇文章主要介绍“使用Python如何制作简单的21点小游戏”的相关知识,下面会通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“使用Python如何制作简单的21点小游戏”文章能帮助大家解决问题。用python实现21点小游戏,供大家参考,具体内容如下
from random import shuffle
import random
import numpy as np
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_count*poker_name
# 用于判断手中的牌是否有A,再根据牌面判断A是否取值1还是11
four_a = {'黑桃A', '红桃A', '梅花A', '方块A'}
# 计分器
total_score = np.array([0, 0])
# 记录回合数
game_round = 1
def random_cards(poker_name_list):
"""
定义洗牌函数:重新对牌进行随机排列
"""
shuffle(poker_name_list)
def score_count(hand_poker):
"""
计算手中牌的分数
:param hand_poker:一个含有牌名的列表
:return: 手中牌 的分数poker_score
"""
# 声明一个变量,记录牌的总分数
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
break
else:
continue
if have_a:
if poker_score + 10 <= 21:
poker_score = poker_score + 10
return poker_score
def who_win(your_score, pc_score):
"""
判断游戏的胜负
:param your_score: 玩家分数
:param pc_score: 电脑分数
:return: 胜负的数组
"""
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_poker():
"""
是否继续要牌
"""
if_continue = input("是否继续要下一张牌?(Y/N)>>>>:")
if if_continue.upper() == "Y":
return get_one_poker()
elif if_continue.upper() == "N":
print('玩家停止叫牌')
return False
else:
print("输入有误,请重新输入")
return if_get_next_poker()
def get_one_poker():
"""
发牌函数:随机将poker_list里的牌取出一张
:return:
"""
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('对不起,剩余牌数不足,无法进行下一轮,游戏结束。')
exit(1)
else:
return True
elif if_next_round.upper() == "N":
print("玩家不玩了。游戏结束!!")
exit(1)
else:
print("输入有误,请重新输入")
return continue_or_quit()
def start_game_init_two_poker(poker_database):
"""
初始化游戏,给玩家和电脑随机发两张牌
:param poker_database: 牌堆
:return: 玩家和电脑的初始牌面列表
"""
return [poker_database.pop(random.randint(0, len(poker_list)-1)),
poker_database.pop(random.randint(0, len(poker_list)-1))]
def every_round(porker_list):
"""
每一轮游戏的流程
:param porker_list:牌堆
:return:游戏的获胜者
"""
# 声明一个变量,代表玩家手里的牌
your_hand_poker = []
# 声明一变量,代表电脑手里的牌
pc_hand_poker = []
# 游戏开始,先从牌堆中取两张牌
you_init_poker = start_game_init_two_poker(porker_list)
pc_init_poker = start_game_init_two_poker(porker_list)
# 展示玩家获得的扑克
print(f"玩家所获得的牌是:{you_init_poker[0]}和{you_init_poker[1]}")
print(f"电脑所获得的第一张牌是:{pc_init_poker[0]}")
# 玩家和电脑得到所发的两张扑克牌
your_hand_poker.extend(you_init_poker)
pc_hand_poker.extend(pc_init_poker)
# 计算初始扑克的分数
your_score = score_count(your_hand_poker)
pc_score = score_count(pc_hand_poker)
# 根据初始牌面分数,判断是否能有21点,如果有直接使用判断输赢函数
if your_score == 21 or pc_score == 21:
print("初始牌中有21点了。")
return who_win(your_score, pc_score)
# 如果没有,根据自己手中的牌,判断是否要牌。
else:
while True:
get_new_poker = if_get_next_poker()
# 玩家要牌
if get_new_poker != False:
# 将新牌拿到手里并重新计算手里的牌的分数
your_hand_poker.append(get_new_poker)
print(f"玩家手里的牌是{your_hand_poker}")
your_score = score_count(your_hand_poker)
if your_score > 21:
print("玩家的牌已经超过21点")
print(f"电脑手里的牌是{pc_hand_poker}")
return who_win(your_score, pc_score)
else:
continue
# 玩家停止要牌,则电脑开始要牌
elif get_new_poker == False:
# 电脑要牌规则一:只要比玩家分数就要牌
# while pc_score < your_score:
# pc_new_poker = get_one_poker()
# pc_hand_poker.append(pc_new_poker)
# # 重新计算电脑手中的牌的分数
# pc_score = score_count(pc_hand_poker)
# 电脑要牌规则二:当电脑的手中牌的分数落在区间[1:18]时,就一直要牌
while pc_score in range(1, 19):
pc_new_poker = get_one_poker()
pc_hand_poker.append(pc_new_poker)
# 重新计算电脑的分数
pc_score = score_count(pc_hand_poker)
print(f"电脑手里的牌为{pc_hand_poker}")
return who_win(your_score, pc_score)
else:
continue
"""
游戏调用主程序
"""
while True:
print("游戏即将开始,祝你好运!!!")
input("按下【enter】开始游戏>>>")
print(f"现在是第{game_round}轮游戏")
# 洗牌
random_cards(poker_list)
# 游戏开始
score = every_round(poker_list)
# 计算总分
total_score = np.add(total_score, score)
print(f'此轮游戏结束,目前比分:{total_score[0]}:{total_score[1]}')
game_round += 1
continue_or_quit()
running result

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
Matplotlib是一个面向对象的绘图库,我们绘制的图像中,每条曲线,每个边框等等都对应一个对象,下面这篇文章主要给大家介绍了关于Python matplotlib绘图时指定图像大小及放大图像的相关资料,需要的朋友可以参考下
python中的schedule模块可以使我们方便简单的使用定时任务,即在特定的时间自动的执行一些任务的功能,本文将用这一模块实现邮件自动发送,需要的可以参考一下
2020.3.7准备scrapy,使用anaconda创建一个新的环境,执行“conda create -n scrapyEnv python=3.6”,结果出现了“CondaHTTPError: HTTP 000 CONNECTION FAILED for url <https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/current_repodata.json>”。以下是我的解决步骤。
本文主要介绍了pytorch中transforms的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧<BR>
深度学习已经成为机器学习中最受欢迎和发展最快的领域。深度学习的常见应用包括语音识别、图像识别、自然语言处理、推荐系统等等。本文将通过一些示例代码,带你详细了解深入学习
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008