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