python地主游戏的逻辑
时间: 2023-08-29 13:05:49 浏览: 38
Python地主游戏的逻辑可以分为以下几个部分:
1. 游戏初始化:包括生成一副牌、洗牌、发牌、确定地主等。
2. 牌型判断:根据玩家出牌的牌型进行判断是否符合规则。
3. 出牌顺序:根据当前出牌的玩家、上一次出牌的玩家以及上一轮出牌的牌型,确定当前玩家是否有权出牌,以及出牌的牌型是否合法。
4. 策略决策:根据当前手牌的情况、出牌的情况以及地主的身份,决定出牌策略。
5. 游戏结束:当某一玩家出完牌或者其他玩家放弃出牌时,游戏结束,计算得分并确定胜负。
下面是一个简单的Python地主游戏逻辑示例代码,供参考:
```python
import random
# 生成一副牌
cards = []
for i in range(3, 16):
cards += [str(i)] * 4
cards += ['16'] * 2
cards += ['17'] * 2
# 洗牌
random.shuffle(cards)
# 发牌
player1 = cards[:17]
player2 = cards[17:34]
player3 = cards[34:51]
landlord_cards = cards[51:54]
# 确定地主
landlord = None
for i in range(3):
print('玩家{}的牌是:{}'.format(i+1, eval('player'+str(i+1))))
if landlord is None:
choice = input('玩家{},是否要当地主?(y/n)'.format(i+1))
if choice == 'y':
landlord = i+1
eval('player'+str(i+1)).extend(landlord_cards)
if landlord is not None:
print('玩家{}成为地主,底牌是:{}'.format(landlord, landlord_cards))
break
# 牌型判断
def is_valid(cards):
if len(cards) == 1:
return True
if len(cards) == 2 and cards[0] == cards[1]:
return True
if len(cards) == 3 and cards[0] == cards[1] and cards[1] == cards[2]:
return True
if len(cards) == 4 and cards[0] == cards[1] and cards[1] == cards[2] and cards[2] == cards[3]:
return True
if len(cards) == 5 and cards[0] == cards[1] and cards[1] == cards[2] and cards[2] == cards[3] and cards[3] == cards[4]:
return True
if len(cards) == 6 and ((cards[0] == cards[1] and cards[1] == cards[2] and cards[3] == cards[4] and cards[4] == cards[5]) or (cards[0] == cards[1] and cards[1] == cards[2] and cards[2] == cards[3] and cards[4] == cards[5])):
return True
if len(cards) == 7 and cards[0] == cards[1] and cards[1] == cards[2] and cards[2] == cards[3] and cards[3] == cards[4] and cards[4] == cards[5] and cards[5] == cards[6]:
return True
if len(cards) == 8 and ((cards[0] == cards[1] and cards[1] == cards[2] and cards[2] == cards[3] and cards[4] == cards[5] and cards[5] == cards[6] and cards[6] == cards[7]) or (cards[0] == cards[1] and cards[1] == cards[2] and cards[2] == cards[3] and cards[3] == cards[4] and cards[5] == cards[6] and cards[6] == cards[7])):
return True
if len(cards) == 9 and cards[0] == cards[1] and cards[1] == cards[2] and cards[2] == cards[3] and cards[3] == cards[4] and cards[4] == cards[5] and cards[5] == cards[6] and cards[6] == cards[7] and cards[7] == cards[8]:
return True
return False
# 出牌顺序
order = [landlord, landlord % 3 + 1, (landlord + 1) % 3 + 1]
last_player = None
last_cards = None
while len(player1) > 0 and len(player2) > 0 and len(player3) > 0:
for i in range(3):
current_player = order[i]
if last_player is None or current_player == last_player or len(eval('player'+str(current_player))) == 0:
continue
print('当前出牌玩家:{}'.format(current_player))
print('上一个出牌玩家:{}'.format(last_player))
print('上一轮出牌:{}'.format(last_cards))
cards = input('玩家{},请输入你要出的牌:'.format(current_player)).split()
if cards == []:
print('玩家{}放弃出牌'.format(current_player))
continue
cards = sorted(cards)
if last_player is not None and not is_valid(cards):
print('出牌不符合规则,请重新出牌')
continue
if last_player is not None and len(cards) != len(last_cards):
print('出牌不符合规则,请重新出牌')
continue
if last_player is not None and eval('player'+str(current_player)).count(cards[0]) < len(cards):
print('出牌不符合规则,请重新出牌')
continue
last_player = current_player
last_cards = cards
eval('player'+str(current_player)) = list(set(eval('player'+str(current_player))) - set(cards))
if len(eval('player'+str(current_player))) == 0:
print('玩家{}已出完所有牌'.format(current_player))
break
# 结束游戏
if len(player1) == 0:
print('玩家1获胜')
elif len(player2) == 0:
print('玩家2获胜')
elif len(player3) == 0:
print('玩家3获胜')
```