斗地主3
程序员文章站
2022-03-07 16:57:13
#coding=utf8import random,timeendcards=[]cardlist1=[ "spade1", "club1", "heart1", "diamond1", "spade2","club2","heart2","diamond2","spade3","club3","h ......
#coding=utf8
import random,time
endcards=[]
cardlist1=[
"spade1", "club1", "heart1", "diamond1", "spade2","club2","heart2","diamond2","spade3","club3","heart3","diamond3",
"spade4", "club4", "heart4", "diamond4", "spade5","club5","heart5","diamond5","spade6","club6","heart6","diamond6",
"spade7", "club7", "heart7", "diamond7", "spade8","club8","heart8","diamond8","spade9","club9","heart9","diamond9",
"spade10","club10","heart10","diamond10","spadeJ","clubJ","heartJ","diamondJ","spadeQ","clubQ","heartQ","diamondQ",
"spadeK", "clubK", "heartK", "diamondK", "queen","king"
]
cardlist2={
"spade3":3, "club3":3, "heart3":3, "diamond3":3, "spade4":4, "club4":4, "heart4":4, "diamond4":4,
"spade5":5, "club5":5, "heart5":5, "diamond5":5, "spade6":6, "club6":6, "heart6":6, "diamond6":6,
"spade7":7, "club7":7, "heart7":7, "diamond7":7, "spade8":8, "club8":8, "heart8":8, "diamond8":8,
"spade9":9, "club9":9, "heart9":9, "diamond9":9, "spade10":10,"club10":10,"heart10":10,"diamond10":10,
"spadeJ":11,"clubJ":11,"heartJ":11,"diamondJ":11,"spadeQ":12, "clubQ":12, "heartQ":12,"diamondQ":12,
"spadeK":13,"clubK":13,"heartK":13,"diamondK":13,"spade1":14, "club1":14, "heart1":14, "diamond1":14,
"spade2":15,"club2":15,"heart2":15,"diamond2":15,"queen":16,"king":17
}
#洗牌
def myshuffle():
random.shuffle(cardlist1)
#随机地主
def randDZ():
global player1,player2,player3
print "三张底牌是:%s" % sancards
DZ = random.randint(1, 3)
if DZ == 1:
print "玩家1是地主,玩家2是农民,玩家3是农民"
player1 = player1 + sancards
for i in range(0, 20):
for j in range(i + 1, 20):
if cardlist2[player1[i]] < cardlist2[player1[j]]:
player1[i], player1[j] = player1[j], player1[i]
# print "地主的牌是:%s" % player1
# print "玩家2的牌是:%s" % player2
# print "玩家3的牌是:%s" % player3
elif DZ == 2:
print "玩家2是地主,玩家1是农民,玩家3是农民"
player2 = player2 + sancards
for i in range(0, 20):
for j in range(i + 1, 20):
if cardlist2[player2[i]] < cardlist2[player2[j]]:
player2[i], player2[j] = player2[j], player2[i]
# print "地主的牌是:%s" % player2
# print "玩家1的牌是:%s" % player1
# print "玩家3的牌是:%s" % player3
elif DZ == 3:
print "玩家3是地主,玩家1是农民,玩家2是农民"
player3 = player3 + sancards
for i in range(0, 20):
for j in range(i + 1, 20):
if cardlist2[player3[i]] < cardlist2[player3[j]]:
player3[i], player3[j] = player3[j], player3[i]
# print "地主的牌是:%s" % player3
# print "玩家1的牌是:%s" % player1
# print "玩家2的牌是:%s" % player2
#判断牌型
def cardtype(delplayer):
cardnums=len(delplayer)
while cardnums<=4:
if cardnums == 1:
print "玩家出的单牌是:%s" % delplayer[0]
break
elif cardnums == 2:
if cardlist2[delplayer[0]] == cardlist2[delplayer[1]]:
print "玩家出的对%s" %delplayer[0]
break
elif cardnums == 3:
if cardlist2[delplayer[0]] == cardlist2[delplayer[1]] == cardlist2[delplayer[2]]:
print "玩家出的三张%s" %delplayer[0]
break
elif cardnums == 4:
if cardlist2[delplayer[0]] == cardlist2[delplayer[1]] == cardlist2[delplayer[2]]==cardlist2[delplayer[3]]:
print "玩家出了炸%s" %delplayer[0]
break
else:
raw_input()
#打牌
def dapai():
global endcards
print " 开始打牌,打牌顺序为玩家1>玩家2>玩家3轮流"
while len(player1)>0 and len(player2)>0 and len(player3)>0:
if len(endcards) > 0:
print "玩家1开始打牌"
print "玩家1的牌是:%s" % player1
delplayer1 = raw_input("请选择玩家1要出的牌:").split(" ")
if len(endcards)==1 and len(delplayer1)==len(endcards) and cardlist2[delplayer1[0]]>cardlist2[endcards[0]]:
print "玩家出了单张%s"%delplayer1[0]
for i in range(len(delplayer1)): # 每次出的牌从原先的牌删除
player1.remove(delplayer1[i])
elif len(endcards)==2 and len(delplayer1)==len(endcards) and cardlist2[delplayer1[0]]>cardlist2[endcards[0]] and cardlist2[delplayer1[0]]==cardlist2[delplayer1[1]]:
print "玩家2出了对%s"%delplayer1[0]
for i in range(len(delplayer1)): # 每次出的牌从原先的牌删除
player1.remove(delplayer1[i])
elif len(endcards)==3 and len(delplayer1)==len(endcards) and cardlist2[delplayer1[0]]>cardlist2[endcards[0]] and cardlist2[delplayer1[0]] == cardlist2[delplayer1[1]] == cardlist2[delplayer1[2]]:
print "玩家2出了三张%s"%delplayer1[0]
for i in range(len(delplayer1)): # 每次出的牌从原先的牌删除
player1.remove(delplayer1[i])
elif len(endcards)==4 and len(delplayer1)==len(endcards) and cardlist2[delplayer1[0]]>cardlist2[endcards[0]] and cardlist2[delplayer1[0]] == cardlist2[delplayer1[1]] == cardlist2[delplayer1[2]]==cardlist2[delplayer1[3]]:
print "玩家2出了四张%s"%delplayer1[0]
for i in range(len(delplayer1)): # 每次出的牌从原先的牌删除
player1.remove(delplayer1[i])
else:
pass
del endcards[:]
if (len(player1) == 0):
print "玩家1为赢家,游戏结束"
break
print "玩家2开始出牌"
print "玩家2的牌是:%s" % player2
delplayer2= raw_input("请选择玩家2要出的牌:").split(" ")
if len(delplayer1)==1 and len(delplayer1)==len(delplayer2) and cardlist2[delplayer2[0]]>cardlist2[delplayer1[0]]:
print "玩家出了单张%s"%delplayer2[0]
for i in range(len(delplayer2)): # 每次出的牌从原先的牌删除
player2.remove(delplayer2[i])
elif len(delplayer1)==2 and len(delplayer1)==len(delplayer2) and cardlist2[delplayer2[0]]>cardlist2[delplayer1[0]] and cardlist2[delplayer2[0]]==cardlist2[delplayer2[1]]:
print "玩家2出了对%s"%delplayer2[0]
for i in range(len(delplayer2)): # 每次出的牌从原先的牌删除
player2.remove(delplayer2[i])
elif len(delplayer1)==3 and len(delplayer1)==len(delplayer2) and cardlist2[delplayer2[0]]>cardlist2[delplayer1[0]] and cardlist2[delplayer2[0]] == cardlist2[delplayer2[1]] == cardlist2[delplayer2[2]]:
print "玩家2出了三张%s"%delplayer2[0]
for i in range(len(delplayer2)): # 每次出的牌从原先的牌删除
player2.remove(delplayer2[i])
elif len(delplayer1)==4 and len(delplayer1)==len(delplayer2) and cardlist2[delplayer2[0]]>cardlist2[delplayer1[0]] and cardlist2[delplayer2[0]] == cardlist2[delplayer2[1]] == cardlist2[delplayer2[2]]==cardlist2[delplayer2[3]]:
print "玩家2出了四张%s"%delplayer2[0]
for i in range(len(delplayer2)): # 每次出的牌从原先的牌删除
player2.remove(delplayer2[i])
else:
pass
del delplayer2[:]
if (len(player2) == 0):
print "玩家2为赢家,游戏结束"
break
print "玩家3开始出牌"
print "玩家3的牌是:%s" % player3
delplayer3 = raw_input("请选择玩家3要出的牌:").split(" ")
if len(delplayer2)==1 and len(delplayer2)==len(delplayer3) and cardlist2[delplayer3[0]]>cardlist2[delplayer2[0]]:
print "玩家出了单张%s"%delplayer3[0]
for i in range(len(delplayer3)): # 每次出的牌从原先的牌删除
player3.remove(delplayer3[i])
elif len(delplayer2)==2 and len(delplayer2)==len(delplayer3) and cardlist2[delplayer3[0]]>cardlist2[delplayer2[0]] and cardlist2[delplayer3[0]]==cardlist2[delplayer3[1]]:
print "玩家2出了对%s"%delplayer3[0]
for i in range(len(delplayer3)): # 每次出的牌从原先的牌删除
player3.remove(delplayer3[i])
elif len(delplayer2)==3 and len(delplayer2)==len(delplayer3) and cardlist2[delplayer3[0]]>cardlist2[delplayer2[0]] and cardlist2[delplayer3[0]] == cardlist2[delplayer3[1]] == cardlist2[delplayer3[2]]:
print "玩家2出了三张%s"%delplayer3[0]
for i in range(len(delplayer3)): # 每次出的牌从原先的牌删除
player3.remove(delplayer3[i])
elif len(delplayer2)==4 and len(delplayer2)==len(delplayer3) and cardlist2[delplayer3[0]]>cardlist2[delplayer2[0]] and cardlist2[delplayer3[0]] == cardlist2[delplayer3[1]] == cardlist2[delplayer3[2]]==cardlist2[delplayer3[3]]:
print "玩家2出了四张%s"%delplayer3[0]
for i in range(len(delplayer3)): # 每次出的牌从原先的牌删除
player3.remove(delplayer3[i])
else:
pass
endcards=delplayer3
del delplayer3[:]
if (len(player3) == 0):
print "玩家3为赢家,游戏结束"
continue
#发牌,指定玩家1,玩家2,玩家3
def deal():
print " 开始洗牌!"
myshuffle()
print "loading..............."
time.sleep(3)
print " 洗牌完毕"
print " 开始发牌!"
print "loading................"
time.sleep(5)
global sancards
sancards=random.sample(cardlist1,3)
print "三张底牌是:%s"%sancards
cardlist1.remove(sancards[0]),cardlist1.remove(sancards[1]),cardlist1.remove(sancards[2])
global player1
player1=random.sample(cardlist1,17)
#将玩家1的牌重新按降序排列
for i in range(0,17):
for j in range(i+1,17):
if cardlist2[player1[i]]<cardlist2[player1[j]]:
player1[i],player1[j]=player1[j],player1[i]
print "玩家1的牌是:%s"%player1
for i in range(0,17):
cardlist1.remove(player1[i])
global player2
player2=random.sample(cardlist1,17)
# 将玩家2的牌重新按降序排列
for i in range(0,17):
for j in range(i+1,17):
if cardlist2[player2[i]]<cardlist2[player2[j]]:
player2[i],player2[j]=player2[j],player2[i]
print "玩家2的牌是:%s"%player2
for i in range(0,17):
cardlist1.remove(player2[i])
global player3
player3=random.sample(cardlist1,17)
# 将玩家3的牌重新按降序排列
for i in range(0, 17):
for j in range(i + 1, 17):
if cardlist2[player3[i]] < cardlist2[player3[j]]:
player3[i], player3[j] = player3[j], player3[i]
print "玩家3的牌是:%s"%player3
print " 发牌完毕,开始随机地主。"
randDZ()
dapai()
deal()
上一篇: 美团.点评服务治理框架