免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1746 | 回复: 0
打印 上一主题 下一主题

找宝藏(treasure hunt) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-04-04 19:44 |只看该作者 |倒序浏览


文件:
treasurehunt.rar
大小:
0KB
下载:
下载
参考地址:
http://www.google.cn/codesearch/p?hl=zh-CN#DHu031yp3ns/~chili/PBI/Exercises/treasurehunt.py&q=genome%20lang:python

根据一个矩阵,利用向上、向下、向左、向右,找到其中的一个元素


# Treasure hunt: you start out in a random room in a 4x4 grid with
# these room numbers:
#
# 0 1 2 3
# 4 5 6 7
# 8 9 10 11
# 12 13 14 15
#
# There is a treasure in one of the other rooms, find it.
#
# As a help, you are told in which directions you can go and
# also the sum of the treasure room number and your current
# room number.
#
# node: it is a hard game, you can cheat if you print treasure and room together.
#
# edit by jimmy
# 20090404
import random
treasure = random.randrange(16) # pick random room (between 0 and 15) for treasure, one number
# print treasure # you can cheat here by print treasure
room = random.randrange(16) # pick random starting room
# print room # you can cheat here by print room
while room == treasure: # - can not be the same as the treasure room
    room = random.randrange(16)
question = "You can go %swhich way do you go? "
while room != treasure:
    print '\nCurrent room + treasure room =', (room+treasure)
    directions = ""
    if room > 3: # if not in the first row, go up
        directions += "(u)p, "
    if room  12: # # if not in the fourth row, go down
        directions += "(d)own, "
    if not room % 4 == 0: # if not in the first col, go left
        directions += "(l)eft, "
    if not room % 4 == 3: # if not in the fourth col, go right
        directions += "(r)ight, "
   
    choice = raw_input( question%directions )
    if choice == 'u':
        room -= 4
    if choice == 'd':
        room += 4
    if choice == 'l':
        room -= 1
    if choice == 'r':
        room += 1
print "\nCongratulations, you found the treasure in room %d!"%treasure




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/33851/showart_1890275.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP