溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python如何實現(xiàn)撲克牌21點游戲

發(fā)布時間:2021-12-31 11:28:14 來源:億速云 閱讀:166 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹Python如何實現(xiàn)撲克牌21點游戲,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

直接上代碼

import random
import sys

# 牌面列表
card_code = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
# 花色列表
card_symbol = ['?', '?', '?', '?']


# 游戲初始化
def init(player_count):
    # 根據(jù)玩家數(shù)來生成玩家記牌器
    player_group = [[] for _ in range(player_count)]
    # 根據(jù)玩家數(shù)來生成玩家是否要牌
    player_isWant = [True for _ in range(player_count)]
    # 生成元素1~52的列表 (去掉大小鬼的撲克牌[52張])
    poker = list(range(1, 53))
    # 用random的shuffle函數(shù)對列表打亂順序 (洗牌)
    random.shuffle(poker)
    # 返回玩家組 玩家是否要牌 亂序52張撲克
    return player_group, player_isWant, poker


# 打印玩家點數(shù)
def print_player_point(player_group):
    # 存放玩家點數(shù)
    player_point = []
    # 遍歷每一位玩家
    for index in range(len(player_group)):
        # 打印每位玩家的牌和點數(shù)
        print("-------玩家"+str(index+1)+"------")
        # 初始化玩家點數(shù) 如果含有牌A 因為A可視為1點或11點 則有兩種點數(shù)
        current_player = [0, 0]
        # 遍歷每位玩家的手牌
        for card in player_group[index]:
            """
            核心代碼
            由于牌面的數(shù)字是從1到52 所以牌面要先減1再求余才是牌面列表真正的下標
            若玩家抽到牌為15 即牌面為15 - 13 = 2 且按花色順序為? 即2?
            牌面 15 - 1 = 14 再 14 % 13 = 1 這個就是對應(yīng)牌面列表的第二位元素 即2
            花色 15 - 1 = 14 再 14 / 13 = 1 對應(yīng)花色列表第二位元素 即?
            """
            # 獲取牌面和花色下標
            code_index = int((card - 1) % 13)
            symbol_index = int((card - 1) / 13)
            # 打印玩家牌信息
            print(card_code[code_index] + card_symbol[symbol_index], end="\t")
            # 如果牌面含有A 則添加不同點數(shù)1和11
            if (code_index + 1) == 1:
                current_player[0] += 1
                current_player[1] += 11
            # 如果牌面不含A 則添加相同點數(shù)
            else:
                current_player[0] += code_index + 1
                current_player[1] += code_index + 1
        # 如果兩個點數(shù)一致 則打印一個點數(shù)
        if current_player[0] == current_player[1]:
            print("點數(shù)為"+str(current_player[0])+"點")
        # 否則打印兩個點數(shù)
        else:
            print("點數(shù)為"+str(current_player[0])+"點或"+str(current_player[1]))
        # 添加當(dāng)前玩家點數(shù)
        player_point.append(current_player)
    # 返回所有玩家點數(shù)
    return player_point


# 玩游戲
def play_game():
    # 打印游戲規(guī)則
    print("-------21點游戲------")
    print("---A可看做1點或11點---")
    # 死循環(huán)一直進行游戲
    while True:
        # 初始化玩家數(shù)為0
        player_count = 0
        # 當(dāng)玩家數(shù)小于等于1或大于5時繼續(xù)詢問
        while player_count <= 1 or player_count > 5:
            # 詢問玩家數(shù)
            print("有多少位玩家?(2~5位)", end="")
            # 獲取控制臺輸入的數(shù)字 無驗證輸入 若輸入非數(shù)字 程序直接報錯
            player_count = int(input())
        # 初始化游戲 返回玩家組 玩家是否要牌 亂序52張撲克
        player_group, player_isWant, poker = init(player_count)
        # 開始發(fā)牌 先為每位玩家發(fā)兩張牌 循環(huán)玩家數(shù)
        for index in range(player_count):
            for i in range(2):
                # pop() 函數(shù)用于移除列表中的一個元素(默認最后一個元素)并且返回該元素的值。
                player_group[index].append(poker.pop())
        # 打印玩家點數(shù) 并獲取當(dāng)前玩家點數(shù)
        player_point = print_player_point(player_group)
        # 只要玩家繼續(xù)要牌 且 還有剩余牌 則一直詢問玩家是否要牌
        while True in player_isWant and len(poker) > 0:
            # 遍歷玩家
            for index in range(player_count):
                # 判斷玩家是否有可能還需要牌
                if player_isWant[index] is True:
                    # 詢問玩家是否要牌
                    print("玩家"+str(index+1)+",您再要一張?(y/n)")
                    # 獲取控制臺輸入
                    isWant = str(input())[0]
                    # 如果輸入的字符為"n" 則將玩家標記為不再需要牌
                    if isWant == "n":
                        player_isWant[index] = False
                    # 如果不為字符"n" 默認為繼續(xù)要牌 給該玩家發(fā)一張牌
                    else:
                        player_group[index].append(poker.pop())
            # 每輪詢問結(jié)束 打印玩家點數(shù) 并獲取當(dāng)前玩家點數(shù)
            player_point = print_player_point(player_group)
        print("\n"*5+"====本輪游戲結(jié)束====")
        # 定義一個計分器
        score = []
        # 要牌結(jié)束 遍歷所有玩家的點數(shù) 判斷哪位玩家勝利
        for point_list in player_point:
            # 如果兩個兩個點數(shù)相同 說明沒有A
            if point_list[0] == point_list[1]:
                # 如果分數(shù)大于21 直接取負數(shù) 小于等于21 任意取一個作為分數(shù)
                score.append(-point_list[0] if point_list[0] > 21 else point_list[0])
            # 如果兩個點數(shù)不想同 說明含有A 則繼續(xù)判斷
            else:
                # 如果兩個點數(shù)中大的那個點數(shù)還小于等于21
                if max(point_list) <= 21:
                    # 去最大值為分數(shù)
                    score.append(max(point_list))
                # 如果兩個點數(shù)中大的那個點數(shù)大于21
                else:
                    # 如果小的點數(shù)大于21 直接取負數(shù) 小于等于21 取最小值為分數(shù)
                    score.append(-min(point_list) if min(point_list) > 21 else min(point_list))
        # 最高分
        max_point = max(score)
        # 如果最高分的人數(shù)為1 直接認為最高分的玩家獲勝 打印游戲結(jié)果
        if score.count(max_point) == 1:
            print("玩家"+str(score.index(max_point) + 1)+"獲勝!")
        # 否則最高分的分數(shù)有并列 認為有多個人獲勝
        else:
            # 獲勝玩家列表
            temp_list = []
            # 遍歷分數(shù)
            for index in range(len(score)):
                # 分數(shù)等于最高分 記錄玩家
                if score[index] == max_point:
                    temp_list.append("玩家"+str(index+1))
            # 拼接獲勝玩家列表 打印游戲結(jié)果
            print("恭喜"+",".join(temp_list)+"獲勝!")
        # 詢問是否繼續(xù)游戲
        print("是否繼續(xù)游戲?(y/n)")
        # 如果控制臺輸入不為字符"y" 表示退出
        if str(input())[0] != 'y':
            sys.exit()


# 程序主入口
if __name__ == '__main__':
    # 玩游戲
    play_game()

運行結(jié)果如下

Python如何實現(xiàn)撲克牌21點游戲

Python如何實現(xiàn)撲克牌21點游戲

以上是“Python如何實現(xiàn)撲克牌21點游戲”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI