# -*- 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.role = role
self.length = len(self.card)
self.count = Counter(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>=0 and 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):
self.count = Counter(self.card)
### 出单牌
if n == 1:
result = [1] + [x] * 1
return result
### 出对子
elif n == 2 and self.count[x] >= 2:
result = [2] + [x] * 2
return result
### 出三带一
elif n == 3 and self.count[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 == 4 and self.count[x] == 4:
result = [4] + [x] * 4
return result
### 出顺子
elif n == 5 and [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:
break
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:]:
self.card.remove(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:]:
self.card.remove(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) != 0 and len(card_2) != 0 and 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) != 0 and len(card_2) != 0 and 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' + "农民胜利!")
python代码实现斗地主
最新推荐文章于 2024-05-19 08:30:00 发布