用python写一个有着10人角色的狼人杀程序中文版
时间: 2023-06-11 18:06:31 浏览: 235
好的,这里是一个简单的狼人杀程序中文版,使用Python实现:
```python
import random
# 角色类
class Role:
def __init__(self, name, camp):
self.name = name # 角色名称
self.camp = camp # 角色阵营
def __str__(self):
return self.name
# 狼人类,继承自角色类
class Werewolf(Role):
def __init__(self):
super().__init__('狼人', '狼人')
# 狼人杀人
def kill(self, players):
print('狼人请杀人:')
for i, player in enumerate(players):
print(f'{i + 1}. {player}')
target = int(input()) - 1
print(f'狼人选择杀死了{players[target]}')
return players[target]
# 村民类,继承自角色类
class Villager(Role):
def __init__(self):
super().__init__('村民', '好人')
# 预言家类,继承自角色类
class Prophet(Role):
def __init__(self):
super().__init__('预言家', '好人')
# 预言家验人
def verify(self, players):
print('预言家请验人:')
for i, player in enumerate(players):
print(f'{i + 1}. {player}')
target = int(input()) - 1
if players[target].camp == '狼人':
print(f'{players[target]}是狼人')
else:
print(f'{players[target]}不是狼人')
# 猎人类,继承自角色类
class Hunter(Role):
def __init__(self):
super().__init__('猎人', '好人')
# 猎人开枪
def shoot(self, players):
print('猎人请开枪:')
for i, player in enumerate(players):
print(f'{i + 1}. {player}')
target = int(input()) - 1
print(f'猎人开枪了{players[target]}')
return players[target]
# 丘比特类,继承自角色类
class Cupid(Role):
def __init__(self):
super().__init__('丘比特', '好人')
# 丘比特连情侣
def couple(self, players):
print('丘比特请连情侣:')
for i, player in enumerate(players):
print(f'{i + 1}. {player}')
target1 = int(input()) - 1
target2 = int(input()) - 1
print(f'丘比特连成了情侣:{players[target1]}和{players[target2]}')
return [players[target1], players[target2]]
# 守卫类,继承自角色类
class Guard(Role):
def __init__(self):
super().__init__('守卫', '好人')
# 守卫守人
def guard(self, players):
print('守卫请守人:')
for i, player in enumerate(players):
print(f'{i + 1}. {player}')
target = int(input()) - 1
print(f'守卫守住了{players[target]}')
return players[target]
# 游戏类
class Game:
def __init__(self, num_players):
self.num_players = num_players
self.players = [] # 玩家列表
self.roles = [] # 角色列表
self.alive = [] # 存活玩家列表
self.dead = [] # 死亡玩家列表
# 初始化游戏
def init_game(self):
# 分配角色
num_werewolves = self.num_players // 4 # 狼人数量
num_villagers = self.num_players - num_werewolves - 3 # 村民数量(预言家、猎人、守卫)
self.roles = [Werewolf() for _ in range(num_werewolves)] # 狼人
self.roles += [Prophet(), Hunter(), Guard()] # 预言家、猎人、守卫
self.roles += [Villager() for _ in range(num_villagers)] # 村民
random.shuffle(self.roles) # 打乱角色顺序
# 创建玩家
for i in range(self.num_players):
self.players.append(f'玩家{i + 1}')
random.shuffle(self.players) # 打乱玩家顺序
# 分配角色给玩家
for i in range(self.num_players):
role = self.roles.pop()
self.players[i] = (self.players[i], role)
self.alive = self.players.copy() # 存活玩家列表初始化
# 丘比特连情侣
cupid = [p for p in self.players if p[1].name == '丘比特'][0]
lovers = cupid[1].couple(self.players)
for player in self.players:
if player[1].name == '丘比特':
continue
if player[1] in lovers:
player[1].lover = lovers[1] if player[1] == lovers[0] else lovers[0]
# 游戏主循环
def play(self):
while True:
# 狼人阶段
print('========== 狼人阶段 ==========')
werewolves = [p for p in self.alive if p[1].name == '狼人']
target = werewolves[0][1].kill(self.alive)
self.dead.append(target)
self.alive.remove(target)
if len([p for p in self.alive if p[1].camp == '好人']) == 0:
print('狼人获胜')
break
# 好人阶段
print('========== 好人阶段 ==========')
while True:
if len([p for p in self.alive if p[1].name == '预言家']) > 0:
prophet = [p for p in self.alive if p[1].name == '预言家'][0]
prophet.verify(self.alive)
if len([p for p in self.alive if p[1].name == '猎人']) > 0:
hunter = [p for p in self.alive if p[1].name == '猎人'][0]
if len([p for p in self.alive if p[1].name == '守卫']) > 0:
guard = [p for p in self.alive if p[1].name == '守卫'][0]
guard_target = guard.guard(self.alive)
else:
guard_target = None
if hunter in self.dead: # 如果猎人已经死亡,则跳过猎人阶段
break
if guard_target in self.dead: # 如果守卫守的人已经死亡,则跳过守卫阶段
guard_target = None
print('好人请发言:')
for i, player in enumerate(self.alive):
print(f'{i + 1}. {player[0]}')
vote = int(input('请投票:')) - 1
print(f'{self.alive[vote][0]}被投票出局了')
self.dead.append(self.alive[vote])
self.alive.remove(self.alive[vote])
if len([p for p in self.alive if p[1].camp == '狼人']) == 0:
print('好人获胜')
break
# 猎人阶段
print('========== 猎人阶段 ==========')
if hunter not in self.dead:
hunter_target = hunter.shoot(self.alive)
self.dead.append(hunter)
self.alive.remove(hunter)
if hunter_target.camp == '狼人':
print(f'{hunter_target}是狼人')
self.dead.append(hunter_target)
self.alive.remove(hunter_target)
if len([p for p in self.alive if p[1].camp == '狼人']) == 0:
print('好人获胜')
break
# 夜晚结束,输出死亡者名单
print('========== 夜晚结束 ==========')
print('死亡者名单:')
for player in self.dead:
print(player[0], end=' ')
print()
# 游戏开始
num_players = 10 # 玩家数量
game = Game(num_players)
game.init_game()
game.play()
```
该程序实现了狼人杀游戏的基本流程,包括角色分配、狼人杀人、预言家验人、守卫守人、猎人开枪等,同时支持中文界面。程序中使用了面向对象的编程思想,将每个角色封装为一个类,并且使用了继承来避免重复代码。
阅读全文