用python写一个有着10人角色的狼人杀程序
时间: 2023-06-11 19:06:43 浏览: 213
好的,以下是一个简单的 Python 狼人杀程序,有10个角色:3个狼人、3个平民、2个预言家、1个女巫和1个猎人。代码如下:
```python
import random
# 初始化角色
characters = ['wolf'] * 3 + ['villager'] * 3 + ['seer'] * 2 + ['witch'] + ['hunter']
random.shuffle(characters)
# 初始化局面
alive_players = set(range(10))
dead_players = set()
# 游戏开始
while True:
# 狼人阶段
wolf_votes = {}
for i in alive_players:
if characters[i] == 'wolf':
vote = random.choice(list(alive_players - {i}))
if vote in wolf_votes:
wolf_votes[vote] += 1
else:
wolf_votes[vote] = 1
if wolf_votes:
victim = max(wolf_votes, key=wolf_votes.get)
if characters[victim] == 'hunter':
dead_players.add(victim)
alive_players.remove(victim)
print('The hunter killed', victim+1)
else:
dead_players.add(victim)
alive_players.remove(victim)
print('The wolves killed', victim+1)
if len(set(characters[i] for i in alive_players)) == 1:
print('Game over. The', characters[alive_players.pop()], 'win!')
break
# 预言家阶段
for i in alive_players:
if characters[i] == 'seer':
print('Seer,', i+1, ',choose someone to check:')
target = int(input())-1
if characters[target] == 'wolf':
print(target+1, 'is a wolf')
else:
print(target+1, 'is not a wolf')
# 女巫阶段
for i in alive_players:
if characters[i] == 'witch':
print('Witch,', i+1, ',do you want to use the potion (1) or not (0)?')
use_potion = int(input())
if use_potion:
print('Witch,', i+1, ',whom do you want to save?')
target = int(input())-1
if target not in dead_players:
print(target+1, 'is still alive')
else:
dead_players.remove(target)
alive_players.add(target)
print(target+1, 'has been saved')
else:
print('Witch,', i+1, ',whom do you want to poison?')
target = int(input())-1
dead_players.add(target)
alive_players.remove(target)
print(target+1, 'has been poisoned')
if len(set(characters[i] for i in alive_players)) == 1:
print('Game over. The', characters[alive_players.pop()], 'win!')
break
# 白天阶段
print('The game enters the day phase. The dead players are:', dead_players)
if len(set(characters[i] for i in alive_players)) == 1:
print('Game over. The', characters[alive_players.pop()], 'win!')
break
vote_count = {}
for i in alive_players:
print('Player', i+1, ',', characters[i], ',please vote:')
vote = int(input())-1
if vote in vote_count:
vote_count[vote] += 1
else:
vote_count[vote] = 1
if vote_count:
victim = max(vote_count, key=vote_count.get)
if characters[victim] == 'hunter':
dead_players.add(victim)
alive_players.remove(victim)
print('The hunter killed', victim+1)
else:
dead_players.add(victim)
alive_players.remove(victim)
print('Player', victim+1, 'was executed')
if len(set(characters[i] for i in alive_players)) == 1:
print('Game over. The', characters[alive_players.pop()], 'win!')
break
```
这个程序中有狼人阶段、预言家阶段、女巫阶段和白天阶段。在每个阶段中,程序会根据角色的不同,执行不同的操作。在每个阶段中,程序还会判断游戏是否结束,如果游戏结束,程序会输出获胜的角色。
阅读全文