最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)21點(diǎn)小游戲

 更新時(shí)間:2021年04月26日 17:02:34   作者:清風(fēng)er  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)21點(diǎn)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

用python實(shí)現(xiàn)21點(diǎn)小游戲,供大家參考,具體內(nèi)容如下

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())

# 撲克牌的數(shù)量
poker_count = 1
poker_list = poker_count*poker_name

# 用于判斷手中的牌是否有A,再根據(jù)牌面判斷A是否取值1還是11
four_a = {'黑桃A', '紅桃A', '梅花A', '方塊A'}

# 計(jì)分器
total_score = np.array([0, 0])

# 記錄回合數(shù)
game_round = 1


def random_cards(poker_name_list):
    """
    定義洗牌函數(shù):重新對(duì)牌進(jìn)行隨機(jī)排列
    """
    shuffle(poker_name_list)


def score_count(hand_poker):
    """
    計(jì)算手中牌的分?jǐn)?shù)
    :param hand_poker:一個(gè)含有牌名的列表
    :return: 手中牌 的分?jǐn)?shù)poker_score
    """
    # 聲明一個(gè)變量,記錄牌的總分?jǐn)?shù)
    poker_score = 0
    # 標(biāo)記:判斷是否有A的標(biāo)記,默認(rèn)沒有
    have_a = False

    # 計(jì)算手中牌的分?jǐn)?shù)
    for k in hand_poker:
        poker_score += playing_cards[k]

    # 判斷手中的牌是否含有A,再根據(jù)A的規(guī)則進(jìn)行分?jǐn)?shù)的計(jì)算
    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):
    """
    判斷游戲的勝負(fù)
    :param your_score: 玩家分?jǐn)?shù)
    :param pc_score: 電腦分?jǐn)?shù)
    :return: 勝負(fù)的數(shù)組
    """
    if your_score > 21 and pc_score > 21:
        print('平局')
        return np.array([0, 0])
    elif your_score > 21 and pc_score <= 21:
        print('對(duì)不起,玩家輸了')
        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('對(duì)不起,玩家輸了')
            return np.array([0, 1])
        else:
            print('平局!!')
            return np.array([0, 0])


def if_get_next_poker():
    """
    是否繼續(xù)要牌
    """
    if_continue = input("是否繼續(xù)要下一張牌?(Y/N)>>>>:")
    if if_continue.upper() == "Y":
        return get_one_poker()

    elif if_continue.upper() == "N":
        print('玩家停止叫牌')
        return False
    else:
        print("輸入有誤,請(qǐng)重新輸入")
        return if_get_next_poker()


def get_one_poker():
    """
    發(fā)牌函數(shù):隨機(jī)將poker_list里的牌取出一張
    :return:
    """
    return poker_list.pop(random.randint(0, len(poker_list)-1))


def continue_or_quit():
    """
    一輪游戲結(jié)束后,詢問玩家是否進(jìn)行下一輪
    """
    if_next_round = input("是否進(jìn)行下一輪游戲(Y/N)>>>>:")
    if if_next_round.upper() == 'Y':
        # 判斷撲克牌是否玩的了下一輪
        if len(poker_list) <= 15:
            print('對(duì)不起,剩余牌數(shù)不足,無法進(jìn)行下一輪,游戲結(jié)束。')
            exit(1)
        else:
            return True
    elif if_next_round.upper() == "N":
        print("玩家不玩了。游戲結(jié)束??!")
        exit(1)
    else:
        print("輸入有誤,請(qǐng)重新輸入")
        return continue_or_quit()


def start_game_init_two_poker(poker_database):
    """
    初始化游戲,給玩家和電腦隨機(jī)發(fā)兩張牌
    :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:游戲的獲勝者
    """
    # 聲明一個(gè)變量,代表玩家手里的牌
    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]}")
    # 玩家和電腦得到所發(fā)的兩張撲克牌
    your_hand_poker.extend(you_init_poker)
    pc_hand_poker.extend(pc_init_poker)
    # 計(jì)算初始撲克的分?jǐn)?shù)
    your_score = score_count(your_hand_poker)
    pc_score = score_count(pc_hand_poker)
    # 根據(jù)初始牌面分?jǐn)?shù),判斷是否能有21點(diǎn),如果有直接使用判斷輸贏函數(shù)
    if your_score == 21 or pc_score == 21:
        print("初始牌中有21點(diǎn)了。")
        return who_win(your_score, pc_score)
    # 如果沒有,根據(jù)自己手中的牌,判斷是否要牌。
    else:
        while True:
            get_new_poker = if_get_next_poker()

            # 玩家要牌
            if get_new_poker != False:
                # 將新牌拿到手里并重新計(jì)算手里的牌的分?jǐn)?shù)
                your_hand_poker.append(get_new_poker)
                print(f"玩家手里的牌是{your_hand_poker}")
                your_score = score_count(your_hand_poker)
                if your_score > 21:
                    print("玩家的牌已經(jīng)超過21點(diǎn)")
                    print(f"電腦手里的牌是{pc_hand_poker}")
                    return who_win(your_score, pc_score)
                else:
                    continue
            # 玩家停止要牌,則電腦開始要牌
            elif get_new_poker == False:
                # 電腦要牌規(guī)則一:只要比玩家分?jǐn)?shù)就要牌
                # while pc_score < your_score:
                #     pc_new_poker = get_one_poker()
                #     pc_hand_poker.append(pc_new_poker)
                #     # 重新計(jì)算電腦手中的牌的分?jǐn)?shù)
                #     pc_score = score_count(pc_hand_poker)
                # 電腦要牌規(guī)則二:當(dāng)電腦的手中牌的分?jǐn)?shù)落在區(qū)間[1:18]時(shí),就一直要牌
                while pc_score in range(1, 19):
                    pc_new_poker = get_one_poker()
                    pc_hand_poker.append(pc_new_poker)
                    # 重新計(jì)算電腦的分?jǐn)?shù)
                    pc_score = score_count(pc_hand_poker)
                print(f"電腦手里的牌為{pc_hand_poker}")
                return who_win(your_score, pc_score)
            else:
                continue


"""
游戲調(diào)用主程序
"""
while True:
    print("游戲即將開始,祝你好運(yùn)?。?!")
    input("按下【enter】開始游戲>>>")
    print(f"現(xiàn)在是第{game_round}輪游戲")

    # 洗牌
    random_cards(poker_list)

    # 游戲開始
    score = every_round(poker_list)

    # 計(jì)算總分
    total_score = np.add(total_score, score)

    print(f'此輪游戲結(jié)束,目前比分:{total_score[0]}:{total_score[1]}')
    game_round += 1
    continue_or_quit()

running result

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python中的字典詳細(xì)介紹

    python中的字典詳細(xì)介紹

    這篇文章主要介紹了python中的字典詳細(xì)介紹,字典是Python中最強(qiáng)大的數(shù)據(jù)類型之一,本文講解了什么是字典、創(chuàng)建字典和給字典賦值 、字典的基本操作、映射類型操作符、映射相關(guān)的函數(shù)、字典的方法等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • Python?運(yùn)算符Inplace?與Standard?

    Python?運(yùn)算符Inplace?與Standard?

    這篇文章主要介紹了Python?運(yùn)算符Inplace?與Standard,nplace運(yùn)算符的行為類似于普通運(yùn)算符,只是它們?cè)诳勺兒筒豢勺兡繕?biāo)的情況下以不同的方式運(yùn)行
    2022-09-09
  • Python使用scipy保存圖片的一些注意點(diǎn)

    Python使用scipy保存圖片的一些注意點(diǎn)

    這篇文章主要介紹了Python使用scipy保存圖片的一些注意點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python 多進(jìn)程共享全局變量之Manager()詳解

    python 多進(jìn)程共享全局變量之Manager()詳解

    這篇文章主要介紹了python 多進(jìn)程共享全局變量之Manager()詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 在keras里面實(shí)現(xiàn)計(jì)算f1-score的代碼

    在keras里面實(shí)現(xiàn)計(jì)算f1-score的代碼

    這篇文章主要介紹了在keras里面實(shí)現(xiàn)計(jì)算f1-score的代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • 200行python代碼實(shí)現(xiàn)2048游戲

    200行python代碼實(shí)現(xiàn)2048游戲

    這篇文章主要為大家詳細(xì)介紹了200行Python代碼實(shí)現(xiàn)2048游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • python GUI編程(Tkinter) 創(chuàng)建子窗口及在窗口上用圖片繪圖實(shí)例

    python GUI編程(Tkinter) 創(chuàng)建子窗口及在窗口上用圖片繪圖實(shí)例

    這篇文章主要介紹了python GUI編程(Tkinter) 創(chuàng)建子窗口及在窗口上用圖片繪圖實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Scrapy項(xiàng)目實(shí)戰(zhàn)之爬取某社區(qū)用戶詳情

    Scrapy項(xiàng)目實(shí)戰(zhàn)之爬取某社區(qū)用戶詳情

    這篇文章主要介紹了Scrapy項(xiàng)目實(shí)戰(zhàn)之爬取某社區(qū)用戶詳情,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Python3用tkinter和PIL實(shí)現(xiàn)看圖工具

    Python3用tkinter和PIL實(shí)現(xiàn)看圖工具

    這篇文章給大家分享了Python3用tkinter和PIL實(shí)現(xiàn)看圖工具的詳細(xì)實(shí)例代碼,有興趣的朋友參考學(xué)習(xí)下。
    2018-06-06
  • VsCode終端激活anconda環(huán)境問題解決

    VsCode終端激活anconda環(huán)境問題解決

    本文主要介紹了VsCode終端激活anconda環(huán)境問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01

最新評(píng)論

依安县| 赤峰市| 曲阜市| 河北省| 唐河县| 蚌埠市| 桂阳县| 东港市| 广元市| 太原市| 南阳市| 成安县| 馆陶县| 宜丰县| 东莞市| 渝北区| 冷水江市| 利辛县| 石首市| 蒲城县| 徐州市| 霍林郭勒市| 嵩明县| 汝州市| 石城县| 台北县| 大埔县| 禄丰县| 东丰县| 井冈山市| 璧山县| 临沧市| 青铜峡市| 广东省| 淮北市| 天等县| 泗洪县| 花莲市| 渑池县| 桂阳县| 青河县|