Lua 简单的IO交互 和迷宫代码
1 function room1 () 2 print("in room1") 3 local move = io.read() 4 if move == "south" then 5 return room3() 6 elseif move == "east" then 7 return room2() 8 else 9 print("invalid move") 10 return room1() -- stay in the same room 11 end 12 end 13 14 function room2 () 15 print("in room2") 16 local move = io.read() 17 if move == "south" then 18 return room4() 19 elseif move == "west" then 20 return room1() 21 else 22 print("invalid move") 23 return room2() 24 end 25 end 26 27 function room3 () 28 print("in room3") 29 local move = io.read() 30 if move == "north" then 31 return room1() 32 elseif move == "east" then 33 return room4() 34 else 35 print("invalid move") 36 return room3() 37 end 38 end 39 40 function room4 () 41 42 print("in room4") 43 print("congratilations!") 44 end 45 46 room1()
Result:
in room1
south
in room3
north
in room1
west
invalid move
in room1
north
invalid move
in room1
south
in room3
south
invalid move
in room3
east
in room4
congratilations!
posted on 2014-02-27 08:13 Step-BY-Step 阅读(388) 评论(0) 编辑 收藏 举报