本人热爱编程,在学习python的过程中想通过做一个单机版文字斗地主来检验并提升自己的实力,因为本人还是菜鸟,程序可能还不够完善,可能会出现一些自己未检测到的bug,请大家多多包含。
整体设计思路如下:
创建一个二维列表存储所有的牌
创建一个长度为54的数字列表,并将0-53随机存与该数组中当作打乱顺序后的牌的下标
再创建一个二维列表用于存储洗后的牌,将第一个二维列表通过上面的下标列表映射到该数组中
创建一个变量id,并赋予1到3中的随机数字,当id为1时,玩家就是地主,id为2时,一号电脑就是地主,否则就是三号电脑是地主
接下来就是发牌,创建三个二维列表当作玩家和电脑的卡牌存储器,然后将洗好的前20张牌给地主,农民各得17张
识别出牌种类的最关键方法是找到重复度最大的一类牌
后面内容太多,有兴趣的朋友可以发评论问我,这里直接上源代码
import random
# 创牌
class card():
type: '无'
num: 1
size: 1
name: ''
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 + 1
if (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 + 1
if (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_arr
while (len(arr) < 54):
a = random.randint(0, 53)
if (not (a in arr)):
arr.append(a)
# shuffle_cards
for 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] = temp
card_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 = 0
char = player1_list[0][3]
# 统计牌的数组,和下标
statistic = []
indexs = []
ind = 0
indexs.extend([0, ' '])
for i in range(0, n):
if player1_list[i][3] == char:
count = count + 1
else:
statistic.extend([count, '张', char, ' '])
ind = ind + count
indexs.extend([ind, ' '])
# print("有{0}个{1}".format(count,char))
count = 1
char = 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 = 0
char = cards[0][2]
for i in range(0, n):
if cards[i][2] == char:
count = count + 1
else:
list.append(char)
list.append(count)
count = 1
char = cards[i][2]
list.append(char)
list.append(count)
# 这样我们就得到了一个统计次数的列表,接下来就是要找到最大次数
max = 0
i = 1
is2 = 0
while (i < len(list)):
if (list[i] > max):
max = list[i]
if (list[i] == 2):
is2 = is2 + 1
i = i + 2
# 然后找到同为最大次数的牌
out.append(max)
out.append(is2)
i = 1
while (i < len(list)):
if (list[i] == max):
out.append(list[i - 1])
i = i + 2
return out
# 输出结果为:第一个为最大次数,第二个为成对数,后面的都是同为这个次数的牌
# print(player2_list)
# print(get_repeat(player2_list))
# 判断是否为顺序
def isSort(arr):
k = 1
for i in range(0, len(arr) - 1):
if (arr[i + 1] - arr[i] != 1):
k = 0
return k
# 接下来开始判断出牌是否符合规则
def jude(cards):
if cards == 0:
return 0
if len(cards) == 0:
print("跳过")
if len(cards) != 0:
card_sort(cards)
# retuen_list
re_list = []
k = 1
str = ""
# 准备输出选择的牌
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 = 0
elif (len(cards) == 3):
print("不符合规则")
k = 0
elif (len(cards) == 4):
if (list[0] == 3):
print("3带1:", str)
re_list.extend(["3带1", list[2]])
else:
print("不符合规则")
k = 0
elif (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 = 0
elif (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 = 0
elif (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 = 0
elif (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 = 0
elif (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 = 0
elif (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 = 0
elif (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 = 0
elif (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 = 0
elif (len(cards) == 13):
print("不符合规则")
k = 0
elif (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 = 0
elif (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 = 0
elif (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 = 0
elif 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 = 0
elif len(cards) == 19:
print("不符合规则")
k = 0
elif 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 = 0
elif len(cards) > 20:
print("不符合规则")
k = 0
if k != 0:
return re_list
else:
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 = 1
list = []
for i in range(0, len(inds)):
if inds[i] <= len(cards) and inds[i] >= 0:
list.append(cards[inds[i]])
else:
print("输入格式有误")
k = 0
if k == 1:
return list
else:
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 = 0
while 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 = 1
if cards_jude != 0 and last_cards_jude != 0 and last_cards_jude[0] != '王炸' and cards_jude[0] == '炸':
k = 1
if cards_jude != 0 and cards_jude[0] == '王炸':
k = 1
if k == 1:
break
put_ind = input("输入错误,请重新输入你所出的牌的下标\n")
return put_ind
# 如果符合规则,那么就删除打出的牌
def remov_cards():
trans_put_ind = trans(put_ind)
for i in range(0,len(trans_put_ind)): #容易出现bug
del (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=0
new_cards = []
for j in range(1, 5):
count = 1
star = 0
for i in range(1, len(cards)):
if cards[i][2] == cards[i - 1][2]:
count = count + 1
else:
if count == j:
for k in range(star,i):
new_cards.append(cards[k])
count = 1
star = i
if 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=0
count = 1
sum = 0
star=0
counts=[]
for i in range(1, len(cards)):
if cards[i][2] == cards[i - 1][2]:
count = count + 1
else:
counts.append(count)
count = 1
counts.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= sum
if len(indexs)<2:
indexs.extend([-1,-1])
elif(counts[i]==3):
indexs.extend([star, sum-1])
star = sum
if len(indexs)<4:
indexs.extend([-1,-1])
elif (counts[i] == 4):
indexs.extend([star, sum-1])
star = sum
if 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=0
step=0
k=1
star=ind1
if(type=="单"):
n = 5
step = 1
elif (type == "双"):
n = 3
step = 2
elif (type == "三"):
n = 2
step = 3
i = ind1
while (i <= ind2-step):
if (list[i] + 1 == list[i + step] and list[i + step]<13):
k = k + 1
else:
if (k >= n):
re_list.extend([star, i + step - 1])
star = i + step
k = 1
i = i + step
if (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=0
i=0
if 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])//3
if 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])
break
elif 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:
break
if 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:
break
elif 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):
break
if 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:
break
elif repeat[0]==3 and len(last_cards)==4 and len(non_seqs[2])>0 and len(non_seqs[0])>0: #3带1
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:
break
if 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带2
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:
break
if 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):
break
if 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: # 飞机带1
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):
break
if 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): # 飞机带2
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):
break
if 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带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:
break
if 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:
break
if 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:
break
elif 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]-1
return outCards
playerCards2=[]
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 = 1
last_cards.clear()
for i in trans(put_ind):
last_cards.append(player1_list[i])
remov_cards()
if len(player1_list) == 0:
break
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 = 2
last_cards.clear()
for i in playerCards2:
last_cards.append(i)
print("电脑一号是农民,剩余%d张牌,出的牌是:"%len(player2_list))
jude(playerCards2)
if len(player2_list) == 0:
break
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 = 3
last_cards.clear()
for i in playerCards3:
last_cards.append(i)
print("电脑二号是农民,剩余%d张牌,出的牌是:" % len(player3_list))
jude(playerCards3)
if len(player3_list) == 0:
break
print_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 = 2
last_cards.clear()
for i in playerCards2:
last_cards.append(i)
print("电脑一号是地主,剩余%d张牌,出的牌是:" % len(player2_list))
jude(playerCards2)
if len(player2_list) == 0:
break
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 = 3
last_cards.clear()
for i in playerCards3:
last_cards.append(i)
print("电脑二号是农民,剩余%d张牌,出的牌是:" % len(player3_list))
jude(playerCards3)
if len(player3_list) == 0:
break
print_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 = 1
last_cards.clear()
for i in trans(put_ind):
last_cards.append(player1_list[i])
remov_cards()
if len(player1_list) == 0:
break
elif 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 = 3
last_cards.clear()
for i in playerCards3:
last_cards.append(i)
print("电脑二号是地主,剩余%d张牌,出的牌是:" % len(player3_list))
jude(playerCards3)
if len(player3_list) == 0:
break
print_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 = 1
last_cards.clear()
for i in trans(put_ind):
last_cards.append(player1_list[i])
remov_cards()
if len(player1_list) == 0:
break
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 = 2
last_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("你赢了!")