|
文件: |
treasurehunt.rar |
大小: |
0KB |
下载: |
下载 | |
参考地址:
根据一个矩阵,利用向上、向下、向左、向右,找到其中的一个元素
# 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
|
阅读(560) | 评论(0) | 转发(0) |