文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>找宝藏(treasure hunt)

找宝藏(treasure hunt)

时间:2009-04-04  来源:blackjimmy

文件: 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

 

 

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载