1. # -*- codeing: utf-8 -*-
  2. # Project: 棋牌遊戲11點
  3. # Author: jack
  4. # Version: 2.2
  5. # Start Time: 2021-07-24
  6. import random
  7. P_INFO = [["小王",0.5],["大王",0.5]] # 初始一副poker牌
  8. USER_LIST = ["莊家","玩家1","玩家2"] # 玩家資訊表
  9. P_INFO_TWO = [] # 剩餘牌列表
  10. USER_POKER = [] # 人員和牌列表
  11. RESULT ={} # 最終成績表
  12. # 1、生成一副撲克牌(自己設計撲克牌的結構)
  13. def one_poker(P_INFO):
  14. poker_type = ["黑桃","紅桃","草花","方塊"]
  15. # 基本牌數字
  16. poker_num = ["A","2","3","4","5","6","7","8","9","10","J","Q","K","小王","大王"]
  17. for type in poker_type:
  18. count = 1
  19. for num in range(len(poker_num)):
  20. if num > 12:
  21. pass
  22. elif num > 9:
  23. card = [type+poker_num[num],0.5] # J、Q、K、代表的值為0.5
  24. P_INFO.append(card)
  25. else:
  26. card = [type+poker_num[num],count]
  27. P_INFO.append(card)
  28. count += 1
  29. # print(P_INFO)
  30. return P_INFO # 已生成一副牌
  31. # 2、3個玩家(玩家也可以自己定義)
  32. def user_num(USER_LIST):
  33. count = 0
  34. while count < 54:
  35. user_name = input("請輸入玩家名字,輸入Q/q退出遊戲,輸入S/s開始發牌:").strip()
  36. print()
  37. if user_name.upper() == "Q":
  38. exit()
  39. elif user_name.upper() == "S":
  40. break
  41. elif user_name in USER_LIST:
  42. print("{}玩家名稱已存在,請重新輸入".format(user_name))
  43. else:
  44. USER_LIST.append(user_name) # 新玩家自動新增到玩家資訊表
  45. count += 1
  46. else:
  47. exit("太多人玩了,牌不夠了")
  48. return USER_LIST
  49. # 3、發牌
  50. def deal(USER_LIST,P_INFO,RESULT,P_INFO_TWO):
  51. count = 0
  52. while count < len(USER_LIST):
  53. user_p_one = random.randint(0,len(P_INFO)-1) # 產生0-53之間的一個整數
  54. card = P_INFO[user_p_one]
  55. user = USER_LIST[count]
  56. USER_POKER.append([user,card])
  57. print("{}抽到的第1張牌為:{}".format(user,card))
  58. print("牌面值為:{}".format(card[1]))
  59. print()
  60. RESULT[user] = card[1]
  61. P_INFO.pop(user_p_one)
  62. count += 1
  63. P_INFO_TWO.extend(P_INFO) # 剩餘牌列表
  64. return USER_POKER,RESULT,P_INFO_TWO
  65. # 4、要牌
  66. def recard(USER_LIST,P_INFO_TWO,RESULT,USER_POKER):
  67. count = 0
  68. while count < len(USER_LIST):
  69. user = input("{}選擇是否要牌,輸入N/n不要牌,回車繼續要牌:".format(USER_LIST[count])).strip()
  70. print()
  71. if user.upper() == "N":
  72. count += 1
  73. # continue
  74. else:
  75. user_p_one = random.randint(0,len(P_INFO_TWO)-1) # 取一張牌
  76. card = P_INFO_TWO[user_p_one] # 牌的資訊
  77. user = USER_LIST[count] # 使用者資訊
  78. USER_POKER[count].append(card) # 牌加到人員和牌列表
  79. print("{}抽到的牌為:{}".format(user,card))
  80. score = RESULT.get(user) # 得到第一次牌分數
  81. score += card[1] # 計算新的分數
  82. print("{}現在的牌面是:{}".format(USER_LIST[count],USER_POKER[count][1:]))
  83. print("牌面值為:{}".format(score))
  84. print()
  85. RESULT[user] = score # 把分數寫進字典
  86. P_INFO_TWO.pop(user_p_one)
  87. if score > 11: # 超過11分牌爆掉
  88. RESULT[user] = 0
  89. print("{}牌爆掉了,下次注意點......".format(user))
  90. print()
  91. count += 1
  92. return P_INFO_TWO,RESULT
  93. one_poker(P_INFO) # 生產一副牌
  94. # for poker in P_INFO:
  95. # print(poker[0],end=" ") # 顯示一副牌,不顯示值
  96. print("JACK賭坊,歡迎光臨".center(30,"-"))
  97. print("小賭怡情,大賭傷身,強賭灰飛煙滅".center(22,"-"))
  98. print("玩家請看牌".center(40,"-"))
  99. print(P_INFO) # 顯示一副牌
  100. print("開始棋牌11點".center(34,"-"))
  101. print()
  102. user_num(USER_LIST) # 人員列表
  103. # print(USER_LIST)
  104. deal(USER_LIST,P_INFO,RESULT,P_INFO_TWO) # 第一次發牌
  105. # print(USER_POKER)
  106. # print(RESULT)
  107. # print(P_INFO_TWO)
  108. recard(USER_LIST,P_INFO_TWO,RESULT,USER_POKER) # 要牌過程
  109. # print(P_INFO_TWO)
  110. print(RESULT) # 列印最終結果字典
  111. # 5、判斷勝者
  112. # 字典排序
  113. win_sorted = sorted(RESULT.items(),key=lambda x:x[1],reverse=True)
  114. # print(win_sorted)
  115. count = 1
  116. while count < len(win_sorted)+1:
  117. if win_sorted[count-1][1] != win_sorted[count][1]:
  118. print("勝者是{}".format(win_sorted[:count]).center(40,"-"))
  119. break
  120. else:
  121. count += 1