python 21点小游戏 python实现21点小游戏
清风er 人气:0想了解python实现21点小游戏的相关内容吗,清风er在本文为您仔细讲解python 21点小游戏的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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
加载全部内容