你在玩扑克吗?Python教你玩“经典纸牌游戏二十一点”

介绍

昨天不是星期天吗?

你通常在家里做什么来放松?

周末购物,出去看电影……这是你的周末。

程序员周末在家躺着唐诗躺着,偶尔加班,或者闲暇时和几个朋友在家打麻将打牌!

​​​特别是长假期间【ps:没有假期,长假就是过年】在老家的时候,亲戚很多,七阿姨和八阿姨终于一年聚一次,打麻将和扑克。最快的接触你的感受~

​​​​说起打扑克,我们经常打200四、炒金花,三比一是什么名字打扑克,让我想想……

​讲真话,我们都是方言,普通话我翻译不出来,估计你没看懂,23333~

​​今天小编就带大家来制作一款二十一点扑克游戏!

有大佬可以优化一下这段代码,做出精致豪华的界面~~

文本

游戏规则:二十一点,又称二十一点,此游戏由 2 到 6 人玩,使用除大小国王外的 52 张牌,玩家的目标是使手中牌的总和不超过 21 并成为尽可能大。. 玩 1 套牌时,选择以下各项之一(无大牌或小牌):

(1)初始化玩家数量:

def iniGame():
    global playerCount, cards
    while(True):
        try:
            playerCount = int(input('输入玩家数:'))
        except ValueError:
            print('无效输入!')
            continue
        if playerCount < 2:
            print('玩家必须大于1!')
            continue
        else:
            break
    try:
        decks = int(input('输入牌副数:(默认等于玩家数)'))
    except ValueError:
        print('已使用默认值!')
        decks = playerCount
    print('玩家数:', playerCount, ',牌副数:', decks)
    cards = getCards(decks)  # 洗牌

(2)创建了一个玩家列表,计算机与玩家对战。

def createPlayerList():
    global playerList
    playerList = []
    for i in range(playerCount):
        playerList += [{'id': '', 'cards': [], 'score': 0}].copy()
        playerList[i]['id'] = '电脑' + str(i+1)
    playerList[playerCount-1]['id'] = '玩家'
    random.shuffle(playerList)  # 为各玩家随机排序

(3)一开始会设置2张明牌来显示积分。

def gameStart():
    print('为各玩家分2张明牌:')
    for i in range(playerCount):  # 为每个玩家分2张明牌
        deal(playerList[i]['cards'], cards, 2)
        playerList[i]['score'] = getScore(playerList[i]['cards'])  # 计算初始得分
        print(playerList[i]['id'], ' ', getCardName(playerList[i]['cards']),
              ' 得分 ', playerList[i]['score'])
        time.sleep(1.5)

(4)游戏依次为电脑和玩家分配第三张隐藏牌,其他人看不到。

def gamePlay():
    for i in range(playerCount):
        print('当前', playerList[i]['id'])
        if playerList[i]['id'] == '玩家':  # 玩家
            while(True):
                print('当前手牌:', getCardName(playerList[i]['cards']))
                _isDeal = input('是否要牌?(y/n)')
                if _isDeal == 'y':
                    deal(playerList[i]['cards'], cards)
                    print('新牌:', getCardName(playerList[i]['cards'][-1]))
                    # 重新计算得分:
                    playerList[i]['score'] = getScore(playerList[i]['cards'])
                elif _isDeal == 'n':
                    break
                else:
                    print('请重新输入!')
        else:  # 电脑
            while(True):
                if isDeal(playerList[i]['score']) == 1:  # 为电脑玩家判断是否要牌
                    deal(playerList[i]['cards'], cards)
                    print('要牌。')
                    # 重新计算得分:
                    playerList[i]['score'] = getScore(playerList[i]['cards'])
                else:
                    print('不要了。')
                    break
        time.sleep(1.5)

(5)随机随机播放:

def getCards(decksNum):
    cardsList = ['Aa', 'Ab', 'Ac', 'Ad',
                 'Ka', 'Kb', 'Kc', 'Kd',
                 'Qa', 'Qb', 'Qc', 'Qd',
                 'Ja', 'Jb', 'Jc', 'Jd',
                 '0a', '0b', '0c', '0d',
                 '9a', '9b', '9c', '9d',
                 '8a', '8b', '8c', '8d',
                 '7a', '7b', '7c', '7d',
                 '6a', '6b', '6c', '6d',
                 '5a', '5b', '5c', '5d',
                 '4a', '4b', '4c', '4d',
                 '3a', '3b', '3c', '3d',
                 '2a', '2b', '2c', '2d']
    cardsList *= decksNum       # 牌副数
    random.shuffle(cardsList)   # 随机洗牌
    return cardsList

(6)​设置品牌名称字典:

cardNameDict = {'Aa': '黑桃A', 'Ab': '红桃A', 'Ac': '梅花A', 'Ad': '方片A',
                'Ka': '黑桃K', 'Kb': '红桃K', 'Kc': '梅花K', 'Kd': '方片K',
                'Qa': '黑桃Q', 'Qb': '红桃Q', 'Qc': '梅花Q', 'Qd': '方片Q',
                'Ja': '黑桃J', 'Jb': '红桃J', 'Jc': '梅花J', 'Jd': '方片J',
                '0a': '黑桃10', '0b': '红桃10', '0c': '梅花10', '0d': '方片10',
                '9a': '黑桃9', '9b': '红桃9', '9c': '梅花9', '9d': '方片9',
                '8a': '黑桃8', '8b': '红桃8', '8c': '梅花8', '8d': '方片8',
                '7a': '黑桃7', '7b': '红桃7', '7c': '梅花7', '7d': '方片7',
                '6a': '黑桃6', '6b': '红桃6', '6c': '梅花6', '6d': '方片6',
                '5a': '黑桃5', '5b': '红桃5', '5c': '梅花5', '5d': '方片5',
                '4a': '黑桃4', '4b': '红桃4', '4c': '梅花4', '4d': '方片4',
                '3a': '黑桃3', '3b': '红桃3', '3c': '梅花3', '3d': '方片3',
                '2a': '黑桃2', '2b': '红桃2', '2c': '梅花2', '2d': '方片2'}

​(7)评判获胜者:

def showWinAndLose():
    loserList = []  # [['id', score], ['id', score], ...]
    winnerList = []  # [['id', score], ['id', score], ...]
    winnerCount = 0
    loserCount = 0
    for i in range(playerCount):
        if playerList[i]['score'] > 21:  # 爆牌直接进入败者列表
            loserList.append([playerList[i]['id'],  playerList[i]['score']])
        else:  # 临时胜者列表
            winnerList.append([playerList[i]['id'], playerList[i]['score']])
    if len(winnerList) == 0:  # 极端情况:全部爆牌
        print('全部玩家爆牌:')
        for i in range(len(loserList)):
            print(loserList[i][0], loserList[i][1])
    elif len(loserList) == 0:  # 特殊情况:无人爆牌
        winnerList.sort(key=lambda x: x[1], reverse=True)  # 根据分数值排序胜者列表
        for i in range(len(winnerList)):  # 计算最低分玩家数量
            if i != len(winnerList)-1:
                if winnerList[-i-1][1] == winnerList[-i-2][1]:
                    loserCount = (i+2)
                else:
                    if loserCount == 0:
                        loserCount = 1
                    break
            else:
                loserCount = len(loserList)
        if loserCount == 1:
            loserList.append(winnerList.pop())
        else:
            while(len(loserList) != loserCount):
                loserList.append(winnerList.pop())
        for i in range(len(winnerList)):  # 计算最高分玩家数量
            if i != len(winnerList)-1:
                if winnerList[i][1] == winnerList[i+1][1]:
                    winnerCount = (i+2)
                else:
                    if winnerCount == 0:
                        winnerCount = 1
                    break
            else:
                winnerCount = len(winnerList)
        while(len(winnerList) != winnerCount):
            winnerList.pop()
        print('获胜:')
        for i in range(len(winnerList)):
            print(winnerList[i][0], winnerList[i][1])
        print('失败:')
        for i in range(len(loserList)):
            print(loserList[i][0], loserList[i][1])
    else:  # 一般情况:有人爆牌
        winnerList.sort(key=lambda x: x[1], reverse=True)  # 根据分数值排序胜者列表
        for i in range(len(winnerList)):  # 计算最高分玩家数量
            if i != len(winnerList)-1:
                if winnerList[i][1] == winnerList[i+1][1]:
                    winnerCount = (i+2)
                else:
                    if winnerCount == 0:
                        winnerCount = 1
                    break
            else:
                winnerCount = len(winnerList)
        while(len(winnerList) != winnerCount):
            winnerList.pop()
        print('获胜:')
        for i in range(len(winnerList)):
            print(winnerList[i][0], winnerList[i][1])
        print('失败:')
        for i in range(len(loserList)):
            print(loserList[i][0], loserList[i][1])

​游戏效果:咳咳咳……感觉这游戏靠运气和胆量!!

​总结​

哈哈哈!小编玩游戏比较没用打扑克,要不要试试呢?无聊的时候可以摸摸鱼做酱油~

制作不易,记得点三下哦!!如需本文完整代码+图片素材。

新手安装包,免费激活码,更多资料【私信编辑器06】可免费获取!!

相关文章

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注