做一个简单的斗地主游戏
时间: 2023-12-26 10:05:14 浏览: 69
好的,我可以给你提供一个简单的斗地主游戏的代码框架,你可以根据自己的需求和喜好进行修改和完善。
```python
import random
# 定义牌组
cards = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2', '小王', '大王']
# 定义牌型权值(仅用于判断大小)
card_values = {'3': 1, '4': 2, '5': 3, '6': 4, '7': 5, '8': 6, '9': 7, '10': 8, 'J': 9, 'Q': 10, 'K': 11, 'A': 12, '2': 13, '小王': 14, '大王': 15}
# 洗牌
def shuffle_cards():
random.shuffle(cards)
# 发牌
def deal_cards():
players = [[], [], []]
for i in range(3):
for j in range(17):
players[i].append(cards.pop(0))
return players
# 判断出牌是否符合规则
def is_valid(cards):
if len(cards) == 0:
return False
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] == cards[2]:
return True
if len(cards) == 4 and cards[0] == cards[1] == cards[2] == cards[3]:
return True
if len(cards) == 5 and (cards[0] == cards[1] == cards[2] == cards[3] or cards[1] == cards[2] == cards[3] == cards[4]):
return True
if len(cards) == 6 and (cards[0] == cards[1] == cards[2] == cards[3] == cards[4] == cards[5] or cards[0] == cards[1] == cards[2] and cards[3] == cards[4] == cards[5]):
return True
if len(cards) == 7 and (cards[0] == cards[1] == cards[2] == cards[3] == cards[4] == cards[5] == cards[6] or cards[0] == cards[1] == cards[2] and cards[3] == cards[4] == cards[5] == cards[6] or cards[0] == cards[1] == cards[2] == cards[3] and cards[4] == cards[5] == cards[6]):
return True
if len(cards) == 8 and (cards[0] == cards[1] == cards[2] == cards[3] == cards[4] == cards[5] == cards[6] == cards[7] or cards[0] == cards[1] == cards[2] and cards[3] == cards[4] == cards[5] == cards[6] == cards[7] or cards[0] == cards[1] == cards[2] == cards[3] and cards[4] == cards[5] == cards[6] == cards[7]):
return True
if len(cards) == 9 and (cards[0] == cards[1] == cards[2] == cards[3] == cards[4] == cards[5] == cards[6] == cards[7] == cards[8] or cards[0] == cards[1] == cards[2] == cards[3] and cards[4] == cards[5] == cards[6] == cards[7] == cards[8] or cards[0] == cards[1] == cards[2] == cards[3] == cards[4] and cards[5] == cards[6] == cards[7] == cards[8]):
return True
return False
# 判断牌型大小,返回 True 表示前者比后者大
def compare(cards1, cards2):
card_type1 = get_card_type(cards1)
card_type2 = get_card_type(cards2)
if card_type1 != card_type2:
return card_type1 > card_type2
if len(cards1) != len(cards2):
return False
return card_values[cards1[0]] > card_values[cards2[0]]
# 获取牌型,返回牌型权值
def get_card_type(cards):
if len(cards) == 1:
return 1
if len(cards) == 2 and cards[0] == cards[1]:
return 2
if len(cards) == 3 and cards[0] == cards[1] == cards[2]:
return 3
if len(cards) == 4 and cards[0] == cards[1] == cards[2] == cards[3]:
return 4
if len(cards) == 5 and (cards[0] == cards[1] == cards[2] == cards[3] or cards[1] == cards[2] == cards[3] == cards[4]):
return 5
if len(cards) == 6 and (cards[0] == cards[1] == cards[2] == cards[3] == cards[4] == cards[5] or cards[0] == cards[1] == cards[2] and cards[3] == cards[4] == cards[5]):
return 6
if len(cards) == 7 and (cards[0] == cards[1] == cards[2] == cards[3] == cards[4] == cards[5] == cards[6] or cards[0] == cards[1] == cards[2] and cards[3] == cards[4] == cards[5] == cards[6] or cards[0] == cards[1] == cards[2] == cards[3] and cards[4] == cards[5] == cards[6]):
return 7
if len(cards) == 8 and (cards[0] == cards[1] == cards[2] == cards[3] == cards[4] == cards[5] == cards[6] == cards[7] or cards[0] == cards[1] == cards[2] and cards[3] == cards[4] == cards[5] == cards[6] == cards[7] or cards[0] == cards[1] == cards[2] == cards[3] and cards[4] == cards[5] == cards[6] == cards[7]):
return 8
if len(cards) == 9 and (cards[0] == cards[1] == cards[2] == cards[3] == cards[4] == cards[5] == cards[6] == cards[7] == cards[8] or cards[0] == cards[1] == cards[2] == cards[3] and cards[4] == cards[5] == cards[6] == cards[7] == cards[8] or cards[0] == cards[1] == cards[2] == cards[3] == cards[4] and cards[5] == cards[6] == cards[7] == cards[8]):
return 9
return 0
# 主程序
shuffle_cards()
players = deal_cards()
landlord = random.randint(0, 2)
last_cards = []
for i in range(3):
print('玩家{}的牌:{}'.format(i+1, ' '.join(players[i])))
print('底牌:{} {} {}'.format(cards[0], cards[1], cards[2]))
print('地主是玩家{}'.format(landlord+1))
players[landlord].extend(cards)
players[landlord].sort(key=lambda x: card_values[x])
for i in range(3):
print('玩家{}的牌:{}'.format(i+1, ' '.join(players[i])))
while True:
for i in range(3):
print('轮到玩家{}出牌了'.format(i+1))
print('上家出的牌:{}'.format(' '.join(last_cards)))
cards = input('请输入你要出的牌(以空格分隔):').split()
if is_valid(cards) and (len(last_cards) == 0 or compare(cards, last_cards)):
players[i] = [card for card in players[i] if card not in cards]
last_cards = cards
print('出牌成功')
if len(players[i]) == 0:
print('玩家{}获胜!'.format(i+1))
exit(0)
else:
print('出牌不符合规则,请重新出牌')
```
这是一个简单的斗地主游戏代码框架,可以实现洗牌、发牌、判断牌型、大小、出牌等功能。你可以根据自己的需求和喜好进行修改和完善。
阅读全文