用python制作斗地主游戏
时间: 2024-01-06 16:05:44 浏览: 100
好的,制作斗地主游戏可以分为以下几个步骤:
1. 定义牌的类型和数量:一副牌包括54张牌,分别为13张点数牌(2-A),4张王牌(小王、大王)和一张花牌(一般为红桃10)。
2. 初始化牌堆:将54张牌打乱顺序并放入牌堆中。
3. 发牌:将牌堆中的牌平均分给3个玩家。
4. 确定地主:随机选择一名玩家作为地主,该玩家可以获得底牌,并且可以在出牌时先出牌。
5. 出牌:地主先出牌,其他玩家按照顺序出牌,每次出牌必须比上一个玩家出的牌大或者相同。
6. 判断胜负:当有玩家出完牌或者其他玩家无法跟牌时,比较三个玩家的牌型,判断胜负并计算得分。
下面是一个简单的斗地主游戏的实现代码,供参考:
```python
import random
# 定义牌的类型和数量
cards = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '小王', '大王']
card_nums = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54]
# 初始化牌堆
def init_cards():
cards_list = cards*4 + ['花牌']
return random.sample(cards_list, len(cards_list))
# 发牌
def deal_cards(cards):
player1 = sorted(cards[:17], key=lambda x: card_nums[cards.index(x)])
player2 = sorted(cards[17:34], key=lambda x: card_nums[cards.index(x)])
player3 = sorted(cards[34:51], key=lambda x: card_nums[cards.index(x)])
bottom = sorted(cards[51:], key=lambda x: card_nums[cards.index(x)])
return player1, player2, player3, bottom
# 判断牌型
def get_card_type(cards):
length = len(cards)
if length == 1:
return '单牌'
elif length == 2:
if cards[0] == '小王' and cards[1] == '大王':
return '王炸'
elif card_nums[cards.index(cards[0])] == card_nums[cards.index(cards[1])]:
return '对子'
else:
return '无效牌型'
elif length == 3:
if card_nums[cards.index(cards[0])] == card_nums[cards.index(cards[1])] and card_nums[cards.index(cards[1])] == card_nums[cards.index(cards[2])]:
return '三张'
else:
return '无效牌型'
elif length == 4:
if card_nums[cards.index(cards[0])] == card_nums[cards.index(cards[1])] and card_nums[cards.index(cards[1])] == card_nums[cards.index(cards[2])] and card_nums[cards.index(cards[2])] == card_nums[cards.index(cards[3])]:
return '炸弹'
else:
return '无效牌型'
elif length == 5:
if card_nums[cards.index(cards[0])] == card_nums[cards.index(cards[1])] and card_nums[cards.index(cards[1])] == card_nums[cards.index(cards[2])] and card_nums[cards.index(cards[3])] == card_nums[cards.index(cards[4])]:
return '三带二'
else:
return '无效牌型'
elif length >= 6 and length <= 12:
types = ['顺子', '连对', '飞机']
index = length // 2 - 3
if length % 2 == 0 and length % 3 == 0 and card_nums[cards.index(cards[0])] == card_nums[cards.index(cards[2])]-2 and card_nums[cards.index(cards[2])] == card_nums[cards.index(cards[4])]-2:
return types[index]
elif length % 2 == 0 and card_nums[cards.index(cards[0])] == card_nums[cards.index(cards[1])]-1 and card_nums[cards.index(cards[1])] == card_nums[cards.index(cards[2])]-1 and card_nums[cards.index(cards[-1])] == card_nums[cards.index(cards[-2])]+1:
return types[index]
elif length % 3 == 0 and card_nums[cards.index(cards[0])] == card_nums[cards.index(cards[2])]-2 and card_nums[cards.index(cards[2])] == card_nums[cards.index(cards[4])]-2 and card_nums[cards.index(cards[4])] == card_nums[cards.index(cards[5])]+1 and card_nums[cards.index(cards[-1])] == card_nums[cards.index(cards[-2])]+1:
return types[index]
else:
return '无效牌型'
else:
return '无效牌型'
# 判断牌型大小
def compare_cards(player_cards, last_cards):
player_type = get_card_type(player_cards)
last_type = get_card_type(last_cards)
if player_type == '无效牌型' or last_type == '无效牌型':
return False
elif player_type == '王炸':
return True
elif player_type != last_type:
return False
elif len(player_cards) != len(last_cards):
return False
else:
if card_nums[player_cards.index(max(player_cards, key=lambda x: card_nums[x]))] > card_nums[last_cards.index(max(last_cards, key=lambda x: card_nums[x]))]:
return True
else:
return False
# 玩游戏
def play_game():
cards = init_cards()
player1, player2, player3, bottom = deal_cards(cards)
players = [player1, player2, player3]
last_cards = []
landlord = random.randint(0, 2)
while True:
for i in range(3):
player = (landlord + i) % 3
print('玩家%d出牌:' % (player+1))
print('手牌:', players[player])
if len(last_cards) == 0:
card_str = input('请出牌(空格隔开):')
else:
card_str = input('请出牌(空格隔开), 上家出牌:%s:' % last_cards)
if card_str == '':
print('不出')
else:
cards = card_str.split(' ')
cards = sorted(cards, key=lambda x: card_nums[player1.index(x)])
if compare_cards(cards, last_cards):
for card in cards:
players[player].remove(card)
last_cards = cards
print('出牌成功!')
if len(players[player]) == 0:
print('玩家%d赢得胜利!' % (player+1))
return
else:
print('出牌失败!')
print('-'*50)
if __name__ == '__main__':
play_game()
```
这个实现只是一个简单的示例,还有很多细节需要完善,如牌型判断、分数计算等。希望能对你有所帮助!
阅读全文