1.每局游戏开始时会给每位玩家随机分发两张牌;
2.两张牌加起来的点数最大者获胜。
# -*- coding: UTF-8 -*-
import random
#auto函数:负责建立顺序排列的牌堆
def auto():
pokers=[]
poker=[]
for i in ['Heart','Spade','Diamond','Club']:
for j in ['A','2','3','4','5','6','7','8','9','10','J','Q','K']:
poker.append(i)
poker.append(j)
pokers.append(poker)
poker=[]
return pokers
def poker_game():
player_name = []
for i in range(player_number):
player_name.append("player"+str(i))
print '玩家共有',player_number,'名','分别是:',player_name
li={}
for k in player_name:
b=random.sample(poker,2) #随机抽两张牌
for s in b:
poker.remove(s) #扑克牌被从牌堆里抽出来
li.setdefault(k,b) #抽出来的扑克牌发给玩家
print '每个玩家抽到的牌为:',li
temp2 = 0
dic = {}
tt_name = []
for i in player_name:
temp = 0
for each in li[i]:
if each[1] == 'A':
temp = temp + 1
elif each[1] == 'J':
temp = temp + 11
elif each[1] == 'Q':
temp = temp + 12
elif each[1] == 'K':
temp = temp + 13
else:
temp = temp + int(each[1])
print '玩家',i,'抽到牌的总点数为:',temp
if temp > temp2:
temp2 = temp
dic.setdefault(i, temp)
tt_name.append(i)
elif temp == temp2: #当前玩家总点数与之前的最大总点数一样大的情况,先记录下来
dic.setdefault(i, temp)
tt_name.append(i)
for i in tt_name: #删去之前记录的不是最大的点数及其玩家姓名
if dic[i] < temp2:
del dic[i]
print '点数最大的玩家姓名及其点数为:',dic
while True:
try:
print "\n———————————————新的一轮游戏开始—————————————————\n"
player_number = input("请输入玩家的数量(输入数字0则游戏结束): ")
if player_number == 0:
break
elif player_number > 0 and player_number < 27: #除去大小王,有52张扑克牌,每人发两张牌,最多26人参加游戏
poker = auto()
print '洗牌前:', poker
random.shuffle(poker) # 洗牌
print '洗牌后:', poker
poker_game()
else:
print "\n !!! 请输入正确的玩家数量!!!(必须是十进制数字,且大于0、小于27)"
except:
print "\n !!! 请输入大于0、小于27的整数数字!!!"