python代码实现⽃地主# -*- coding: utf-8 -*-
"""
Created on Sat Feb 27 12:45:14 2021
@author: lenovo
"""
import random
from collections import Counter
class Player():
def__init__(self, card, role):
self.card = card
self.length =len(self.card)
> 理牌
def clear(self):
for i in range(1, self.length):
j = i -1
if(self.card[i]< self.card[j]):
temp = self.card[i]
self.card[i]= self.card[j]
j = j-1
while j>=0and self.card[j]> temp:
self.card[j+1]= self.card[j]
j = j-1
self.card[j+1]= temp
return self.card
> 随机出牌
def play(self, n, x):
### 出单牌
if n ==1:
result =[1]+[x]*1
return result
### 出对⼦
elif n ==unt[x]>=2:
result =[2]+[x]*2
return result
### 出三带⼀
elif n ==unt[x]>=3:
a =0
while a not in self.card and a != x:
a = random.randint(3,17)
result =[3]+[x]*3+[a]
return result
### 出
elif n ==unt[x]==4:
result =[4]+[x]*4
return result
### 出顺⼦
elif n ==5and[x+1, x+2, x+3, x+4]in self.card:
result =[5]+[x, x+1, x+2, x+3, x+4]
flag =0
i =5
while x+i in self.card:
flag = random.randint(0,1)
if flag ==1:
result.append(x+i)
elif flag ==0:
breakrandom翻译
i = i +1
return result
else:
return[0]
def first_play(self):
result =[0]
while result ==[0]:
n = random.randint(1,5)
x =0
while x not in self.card:
x = random.randint(3,17)
result = self.play(n, x)
for x in result[1:]:
ve(x)
return result
def next_play(self, table):
n = table[-1][0]
x =0
result =[0]
i =0
while result ==[0]or x not in self.card:            x = random.randint(table[-1][1],17)            result = self.play(n, x)
i = i +1
if i ==30:
return table[-1]
for x in result[1:]:
ve(x)
return result
cards =[]
for i in range(3,16):
cards = cards +[i]*4
cards = cards +[16,17]
> 发牌
def deal():
card =[]
for i in range(0,17):
p = random.randint(0,len(cards)-1)        temp = cards.pop(p)
card.append(temp)
return card
card_1 = deal()
card_2 = deal()
card_3 = deal()
> 选地主
lord = random.randint(1,3)
if lord ==1:
card_1 = card_1 + cards
if lord ==2:
card_2 = card_2 + cards
if lord ==3:
card_3 = card_3 + cards
player_1 = Player(card_1,0)
player_2 = Player(card_2,0)
player_3 = Player(card_3,0)
> 理牌
player_1.clear()
player_2.clear()
player_3.clear()
> 显⽰
print("玩家⼀:"+str(card_1))
print("玩家⼆:"+str(card_2))
print("玩家三:"+str(card_3))
print("地主:"+str(lord)+'\n')
> 定义出过的牌
table =[]
> 定义顺序
order =[player_1, player_2, player_3]
leader = lord -1
while len(card_1)!=0and len(card_2)!=0and len(card_3)!=0:    p =0
i =0
table.append(order[leader-1].first_play())
print('\n'+"头家:"+str(leader+1))
print("出牌:"+str(table[-1]))
while len(card_1)!=0and len(card_2)!=0and len(card_3)!=0:        table.append(order[(leader+i)%3].next_play(table)) print("出牌:"+str(table[-1]))
i = i +1
if table[-2]== table[-1]:
p = p +1
else: p =0
if p ==2:
leader =(leader+i-2)%3
break
if len(order[lord-1].card)==0:
print('\n'+"地主胜利!")
else:
print('\n'+"农民胜利!")

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论