Chinaunix首页 | 论坛 | 博客
  • 博客访问: 367065
  • 博文数量: 97
  • 博客积分: 2846
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-19 20:00
文章分类

全部博文(97)

文章存档

2017年(1)

2013年(2)

2012年(6)

2011年(17)

2010年(12)

2009年(41)

2007年(18)

我的朋友

分类: Python/Ruby

2009-04-04 19:44:35

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


 

 

阅读(530) | 评论(0) | 转发(0) |
0

上一篇:返回一个list中最大的3个数

下一篇:IQ测试

给主人留下些什么吧!~~