# Project: 棋牌遊戲11點
# Author: jack
# Version: 2.2
# Start Time: 2021-07-24
import random
P_INFO = [["小王",0.5],["大王",0.5]] # 初始一副poker牌
USER_LIST = ["莊家","alex","武沛齊"] # 玩家資訊表
P_INFO_TWO = [] # 剩餘牌列表
USER_POKER = [] # 人員和牌列表
RESULT ={} # 最終成績表
# 1、生成一副撲克牌(自己設計撲克牌的結構)
def one_poker(P_INFO):
poker_type = ["黑桃","紅桃","草花","方塊"]
# 基本牌數字
poker_num = ["A","2","3","4","5","6","7","8","9","10","J","Q","K","小王","大王"]
for type in poker_type:
count = 1
for num in range(len(poker_num)):
if num > 12:
pass
elif num > 9:
card = [type+poker_num[num],0.5] # J、Q、K、代表的值為0.5
P_INFO.append(card)
else:
card = [type+poker_num[num],count]
P_INFO.append(card)
count += 1
# print(P_INFO)
return P_INFO # 已生成一副牌
# 2、3個玩家(玩家也可以自己定義)
def user_num(USER_LIST):
count = 0
while count < 54:
user_name = input("請輸入玩家名字,輸入Q/q退出遊戲,輸入S/s開始發牌:").strip()
print()
if user_name.upper() == "Q":
exit()
elif user_name.upper() == "S":
break
elif user_name in USER_LIST:
print("{}玩家名稱已存在,請重新輸入".format(user_name))
else:
USER_LIST.append(user_name) # 新玩家自動新增到玩家資訊表
count += 1
else:
exit("太多人玩了,牌不夠了")
return USER_LIST
# 3、發牌
def deal(USER_LIST,P_INFO,RESULT,P_INFO_TWO):
count = 0
while count < len(USER_LIST):
user_p_one = random.randint(0,len(P_INFO)-1) # 產生0-53之間的一個整數
card = P_INFO[user_p_one]
user = USER_LIST[count]
USER_POKER.append([user,card])
print("{}抽到的第1張牌為:{}".format(user,card))
print("牌面值為:{}".format(card[1]))
print()
RESULT[user] = card[1]
P_INFO.pop(user_p_one)
count += 1
P_INFO_TWO.extend(P_INFO) # 剩餘牌列表
return USER_POKER,RESULT,P_INFO_TWO
# 4、要牌
def recard(USER_LIST,P_INFO_TWO,RESULT,USER_POKER):
count = 0
while count < len(USER_LIST):
user = input("{}選擇是否要牌,輸入N/n不要牌,回車繼續要牌:".format(USER_LIST[count])).strip()
print()
if user.upper() == "N":
count += 1
# continue
else:
user_p_one = random.randint(0,len(P_INFO_TWO)-1) # 取一張牌
card = P_INFO_TWO[user_p_one] # 牌的資訊
user = USER_LIST[count] # 使用者資訊
USER_POKER[count].append(card) # 牌加到人員和牌列表
print("{}抽到的牌為:{}".format(user,card))
score = RESULT.get(user) # 得到第一次牌分數
score += card[1] # 計算新的分數
print("{}現在的牌面是:{}".format(USER_LIST[count],USER_POKER[count][1:]))
print("牌面值為:{}".format(score))
print()
RESULT[user] = score # 把分數寫進字典
P_INFO_TWO.pop(user_p_one)
if score > 11: # 超過11分牌爆掉
RESULT[user] = 0
print("{}牌爆掉了,下次注意點......".format(user))
print()
count += 1
return P_INFO_TWO,RESULT
one_poker(P_INFO) # 生產一副牌
# for poker in P_INFO:
# print(poker[0],end=" ") # 顯示一副牌,不顯示值
print("JACK賭坊,歡迎光臨".center(30,"-"))
print("小賭怡情,大賭傷身,強賭灰飛煙滅".center(22,"-"))
print("玩家請看牌".center(40,"-"))
print(P_INFO) # 顯示一副牌
print("開始棋牌11點".center(34,"-"))
print()
user_num(USER_LIST) # 人員列表
# print(USER_LIST)
deal(USER_LIST,P_INFO,RESULT,P_INFO_TWO) # 第一次發牌
# print(USER_POKER)
# print(RESULT)
# print(P_INFO_TWO)
recard(USER_LIST,P_INFO_TWO,RESULT,USER_POKER) # 要牌過程
# print(P_INFO_TWO)
print(RESULT) # 列印最終結果字典
# 5、判斷勝者
# 字典排序
win_sorted = sorted(RESULT.items(),key=lambda x:x[1],reverse=True)
# print(win_sorted)
count = 1
while count < len(win_sorted):
if win_sorted[count-1][1] != win_sorted[count][1]:
print("勝者是{}".format(win_sorted[:count]).center(40,"-"))
break
else:
count += 1
else:
exit("沒有贏家...")