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 可交互视频 此视频是可 ...
随机推荐
- Linux系统编程(34)—— socket编程之TCP服务器与客户端的交互
前面几篇中实现的client每次运行只能从命令行读取一个字符串发给服务器,再从服务器收回来,现在我们把它改成交互式的,不断从终端接受用户输入并和server交互. /* client.c */ #in ...
- Android客户端实现七牛云存储文件上传
1.简单文件上传 上传模型如下. 1.1获得Token 不管是简单文件上传,还是分片上传.断点续传 都需要首先访问服务器,以获得上传凭证信息Token..用于测试时,可以用本地模拟Token信息(有 ...
- HDU_2046——骨牌铺放问题,递推
Problem Description 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数. 例如n=3时,为2× 3方格,骨牌的铺放方案有三种,如下图: In ...
- HDU-5504(逻辑if-else大水题)
Problem Description You are given a sequence of N integers. You should choose some numbers(at least ...
- linux系统下mySQL数据库 备份方法和脚本
数据库备份1.创建个备份存储目录mkdir /root/backup/2.以下内容写到dbbackup.sh #!/bin/bash cd /data/db_backup/mysqldump -uad ...
- JavaScript学习笔记(高级部分—01)
JavaScript的核心ECMAScript描述了该语言的语法和基本对象:DOM描述了处理网页内容的方法和接口:BOM描述了与浏览器进行交互的方法和接口. 简单说,ECMAScript描述了以下内容 ...
- [React Testing] The Redux Store - Multiple Actions
When using Redux, we can test that our application state changes are working by testing that dispatc ...
- REST深入浅出
不知你是否意识到,围绕着什么才是实现异构的应用到应用通信的“正确”方式,一场争论正进行的如火如荼:虽然当前主流的方式明显地集中在基于SOAP.WSDL和WS-*规范的Web Services领域,但也 ...
- CSS3动画(360度旋转、旋转放大、放大、移动)
Title div { width: 120px; height: 120px; line-height: 120px; margin: 20px; background-color: #5cb85c ...
- Spring框架快速入门之简介
Spring是java平台上的一个开源应用框架.它的第一个版本是由Rod Johnson写出来的.Rod在他的Expert One-On- One Java EE Design and Develop ...