Hunt the Wumpus第二个版本---多怪兽,多洞穴,洞穴间双向互通
其中,将洞穴连起来的算法要好好体会。
学习构建临时变量列表,确认循环用FOR,非确定循环用 WHILE,并定好退出条件。
from random import choice
cave_numbers = range(0,20)
caves = []
for i in cave_numbers:
caves.append([])
#保证所有洞穴双向连通
unvisited_caves = range(0,20)
visited_caves = [0]
unvisited_caves.remove(0)
while unvisited_caves != []:
i = choice(visited_caves)
if len(caves[i]) >= 3:
continue
next_cave = choice(unvisited_caves)
caves[i].append(next_cave)
caves[next_cave].append(i)
visited_caves.append(next_cave)
unvisited_caves.remove(next_cave)
'''
for number in cave_numbers:
print number, ":", caves[number]
print "-------visited cave------"
'''
print caves
#保证每个洞穴与另外三个洞穴相连
for i in cave_numbers:
while len(caves[i]) < 3:
passage_to = choice(cave_numbers)
caves[i].append(passage_to)
'''
for number in cave_numbers:
print number, ":", caves[number]
print "-------other cave----------"
'''
print caves
#加入怪兽的朋友
wumpus_location = choice(cave_numbers)
wumpus_friend_location = choice(cave_numbers)
player_location = choice(cave_numbers)
while player_location == wumpus_location or player_location == wumpus_friend_location:
player_location = choice(cave_numbers)
print "Welcome to Hunt the Wumpus!"
print "You can see ", len(cave_numbers), "caves"
print "To play, just type the number"
print "of the cave you wish to enter next"
while True:
print "You are in cave ", player_location
print "From here, you can see caves:", caves[player_location]
if wumpus_location in caves[player_location] :
print "I smell a wumpus!"
if wumpus_friend_location in caves[player_location]:
print "I smell an even stinkier wumpus!"
'''
if (player_location == wumpus_location - 1 or
player_location == wumpus_location + 1):
print "I smell a wumpus!"
if (player_location == wumpus_friend_location - 1 or
player_location == wumpus_friend_location + 1):
print "I smell an even stinkier wumpus!"
'''
print "Which cave next?"
player_input = raw_input(">")
if (not player_input.isdigit() or
int(player_input) not in caves[player_location]):
print player_input + "?"
print "That's not a direction that I can see!"
continue
else:
player_location = int(player_input)
if player_location == wumpus_location:
print "Aargh! you got eaten by a wumpus!"
break
if player_location == wumpus_friend_location:
print "Aargh! you got eaten by a wumpus's friend!"
break

Hunt the Wumpus第二个版本---多怪兽,多洞穴,洞穴间双向互通的更多相关文章
- 吃午饭前,按书上的代码写会儿--Hunt the Wumpus第一个版本
有空就要慢慢练起~~~~脑袋动起来是很快乐的事儿....:) <易学PYTHON>演练一遍. from random import choice cave_numbers = range( ...
- Spring Cloud Alibaba发布第二个版本,Spring 发来贺电
还是熟悉的面孔,还是熟悉的味道,不同的是,这次的配方升级了. 今年10月底,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cl ...
- 怪兽z主机豪华版 答问。
我的淘宝店里,怪兽z主机标准版,分经济版本,标准版,豪华版,三个版本.这里给大家详细介绍一下豪华版的概况. 淘宝购买地址:http://item.taobao.com/item.htm?id=3818 ...
- 饼干怪兽和APT攻击
APT攻击就像一个孩子,你通过各种方式窃取他们的大脑要拿出饼干,为了防止恶意攻击,过失作为母亲未能发现和防止饼干盗窃贼如.于她仅仅监视厨房椅子.衣柜门或烤箱门的开启.建立起有效防御目标攻击与APT攻击 ...
- hdu 1026(BFS+输出路径) 我要和怪兽决斗
http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...
- 使用canvas通过js制作一个小型英雄抓怪兽的2D小游戏
首先,这是一个HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- 如何用纯 CSS 创作一只徘徊的果冻怪兽
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdOKQG 可交互视频 ...
- 51nod-1670-打怪兽(递推/组合数学)
1670 打怪兽 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 lyk在玩一个叫做“打怪兽”的游戏.游戏的规则是这样的.lyk一开始会有一个初始 ...
- 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdOKQG 可交互视频 此视频是可 ...
随机推荐
- Windows登录脚本可以限制并发登录吗
在Windows服务器中,使用一个Windows登录脚本来限制并发会话靠谱吗? 事实上,这种解决方案存在很多缺点和弱点,并不能满足大中型IT基础设施的安全性需求. 一.使用登陆脚本限制并发会话,恶意用 ...
- Redis应用场景 及其数据对象 string hash list set sortedset
原文地址:http://www.cnblogs.com/shanyou/archive/2012/09/04/2670972.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在 ...
- 创建多模块maven项目
有两种方式: 1,new -->maven project-->maven-archetype-quickstart 建完多个独立的project后,手动修改pom文件的packing类型 ...
- SSO跨域解决方案
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可 以将 ...
- 各种快递查询--Api接口
授权成功我的密钥 爱查快递API使用说明文档 API地址: 以前:http://api.ickd.cn/?com=[]&nu=[]&id=[]&type=[]&enco ...
- AgileEAS.NET SOA中间件平台/敏捷软件开发平台 and SQL详解
AgileEAS.NET SOA中间件平台/敏捷软件开发平台 http://www.smarteas.net/ SQL详解: http://www.w3school.com.cn/sql/func_d ...
- Linux命令 rpm
rpm -q samba --查询程序是否安装rpm -qa | grep httpd --[搜索指定rpm包是否安装] --all搜索*http ...
- Linux学习一些在Terminal可以用到的快捷键及Shell常用的通配符
Ctrl+c 终断程序 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂定当前程序,暂停后按下任意键恢复运行 Ctrl+z 将当前程序放到后台运行,恢复到前台为命令fg Ctrl+a 将光标移至 ...
- C#和.NET Framework
.NET Framework概述 .NET Framework是由微软开发,一个致力于敏捷软件开发.快速应用开发.平台无关性和网络透明化的软件开发平台. .NET Framework组成 .NET F ...
- [亲测有效] - Mac下屏蔽优酷广告最简单的方法
这里不和圣母婊争论该不该屏蔽广告,仅给出可行方法. Windows下的Adsafe,万能的去广告神器!可惜Mac下没有这款软件,Adblock之流在国内基本是个玩具,但是我们可以通过修改HOST文件达 ...