python 21点小游戏源码

这是一个用Python编写的21点小游戏程序,包括初始化玩家、创建玩家列表、开始游戏、游戏进行、展示结果和判断胜负等功能。玩家和电脑按顺序决定是否要牌,最后根据得分确定赢家。
摘要由CSDN通过智能技术生成

import random
import time


def main():
    while(True):
        iniGame()
        createPlayerList()
        gameStart()
        gamePlay()
        showResult()
        showWinAndLose()
        startNewGame = input('开始新游戏?(y)')
        if startNewGame != 'y':
            break


# 初始化玩家参数
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)  # 洗牌


# 建立玩家列表
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)  # 为各玩家随机排序


# 分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)


# 按顺序询问玩家是否要牌
def gamePlay():
    for i in range(playerCount):
        print('当前', playerList[i]['id'])
        if playerList[i]['id'] == '玩家':  # 玩家
            while(True):
                print('当前手牌:', getCardName(playerL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悠闲饭团

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值