用python写一个文字版单机斗地主
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                用python写一个文字版单机斗地主
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
                            
                            
                            本人熱愛編程,在學習python的過程中想通過做一個單機版文字斗地主來檢驗并提升自己的實力,因為本人還是菜鳥,程序可能還不夠完善,可能會出現一些自己未檢測到的bug,請大家多多包含。 
                        
                        
                        整體設計思路如下:
創建一個二維列表存儲所有的牌
創建一個長度為54的數字列表,并將0-53隨機存與該數組中當作打亂順序后的牌的下標
再創建一個二維列表用于存儲洗后的牌,將第一個二維列表通過上面的下標列表映射到該數組中
創建一個變量id,并賦予1到3中的隨機數字,當id為1時,玩家就是地主,id為2時,一號電腦就是地主,否則就是三號電腦是地主
接下來就是發牌,創建三個二維列表當作玩家和電腦的卡牌存儲器,然后將洗好的前20張牌給地主,農民各得17張
識別出牌種類的最關鍵方法是找到重復度最大的一類牌
后面內容太多,有興趣的朋友可以發評論問我,這里直接上源代碼
import random# 創牌 class card():type: '無'num: 1size: 1name: ''card_list = [] card_temp = ['', 1, 1, ''] for i in range(54):card_list.append(['無', 1, 1, ''])for i in range(0, 13):# print("%d\n"%i)for j in range(0, 4):# print(i*4+j)card_list[i * 4 + j][2] = i + 1if (i * 4 + j) % 4 == 0:card_list[i * 4 + j][0] = '?'elif (i * 4 + j) % 4 == 1:card_list[i * 4 + j][0] = '?'elif (i * 4 + j) % 4 == 2:card_list[i * 4 + j][0] = '?'elif (i * 4 + j) % 4 == 3:card_list[i * 4 + j][0] = '?'card_list[i * 4 + j][1] = (card_list[i * 4 + j][2] + 1) % 13 + 1if (card_list[i * 4 + j][1] == 1):card_list[i * 4 + j][3] = 'A'if (card_list[i * 4 + j][1] == 11):card_list[i * 4 + j][3] = 'J'if (card_list[i * 4 + j][1] == 12):card_list[i * 4 + j][3] = 'Q'if (card_list[i * 4 + j][1] == 13):card_list[i * 4 + j][3] = 'K'if (card_list[i * 4 + j][1] > 1 and card_list[i * 4 + j][1] < 11):card_list[i * 4 + j][3] = str(card_list[i * 4 + j][1])# 給大小王下定義 card_list[52][0] = '小' card_list[52][1] = 14 card_list[52][2] = 14 card_list[52][3] = '小王'card_list[53][0] = '大' card_list[53][1] = 14 card_list[53][2] = 15 card_list[53][3] = '大王'# print(card_list)# 洗牌 # 先創建一個無重復的隨機數組 # 然后將其下標映射到新數組 arr = [] new_cards = []def shuffle_cards(arr, new_cards, card_list):# creat_random_arrwhile (len(arr) < 54):a = random.randint(0, 53)if (not (a in arr)):arr.append(a)# shuffle_cardsfor i in range(0, 54):new_cards.append(card_list[arr[i]])shuffle_cards(arr, new_cards, card_list)# print(new_cards) # 洗牌結束,開始發牌# 創建角色 class player():name: ''list = []id = '未知'player1_list = [] player2_list = [] player3_list = []# 發牌,先發51張 for i in range(0, 51):if (i % 3 == 0):player1_list.append(new_cards[i])elif (i % 3 == 1):player2_list.append(new_cards[i])elif (i % 3 == 2):player3_list.append(new_cards[i])# 選地主,發地主牌 id = random.randint(1, 3) if (id == 1):player1_list.extend(new_cards[51:]) elif (id == 2):player2_list.extend(new_cards[51:]) elif (id == 3):player3_list.extend(new_cards[51:])# print(player1_list) # print(player2_list) # print(player3_list)# 創建一個用于保存上一個玩家出牌的列表 last_cards = []# 給牌進行排序 def card_sort(card_list):if (len(card_list)) > 1:temp = card_list[0]n = len(card_list)for i in range(n):for j in range(0, n - i - 1):if card_list[j][2] > card_list[j + 1][2]:temp = card_list[j]card_list[j] = card_list[j + 1]card_list[j + 1] = tempcard_sort(player1_list) card_sort(player2_list) card_sort(player3_list) # print(player1_list)# 顯示玩家有哪些牌,并且統計每種牌的張數 card_view = []def print_card(player1_list, card_view):if len(player1_list)>0:if id == 1:print("你是地主,剩余牌數:%d\n你的牌是:"%len(player1_list))else:print("你是農民,剩余牌數:%d\n你的牌是:"%len(player1_list))n = len(player1_list)card_view.clear()for i in range(0, n):# print("{0}-{1}-{2}".format(cards[i][0],cards[i][3],i))card_view.append(player1_list[i][3])card_view.append(player1_list[i][0])card_view.append(i)card_view.append(' ')print(card_view)count = 0char = player1_list[0][3]# 統計牌的數組,和下標statistic = []indexs = []ind = 0indexs.extend([0, ' '])for i in range(0, n):if player1_list[i][3] == char:count = count + 1else:statistic.extend([count, '張', char, ' '])ind = ind + countindexs.extend([ind, ' '])# print("有{0}個{1}".format(count,char))count = 1char = player1_list[i][3]# print("有{0}個{1}".format(count, char))statistic.extend([count, '張', char, ' '])print(statistic)print(indexs)print_card(player1_list, card_view)# 首先找最大重復的牌數目,如果是1并且牌的數目是1則為單牌,如果最大重復是1且牌不只一張則考慮順子# 創造一個統計數組元素最大重復數和出現次數的函數,返回[最大重復數-重復了幾次-最大重復的牌是幾號(重復了幾次就會出現幾個號)] def get_repeat(cards):# 然后找到同為最大次數的牌out = []if len(cards)>0:# 構建一個統計次數的列表list = []# 先排序card_sort(cards)n = len(cards)count = 0char = cards[0][2]for i in range(0, n):if cards[i][2] == char:count = count + 1else:list.append(char)list.append(count)count = 1char = cards[i][2]list.append(char)list.append(count)# 這樣我們就得到了一個統計次數的列表,接下來就是要找到最大次數max = 0i = 1is2 = 0while (i < len(list)):if (list[i] > max):max = list[i]if (list[i] == 2):is2 = is2 + 1i = i + 2# 然后找到同為最大次數的牌out.append(max)out.append(is2)i = 1while (i < len(list)):if (list[i] == max):out.append(list[i - 1])i = i + 2return out# 輸出結果為:第一個為最大次數,第二個為成對數,后面的都是同為這個次數的牌# print(player2_list) # print(get_repeat(player2_list))# 判斷是否為順序 def isSort(arr):k = 1for i in range(0, len(arr) - 1):if (arr[i + 1] - arr[i] != 1):k = 0return k# 接下來開始判斷出牌是否符合規則 def jude(cards):if cards == 0:return 0if len(cards) == 0:print("跳過")if len(cards) != 0:card_sort(cards)# retuen_listre_list = []k = 1str = ""# 準備輸出選擇的牌str_list = []for i in range(0, len(cards)):str_list.append(cards[i][3])str = ' '.join(str_list)list = get_repeat(cards)if (len(cards) == list[0]):if (list[0] == 1):print("單牌:", str)re_list.extend(["單牌", list[2]])elif (list[0] == 2):print("對子:", str)re_list.extend(["對子", list[2]])elif (list[0] == 3):print("三個:", str)re_list.extend(["三個", list[2]])elif (list[0] == 4):print("炸:", str)re_list.extend(["炸", list[2]])elif (len(cards) == 2):if (list[2] == 14 and list[3] == 15):print("王炸:", str)re_list.extend(["王炸", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 3):print("不符合規則")k = 0elif (len(cards) == 4):if (list[0] == 3):print("3帶1:", str)re_list.extend(["3帶1", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 5):if (list[0] == 3 and list[1] == 1):print("3帶2:", str)re_list.extend(["3帶2", list[2]])elif (list[0] == 1 and isSort(list[2:]) == 1 and (13 - list[2]) > len(cards)):print("順子:", str)re_list.extend(["順子", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 6):if (list[0] == 3 and len(list) - 2 == 2 and isSort(list[2:]) == 1 and len(list) - 2 == 2):print("飛機33:", str)re_list.extend(["飛機33", list[2]])elif (list[0] == 4 and list[1] == 0):print("4帶2:", str)re_list.extend(["4帶2", list[2]])elif (list[0] == 2 and isSort(list[2:]) == 1 and (13 - list[2]) > (len(cards) / 2)):print("連對:", str)re_list.extend(["連對", list[2]])elif (list[0] == 1 and isSort(list[2:]) == 1 and (13 - list[2]) > len(cards)):print("順子:", str)re_list.extend(["順子", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 7):if (list[0] == 1 and isSort(list[2:]) == 1 and (13 - list[2]) > len(cards)):print("順子:", str)re_list.extend(["順子", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 8):if (list[0] == 1 and isSort(list[2:]) == 1 and (13 - list[2]) > len(cards)):print("順子:", str)re_list.extend(["順子", list[2]])elif (list[0] == 2 and isSort(list[2:]) == 1 and (13 - list[2]) > (len(cards) / 2)):print("連對:", str)re_list.extend(["連對", list[2]])elif (list[0] == 4 and list[1] == 2):print("4帶2對:", str)re_list.extend(["4帶2對", list[2]])elif (list[0] == 3 and isSort(list[2:]) == 1 and len(list) - 2 == 2):print("飛機3311:", str)re_list.extend(["飛機3311", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 9):if (list[0] == 1 and isSort(list[2:]) == 1 and (13 - list[2]) > len(cards)):print("順子:", str)re_list.extend(["順子", list[2]])elif (list[0] == 3 and isSort(list[2:]) == 1 and len(list) - 2 == 3):print("飛機333:", str)re_list.extend(["飛機333", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 10):if (list[0] == 1 and isSort(list[2:]) == 1 and (13 - list[2]) > len(cards)):print("順子:", str)re_list.extend(["順子", list[2]])elif (list[0] == 3 and isSort(list[2:]) == 1 and list[1] == 2):print("飛機3322:", str)re_list.extend(["飛機3322", list[2]])elif (list[0] == 2 and isSort(list[2:]) == 1 and list[1] == 5 and (13 - list[2]) > (len(cards) / 2)):print("連對:", str)re_list.extend(["連對", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 11):if (list[0] == 1 and isSort(list[2:]) == 1 and (13 - list[2]) > len(cards)):print("順子:", str)re_list.extend(["順子", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 12):if (list[0] == 1 and isSort(list[2:]) == 1 and (13 - list[2]) > len(cards)):print("順子:", str)re_list.extend(["順子", list[2]])elif (list[0] == 2 and isSort(list[2:]) == 1 and (13 - list[2]) > (len(cards) / 2)):print("連對:", str)re_list.extend(["連對", list[2]])elif (list[0] == 3 and isSort(list[2:]) == 1 and len(list) - 2 == 4):print("飛機3333:", str)re_list.extend(["飛機3333", list[2]])elif (list[0] == 3 and isSort(list[2:]) == 1 and len(list) - 2 == 3):print("飛機333111:", str)re_list.extend(["飛機333111", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 13):print("不符合規則")k = 0elif (len(cards) == 14):if (list[0] == 2 and isSort(list[2:]) == 1 and (13 - list[2]) > (len(cards) / 2)):print("連對:", str)re_list.extend(["連對", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 15):if (list[0] == 3 and isSort(list[2:]) == 1 and len(list) - 2 == 5):print("飛機33333:", str)re_list.extend(["飛機33333", list[2]])elif (list[0] == 3 and isSort(list[2:]) == 1 and list[1] == 3):print("飛機333222:", str)re_list.extend(["飛機333222", list[2]])else:print("不符合規則")k = 0elif (len(cards) == 16):if (list[0] == 2 and isSort(list[2:]) == 1 and (13 - list[2]) > (len(cards) / 2)):print("連對:", str)re_list.extend(["連對", list[2]])elif (list[0] == 3 and isSort(list[2:]) == 1 and len(list) - 2 == 4):print("飛機33331111:", str)re_list.extend(["飛機33331111", list[2]])else:print("不符合規則")k = 0elif len(cards) == 17:print("不符合規則")elif len(cards) == 18:if list[0] == 2 and isSort(list[2:]) == 1 and (13 - list[2]) > (len(cards) / 2):print("連對:", str)re_list.extend(["連對", list[2]])else:print("不符合規則")k = 0elif len(cards) == 19:print("不符合規則")k = 0elif len(cards) == 20:if list[0] == 2 and isSort(list[2:]) == 1 and (13 - list[2]) > (len(cards) / 2):print("連對:", str)re_list.extend(["連對", list[2]])elif list[0] == 3 and isSort(list[2:]) == 1 and list[1] == 4:print("飛機33332222:", str)re_list.extend(["飛機33332222", list[2]])elif list[0] == 3 and isSort(list[2:]) == 1 and len(list) - 2 == 5:print("飛機3333311111:", str)re_list.extend(["飛機3333311111", list[2]])else:print("不符合規則")k = 0elif len(cards) > 20:print("不符合規則")k = 0if k != 0:return re_listelse:return k# 制定出牌規則 # 統計出牌器里面的最大重復數及其出現的次數 # 先統計出牌的張數 # 1張可能是單牌 # 2張可能是成對,要一樣 可能是王炸 # 3張可能是成帶,要一樣 # 4張可能是炸也可能是3帶1 # 5張可能是3帶2也可能是順子 # 6張可能是4帶2 可能是順子 可能是飛機33 可能是連對 # 7張只有可能是順子 # 8張牌 可能是4帶2對 可能是順子 可能是飛機3311 可能是連對 # 9張牌 可能是順子 可能是飛機333 # 10張牌 可能是順子 可能是連對 可能是飛機3322 # 11張牌 可能是順子 # 12張牌 可能是順子 可能是連對 可能是飛機3333、333111 # 13張牌 # 14張牌 可能是連對 # 15張牌 可能是飛機33333、可能是飛機333222 # 16張牌 可能是連對 可能是飛機33331111 # 17張牌 # 18張牌 可能是連對 # 19張牌 # 20張牌 可能是連對、 可能是飛機33332222、 可能是飛機3333311111# 一共有38種出牌方式,這里用38個函數進行判斷,并且返回一個數組,里面包括出牌類型和級別# 出牌器--輸入的是一個字符數組 # 將字符串轉化成數字列表 def trans(str):list = []if len(str)>0:str = str.split(',')for i in str:list.append(int(i))return list# 得到輸出的牌組 def put_cards(inds, cards):k = 1list = []for i in range(0, len(inds)):if inds[i] <= len(cards) and inds[i] >= 0:list.append(cards[inds[i]])else:print("輸入格式有誤")k = 0if k == 1:return listelse:return 0 # 如果格式有誤則重新輸入# 玩家輸入 put_ind="" def player_input(last_cards):cards_jude = []last_cards_jude = []put_ind = input("輸入你所出的牌的下標\n")if trans(put_ind)[0]==-1:return []if len(last_cards) == 0:while jude(put_cards(trans(put_ind), player1_list)) == 0 or put_cards(trans(put_ind), player1_list) == 0:put_ind = input("輸入你所出的牌的下標\n")else:k = 0while k == 0:cards_jude = jude(put_cards(trans(put_ind), player1_list))last_cards_jude = jude(last_cards)if cards_jude != 0 and last_cards_jude != 0 and len(trans(put_ind)) == len(last_cards) and cards_jude[0] == last_cards_jude[0] and cards_jude[1] > last_cards_jude[1]:k = 1if cards_jude != 0 and last_cards_jude != 0 and last_cards_jude[0] != '王炸' and cards_jude[0] == '炸':k = 1if cards_jude != 0 and cards_jude[0] == '王炸':k = 1if k == 1:breakput_ind = input("輸入錯誤,請重新輸入你所出的牌的下標\n")return put_ind# 如果符合規則,那么就刪除打出的牌def remov_cards():trans_put_ind = trans(put_ind)for i in range(0,len(trans_put_ind)): #容易出現bugdel (player1_list[trans_put_ind[i]])for j in range(i+1,len(trans_put_ind)):if trans_put_ind[i] < trans_put_ind[j]:trans_put_ind[j]=trans_put_ind[j]-1# put_ind = player_input() # remov_cards()# 與人機出牌有關的函數 # 按張數排序 def distribute_card(cards):i=0new_cards = []for j in range(1, 5):count = 1star = 0for i in range(1, len(cards)):if cards[i][2] == cards[i - 1][2]:count = count + 1else:if count == j:for k in range(star,i):new_cards.append(cards[k])count = 1star = iif count == j:for k in range(star, i+1):new_cards.append(cards[k])cards = new_cards[:]for k in range(0,len(new_cards)):cards[k] = new_cards[k]return new_cards# 得到牌區下標 def get_range(cards):indexs = []if len(cards)>0:i=0count = 1sum = 0star=0counts=[]for i in range(1, len(cards)):if cards[i][2] == cards[i - 1][2]:count = count + 1else:counts.append(count)count = 1counts.append(count)for i in range(1,len(counts)):sum = sum+counts[i-1]if counts[i-1] != counts[i]:if(counts[i]==2):indexs.extend([star,sum-1])star= sumif len(indexs)<2:indexs.extend([-1,-1])elif(counts[i]==3):indexs.extend([star, sum-1])star = sumif len(indexs)<4:indexs.extend([-1,-1])elif (counts[i] == 4):indexs.extend([star, sum-1])star = sumif len(indexs)<6:indexs.extend([-1,-1])if len(counts)>=i+1:sum = sum + counts[i]indexs.extend([star, sum-1])if len(indexs) < 4:indexs.extend([-1,-1,-1,-1,-1,-1])elif len(indexs) < 6:indexs.extend([-1,-1,-1,-1])elif len(indexs) < 8:indexs.extend([-1,-1])else:indexs.extend([-1,-1,-1,-1,-1,-1,-1,-1])return indexs# 通過牌數區得到有序區 def find_seq(type,cards,ind1,ind2):re_list = []if(ind1!=ind2 and ind1!=-1 and ind2!=-1):list=[]for i in range(0,len(cards)):list.append(cards[i][2])n=0step=0k=1star=ind1if(type=="單"):n = 5step = 1elif (type == "雙"):n = 3step = 2elif (type == "三"):n = 2step = 3i = ind1while (i <= ind2-step):if (list[i] + 1 == list[i + step] and list[i + step]<13):k = k + 1else:if (k >= n):re_list.extend([star, i + step - 1])star = i + stepk = 1i = i + stepif (k >= n):re_list.extend([star, i + step - 1])return re_list# 得出非順序下標 def find_nonSeq(ranges,seqs):non_seqs = []temps0 = []temps1 = []temps2 = []if ranges[0]!=-1:temps0.clear()for i in range(ranges[0],ranges[1]+1):if len(seqs[0]) > 0:for j in range(0,(len(seqs[0])+1)//2):if not(i>= seqs[0][j] and i<= seqs[0][j+1]):temps0.append(i)else:temps0.append(i)non_seqs.append(temps0)else:non_seqs.append([])if ranges[2] != -1:temps1.clear()for i in range(ranges[2], ranges[3] + 1):if len(seqs[1]) > 0:for j in range(0, (len(seqs[1]) + 1) // 2):if not (i >= seqs[1][j] and i <= seqs[1][j + 1]):temps1.append(i)else:temps1.append(i)non_seqs.append(temps1)else:non_seqs.append([])if ranges[4] != -1:temps2.clear()for i in range(ranges[4], ranges[5]+1):if len(seqs[2])>0:for j in range(0, (len(seqs[2])+1) //2):if not (i >= seqs[2][j] and i <= seqs[2][j + 1]):temps2.append(i)else:temps2.append(i)non_seqs.append(temps2)else:non_seqs.append([])return non_seqs def out_cards(last_cards,cards):ranges = []seqs = []non_seqs = []ranges = get_range(cards)size_list=[]for i in cards:size_list.append(i[2])seqs.append(find_seq("單", cards, ranges[0], ranges[1]))seqs.append(find_seq("雙", cards, ranges[2], ranges[3]))seqs.append(find_seq("三", cards, ranges[4], ranges[5]))non_seqs = find_nonSeq(ranges,seqs)out_inds=[] # 這里n表示了飛機的三數長度n=0i=0if len(last_cards) == 0: # 考慮出飛機if len(seqs[2])>0:for i in range(seqs[2][0],seqs[2][1]+1):out_inds.append(i)n = (seqs[2][1]+1-seqs[2][0])//3if ranges[0]!=-1 and len(non_seqs[0]) >= n:for i in range(0, n):out_inds.append(non_seqs[0][i])elif ranges[1]!=-1 and len(non_seqs[1]) >= 2*n:for i in range(0, 2*n):out_inds.append(non_seqs[0][i]) # 考慮出連對elif len(seqs[1])>0:for i in range(seqs[1][0],seqs[1][1]+1):out_inds.append(i) # 考慮出順子elif len(seqs[0])>0:for i in range(seqs[0][0],seqs[0][1]+1):out_inds.append(i) # 考慮出帶elif ranges[4]!=-1:for i in range(0,3):out_inds.append(ranges[4] + i)if ranges[2]!=-1:for i in range(0, 2):out_inds.append(ranges[2] + i)elif ranges[0]!=-1:out_inds.append(ranges[0]) # 考慮出單elif ranges[0]!=-1:out_inds.append(ranges[0]) # 考慮出對elif ranges[2]!=-1:for i in range(0, 2):out_inds.append(ranges[2] + i)# 接下來考慮當電腦不是第一次出牌的情況else:repeat = []repeat = get_repeat(last_cards)if repeat[0]==1 and len(last_cards)==1 and len(non_seqs[0])>0: #單牌for i in range(0,len(non_seqs[0])):if cards[non_seqs[0][i]][2]>repeat[2]:out_inds.append(non_seqs[0][i])breakelif repeat[0]==1 and len(last_cards)>1 and len(seqs[0])>0 and len(seqs[0])>0 and seqs[0][1]-seqs[0][0]+1>=len(repeat)-2 : #順子for i in range(seqs[0][0], seqs[0][1]+1):if cards[i][2] > repeat[2]:out_inds.append(i)if len(out_inds) >= len(repeat)-2:breakif len(out_inds) != len(last_cards):out_inds.clear()elif repeat[0]==2 and len(last_cards)==2 and len(non_seqs[1])>0: #對子for i in range(0,len(non_seqs[1])):if cards[non_seqs[1][i]][2]>repeat[2]:out_inds.append(non_seqs[1][i])if len(out_inds)>1:breakelif repeat[0]==2 and len(last_cards)>2 and len(seqs[1])>0 and len(seqs[1])>0 and seqs[1][1]-seqs[1][0]+1>=2*(len(repeat)-2) : #連對for i in range(seqs[1][0], seqs[1][1] + 1):if cards[i][2] > repeat[2]:out_inds.append(i)if len(out_inds) >= 2*(len(repeat) - 2):breakif len(out_inds) != len(last_cards):out_inds.clear()elif repeat[0]==3 and len(last_cards)==3 and len(non_seqs[2])>0: #三個for i in range(0,len(non_seqs[2])):if cards[non_seqs[2][i]][2]>repeat[2]:out_inds.append(non_seqs[2][i])if len(out_inds)>2:breakelif repeat[0]==3 and len(last_cards)==4 and len(non_seqs[2])>0 and len(non_seqs[0])>0: #3帶1for i in range(0,len(non_seqs[2])):if cards[non_seqs[2][i]][2]>repeat[2]:out_inds.append(non_seqs[2][i])if len(out_inds)>2:breakif len(out_inds)>2 :out_inds.append(non_seqs[0][0])elif repeat[0]==3 and len(last_cards)==5 and repeat[1]==1 and len(non_seqs[2])>0 and len(non_seqs[1])>0: #3帶2for i in range(0,len(non_seqs[2])):if cards[non_seqs[2][i]][2]>repeat[2]:out_inds.append(non_seqs[2][i])if len(out_inds)>2:breakif len(out_inds)>2 :out_inds.append(non_seqs[1][0])out_inds.append(non_seqs[1][1])elif repeat[0]==3 and len(last_cards) % 3 == 0 and 3*(len(repeat) - 2)== len(last_cards) and len(last_cards)>3 and len(seqs[2])>0 and seqs[2][1]-seqs[2][0]+1 >= len(last_cards): #飛機不帶for i in range(seqs[2][0], seqs[2][1] + 1):if cards[i][2] > repeat[2]:out_inds.append(i)if len(out_inds) >= 3*(len(repeat) - 2):breakif len(out_inds) != len(last_cards):out_inds.clear()elif repeat[0] == 3 and len(last_cards) % 4 == 0 and len(last_cards) > 4 and len(seqs[2])>0 and seqs[2][1] - seqs[2][0] + 1 >= (len(repeat) - 2)*3 and len(non_seqs[0]) >= len(last_cards)//4: # 飛機帶1for i in range(seqs[2][0], seqs[2][1] + 1):if cards[i][2] > repeat[2]:out_inds.append(i)if len(out_inds) >= 3*(len(repeat) - 2):breakif len(out_inds) >= 3 * (len(repeat) - 2):for i in range(0,len(last_cards)//4):out_inds.append(non_seqs[0][i])if len(out_inds) != len(last_cards):out_inds.clear()elif repeat[0] == 3 and len(last_cards) % 5 == 0 and len(last_cards) > 5 and len(seqs[2])>0 and seqs[2][1] - seqs[2][0] + 1 >= (len(repeat)-2)*3 and len(non_seqs[1]) >= 2*(len(last_cards)//5): # 飛機帶2for i in range(seqs[2][0], seqs[2][1] + 1):if cards[i][2] > repeat[2]:out_inds.append(i)if len(out_inds) >= 3*(len(repeat) - 2):breakif len(out_inds) >= 3 * (len(repeat) - 2):for i in range(0,2*len(last_cards)//5):out_inds.append(non_seqs[1][i])if len(out_inds) != len(last_cards):out_inds.clear()elif repeat[0]==4 and len(last_cards)==6 and ranges[6]!=-1 and len(non_seqs[0])>=2: #4帶2for i in range(ranges[6], ranges[7]+1):if cards[i][2]>repeat[2]:out_inds.append(i)if len(out_inds)>=4:breakif len(out_inds)>=4 :out_inds.append(non_seqs[0][0])out_inds.append(non_seqs[0][1])elif repeat[0]==4 and len(last_cards)==8 and repeat[1]==2 and ranges[6]!=-1 and len(non_seqs[1])>=4: #4帶2對for i in range(ranges[6], ranges[7] + 1):if cards[i][2] > repeat[2]:out_inds.append(i)if len(out_inds) >= 4:breakif len(out_inds)>=4 :for i in range(0,4):out_inds.append(non_seqs[1][i])elif len(last_cards)==2 and last_cards[0][2]==14 and last_cards[1][2]==15 : #王炸out_inds=[]elif repeat[0]==4 and len(last_cards)==4 and ranges[6]!=-1: # 炸for i in range(ranges[6], ranges[7]+1):if cards[i][2] > repeat[2]:out_inds.append(i)if len(out_inds) >= 4:breakelif len(last_cards) < len(cards) and 14 in size_list and 15 in size_list: # 其它情況出王炸out_inds.append(ranges[1])out_inds.append(ranges[1] - 1)elif ranges[6] != -1 and not (len(last_cards) == 2 and last_cards[0][2] == 14 and last_cards[1][2] == 15) and not (repeat[0] == 4 and len(last_cards) == 4): # 其它情況出炸for i in range(ranges[6], ranges[6] + 4):out_inds.append(i)# 組裝要出的牌outCards = []for i in range(0, len(out_inds)):outCards.append(cards[out_inds[i]])# 刪除剛才出的牌for i in range(0,len(out_inds)):del (cards[out_inds[i]])for j in range(i+1,len(out_inds)):if out_inds[i] < out_inds[j]:out_inds[j]=out_inds[j]-1return outCardsplayerCards2=[] playerCards3=[]# 創建一個變量統計當前出牌的編號 card_id = 0 while(len(player1_list)>0 and len(player2_list)>0 and len(player3_list)>0):if id == 1:print("輪到你出牌了")put_ind = player_input(last_cards)if len(trans(put_ind)) > 0:card_id = 1last_cards.clear()for i in trans(put_ind):last_cards.append(player1_list[i])remov_cards()if len(player1_list) == 0:breakplayer2_list=distribute_card(player2_list)if card_id == 2:last_cards.clear()playerCards2=out_cards(last_cards,player2_list)if len(playerCards2)>0:card_id = 2last_cards.clear()for i in playerCards2:last_cards.append(i)print("電腦一號是農民,剩余%d張牌,出的牌是:"%len(player2_list))jude(playerCards2)if len(player2_list) == 0:breakplayer3_list = distribute_card(player3_list)if card_id == 3:last_cards.clear()playerCards3 = out_cards(last_cards, player3_list)if len(playerCards3) > 0:card_id = 3last_cards.clear()for i in playerCards3:last_cards.append(i)print("電腦二號是農民,剩余%d張牌,出的牌是:" % len(player3_list))jude(playerCards3)if len(player3_list) == 0:breakprint_card(player1_list, card_view)if card_id == 1:last_cards.clear()print("你要大過的牌:")jude(last_cards)elif id == 2:player2_list = distribute_card(player2_list)if card_id == 2:last_cards.clear()playerCards2 = out_cards(last_cards, player2_list)if len(playerCards2) > 0:card_id = 2last_cards.clear()for i in playerCards2:last_cards.append(i)print("電腦一號是地主,剩余%d張牌,出的牌是:" % len(player2_list))jude(playerCards2)if len(player2_list) == 0:breakplayer3_list = distribute_card(player3_list)if card_id == 3:last_cards.clear()playerCards3 = out_cards(last_cards, player3_list)if len(playerCards3) > 0:card_id = 3last_cards.clear()for i in playerCards3:last_cards.append(i)print("電腦二號是農民,剩余%d張牌,出的牌是:" % len(player3_list))jude(playerCards3)if len(player3_list) == 0:breakprint_card(player1_list, card_view)if card_id == 1:last_cards.clear()print("你要大過的牌:")jude(last_cards)print("輪到你出牌了")put_ind = player_input(last_cards)if len(trans(put_ind)) > 0:card_id = 1last_cards.clear()for i in trans(put_ind):last_cards.append(player1_list[i])remov_cards()if len(player1_list) == 0:breakelif id == 3:player3_list = distribute_card(player3_list)if card_id == 3:last_cards.clear()playerCards3 = out_cards(last_cards, player3_list)if len(playerCards3) > 0:card_id = 3last_cards.clear()for i in playerCards3:last_cards.append(i)print("電腦二號是地主,剩余%d張牌,出的牌是:" % len(player3_list))jude(playerCards3)if len(player3_list) == 0:breakprint_card(player1_list, card_view)if card_id == 1:last_cards.clear()print("你要大過的牌:")jude(last_cards)print("輪到你出牌了")put_ind = player_input(last_cards)if len(trans(put_ind)) > 0:card_id = 1last_cards.clear()for i in trans(put_ind):last_cards.append(player1_list[i])remov_cards()if len(player1_list) == 0:breakplayer2_list = distribute_card(player2_list)if card_id == 2:last_cards.clear()playerCards2 = out_cards(last_cards, player2_list)if len(playerCards2) > 0:card_id = 2last_cards.clear()for i in playerCards2:last_cards.append(i)print("電腦一號是農民,剩余%d張牌,出的牌是:" % len(player2_list))jude(playerCards2)if len(player2_list) == 0:break# 接下來判斷輸贏 if id == 1:if len(player1_list)==0:print("你贏了!")else:print("你輸了") elif id==2:if len(player2_list)==0:print("你輸了!")else:print("你贏了!") elif id==3:if len(player3_list)==0:print("你輸了!")else:print("你贏了!")總結
以上是生活随笔為你收集整理的用python写一个文字版单机斗地主的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 开机出现Oxc000000e故障的解决方
 - 下一篇: 普通话测试能打分的软件是什么软件,普通话