用python写一个有着10人角色的狼人杀程序

时间: 2023-06-11 11:06:43 浏览: 109
好的,以下是一个简单的 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 ``` 这个程序中有狼人阶段、预言家阶段、女巫阶段和白天阶段。在每个阶段中,程序会根据角色的不同,执行不同的操作。在每个阶段中,程序还会判断游戏是否结束,如果游戏结束,程序会输出获胜的角色。

相关推荐

最新推荐

recommend-type

人机交互程序 python实现人机对话

主要为大家详细介绍了人机交互程序,初步实现python人机对话,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

基于Python的一个自动录入表格的小程序

主要介绍了基于Python的一个自动录入表格的小程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

一个简单的python程序实例(通讯录)

/usr/bin/python#Filename:friendbook.pyimport cPickle as pimport sysimport timeimport os ab={‘Xdex’:’cneds@fnedf.com’, ‘Laexly’:’fev@fe.com’, ‘Fukc’:’fexok@ver.com’, ‘Stifu’:’stif@qq....
recommend-type

python杀死一个线程的方法

由于python线程没有提供abort方法,所以我们需要自己想办法解决此问题,面对这一问题,小编帮大家解决phthon杀死一个线程的方法,需要的朋友一起来学习吧
recommend-type

使用Python写一个量化股票提醒系统

主要介绍了小白用Python写了一个股票提醒系统,迷你版量化系统,完美的实现了实时提醒功能,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。