您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了python如何實現四人制撲克牌游戲,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
題目:
設計一個簡單的四人制撲克牌游戲,能夠完成以下功能:
1. 洗牌
2. 發牌
3.自定義規則,在每輪單張出牌時,判定贏家
4.自定義規則,判定最終的贏家
規則簡化版:
僅能出單張牌,且出牌時,每個人出的是自己手中牌中剛好能壓過上家的最小牌,最先出完的為贏家
import random from random import choice flower = ['\u2660','\u2663','\u2665','\u2666'] pai = ['3','4','5','6','7','8','9','10','J','Q','K','A','2'] list = [] list0 = []#儲存發的牌 list1 = [] list2 = [] list3 = [] value = [] value0 = []#儲存牌代表的值 value1 = [] value2 = [] value3 = [] l00 = []#儲存進行升序排序后的牌 l11 = [] l22 = [] l33 = [] for i in flower: for j in pai: list.append(i+j) for i in range(4): for j in range(13): value.append(j) d = dict(zip(list,value)) for i in range(13): for j in range(4): if(j == 0): k = choice(list)#隨機選牌 for x in range(len(list)): if(k == list[x]):#和鏈表的牌進行匹配,刪掉對應項 value0.append(d[k]) list.pop(x) break list0.append(k) if(j == 1): k = choice(list) for x in range(len(list)): if (k == list[x]): value1.append(d[k]) list.pop(x) break list1.append(k) if (j == 2): k = choice(list) for x in range(len(list)): if (k == list[x]): value2.append(d[k]) list.pop(x) break list2.append(k) if (j == 3): k = choice(list) for x in range(len(list)): if (k == list[x]): value3.append(d[k]) list.pop(x) break list3.append(k) d0 = dict(zip(list0,value0))#將每個人的牌轉換為字典形式 d1 = dict(zip(list1,value1)) d2 = dict(zip(list2,value2)) d3 = dict(zip(list3,value3)) l0 = sorted(d0.values())#對牌所代表的數字進行排序 l1 = sorted(d1.values()) l2 = sorted(d2.values()) l3 = sorted(d3.values()) #對發給每個人的牌進行排序 for i in range(len(l0)): for j in list0: if(l0[i] == d0[j]): l00.append(j) break for i in range(len(l1)): for j in list1: if(l1[i] == d1[j]): l11.append(j) break for i in range(len(l2)): for j in list2: if(l2[i] == d2[j]): l22.append(j) break for i in range(len(l0)): for j in list3: if(l3[i] == d3[j]): l33.append(j) break # y = choice(['0','1','2','3']) print("第一個人的牌:",l00) print("第二個人的牌:",l11) print("第三個人的牌:",l22) print("第四個人的牌:",l33) y = random.randint(0,3) if (y == 0): y = y + 1 n = l0[0] l0.pop(0) elif (y == 1): y = y + 1 n = l1[0] l1.pop(0) elif (y == 2): y = y + 1 n = l2[0] l2.pop(0) elif (y == 3): y = 0 n = l3[0] l3.pop(0) for i in range(13): if(y == 0): for j in range(len(l0)): if(l0[j] > n): n = l0[j] l0.pop(j) if(len(l0) != 0 and n >= l1[len(l1)-1] and n >= l2[len(l2)-1] and n >= l3[len(l3)-1]):#判斷是否當前牌中最大牌,若是,則該此人繼續出牌 n = l0[0] l0.pop(0) break y = y + 1 if (len(l0) == 0): print("贏家:第一個人") break if(y == 1): for j in range(len(l1)): if(l1[j] > n): n = l1[j] l1.pop(j) if (len(l1) != 0 and n >= l0[len(l0) - 1] and n >= l2[len(l2) - 1] and n >= l3[len(l3) - 1]): n = l1[0] l1.pop(0) break y = y + 1 if (len(l1) == 0): print("贏家:第二個人") break if(y == 2): for j in range(len(l2)): if(l2[j] > n): n = l2[j] l2.pop(j) if (len(l2) != 0 and n >= l0[len(l0) - 1] and n >= l1[len(l1) - 1] and n >= l3[len(l3) - 1]): n = l2[0] l2.pop(0) break y = y + 1 if (len(l2) == 0): print("贏家:第三個人") break if (y == 3): for j in range(len(l3)): if (l3[j] > n): n = l3[j] l3.pop(j) if (len(l3) != 0 and n >= l0[len(l0) - 1] and n >= l1[len(l1) - 1] and n >= l2[len(l2) - 1]): n = l3[0] l3.pop(0) break y = 0 if (len(l3) == 0): print("贏家:第四個人") break #將剩余牌從鍵值轉化成牌 if(len(l0) != 0): for i in range(len(l0)): for j in list0: if(l0[i] == d0[j]): l0[i] = j break if(len(l1) != 0): for i in range(len(l1)): for j in list1: if(l1[i] == d1[j]): l1[i] = j break if(len(l2) != 0): for i in range(len(l2)): for j in list2: if(l2[i] == d2[j]): l2[i] = j break if(len(l3) != 0): for i in range(len(l3)): for j in list3: if(l3[i] == d3[j]): l3[i] = j break print("第一個人的牌:",l0) print("第二個人的牌:",l1) print("第三個人的牌:",l2) print("第四個人的牌:",l3)
以上就是關于python如何實現四人制撲克牌游戲的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。