其中,将洞穴连起来的算法要好好体会。

学习构建临时变量列表,确认循环用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第二个版本---多怪兽,多洞穴,洞穴间双向互通的更多相关文章

  1. 吃午饭前,按书上的代码写会儿--Hunt the Wumpus第一个版本

    有空就要慢慢练起~~~~脑袋动起来是很快乐的事儿....:) <易学PYTHON>演练一遍. from random import choice cave_numbers = range( ...

  2. Spring Cloud Alibaba发布第二个版本,Spring 发来贺电

    还是熟悉的面孔,还是熟悉的味道,不同的是,这次的配方升级了. 今年10月底,Spring Cloud联合创始人Spencer Gibb在Spring官网的博客页面宣布:阿里巴巴开源 Spring Cl ...

  3. 怪兽z主机豪华版 答问。

    我的淘宝店里,怪兽z主机标准版,分经济版本,标准版,豪华版,三个版本.这里给大家详细介绍一下豪华版的概况. 淘宝购买地址:http://item.taobao.com/item.htm?id=3818 ...

  4. 饼干怪兽和APT攻击

    APT攻击就像一个孩子,你通过各种方式窃取他们的大脑要拿出饼干,为了防止恶意攻击,过失作为母亲未能发现和防止饼干盗窃贼如.于她仅仅监视厨房椅子.衣柜门或烤箱门的开启.建立起有效防御目标攻击与APT攻击 ...

  5. hdu 1026(BFS+输出路径) 我要和怪兽决斗

    http://acm.hdu.edu.cn/showproblem.php?pid=1026 模拟一个人走迷宫,起点在(0,0)位置,遇到怪兽要和他决斗,决斗时间为那个格子的数字,就是走一个格子花费时 ...

  6. 使用canvas通过js制作一个小型英雄抓怪兽的2D小游戏

    首先,这是一个HTML: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...

  7. 如何用纯 CSS 创作一只徘徊的果冻怪兽

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdOKQG 可交互视频 ...

  8. 51nod-1670-打怪兽(递推/组合数学)

    1670 打怪兽  基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 lyk在玩一个叫做“打怪兽”的游戏.游戏的规则是这样的.lyk一开始会有一个初始 ...

  9. 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdOKQG 可交互视频 此视频是可 ...

随机推荐

  1. Java正则表达式详解教程

    public class Test { private static Scanner scanner; public static void main(String[] args) { scanner ...

  2. Android录音服务

    MediaRecorder Mediarecorder=new MediaRecorder();//创建一个MediaRecorder对象 Mediarecorder.setAudioSource(M ...

  3. Remote Desktop Connection Manager 多个远程管理

    http://www.microsoft.com/en-us/download/details.aspx?id=21101 Remote Desktop Connection Manager 多个远程 ...

  4. 查看MySQL数据库的默认编码

    查看MySQL数据库的默认编码 1.使用status命令能够显示数据库的相关系信息,示例如下: mysql> status;————–mysql Ver 14.12 Distrib 5.0.77 ...

  5. linux 建库,编码,导入数据

    二.导入数据库1.首先建空数据库mysql>create database abc; 2.导入数据库方法一:(1)选择数据库mysql>use abc;(2)设置数据库编码mysql> ...

  6. Hive和HBase的区别

    一.两者分别是什么:     Apache Hive是一个构建在Hadoop基础设施之上的数据仓库.通过Hive可以使用HQL语言查询存放在HDFS上的数据.HQL是一种类SQL语言,这种语言最终被转 ...

  7. Odoo “坑” 系列之 XML中的布尔类型

    在Odoo中试图通过XML方式更新某条Record的值,却意外发现根本不能更新,经查,对于XML中Boolean类型的字段,更新的方式应该采用eval的方式.

  8. Redis哨兵模式

    Redis-Sentinel redis的哨兵模式 Redis Sentinel 模式简介 Redis-Sentinel是官方推荐的高可用解决方案,当redis在做master-slave的高可用方案 ...

  9. PHP发送POST请求的三种方式

    class Request{ public static function post($url, $post_data = '', $timeout = 5){//curl $ch = curl_in ...

  10. wpf下拉框不能多选的原因

    <dxe:ComboBoxEdit Margin="0"  Height="25" Width="65" VerticalAlignm ...