写一个python斗地主小游戏
时间: 2024-05-17 12:14:09 浏览: 172
欢乐斗地主python项目游戏源码下载
好的,下面是一个简单的斗地主小游戏的Python代码:
```python
import random
# 定义一副扑克牌
poker = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] * 4
poker.append('小王')
poker.append('大王')
# 定义洗牌函数
def shuffle_poker(poker):
random.shuffle(poker)
# 定义发牌函数
def deal_poker(poker):
player1 = poker[0:17]
player2 = poker[17:34]
player3 = poker[34:51]
bottom = poker[51:]
return player1, player2, player3, bottom
# 定义牌型大小比较函数
def compare_poker(poker1, poker2):
poker_dict = {'3': 0, '4': 1, '5': 2, '6': 3, '7': 4, '8': 5, '9': 6, '10': 7, 'J': 8, 'Q': 9, 'K': 10, 'A': 11, '2': 12, '小王': 13, '大王': 14}
if len(poker1) == len(poker2):
if poker_dict[poker1[0]] > poker_dict[poker2[0]]:
return True
else:
return False
else:
return False
# 定义出牌函数
def play_poker(poker_lst, last_poker=None):
if last_poker:
print('上家出牌:', last_poker)
else:
print('轮到你出牌!')
poker_str = input('请出牌(用空格分隔,不出请输“不要”):')
if poker_str == '不要':
return None
else:
poker = poker_str.split()
if len(poker) != len(last_poker) and last_poker:
print('出牌不符合规则,请重新出牌!')
return play_poker(poker_lst, last_poker)
else:
for p in poker:
if p not in poker_lst:
print('出牌不符合规则,请重新出牌!')
return play_poker(poker_lst, last_poker)
for p in poker:
poker_lst.remove(p)
return poker
# 定义判断游戏是否结束函数
def is_game_over(player1, player2, player3):
if len(player1) == 0:
return True, 'player1'
elif len(player2) == 0:
return True, 'player2'
elif len(player3) == 0:
return True, 'player3'
else:
return False, None
# 定义主函数
def main():
shuffle_poker(poker)
player1, player2, player3, bottom = deal_poker(poker)
players = {'player1': player1, 'player2': player2, 'player3': player3}
order = ['player1', 'player2', 'player3']
game_over = False
last_poker = None
while not game_over:
for p in order:
print(p, '的牌:', players[p])
poker = play_poker(players[p], last_poker)
if poker:
last_poker = poker
else:
print(p, '不要了!')
if len(players[p]) == 0:
game_over, winner = is_game_over(player1, player2, player3)
if game_over:
print('游戏结束,', winner, '赢了!')
break
print('-----------------')
if __name__ == '__main__':
main()
```
运行以上代码,即可开始玩斗地主小游戏。注意,这只是一个简单的实现,游戏规则可能会和真实斗地主游戏略有不同。
阅读全文