Lua 简单的IO交互 和迷宫代码

时间:2021-09-04 16:20:31
 function room1 ()
print("in room1")
local move = io.read()
if move == "south" then
return room3()
elseif move == "east" then
return room2()
else
print("invalid move")
return room1() -- stay in the same room
end
end function room2 ()
print("in room2")
local move = io.read()
if move == "south" then
return room4()
elseif move == "west" then
return room1()
else
print("invalid move")
return room2()
end
end function room3 ()
print("in room3")
local move = io.read()
if move == "north" then
return room1()
elseif move == "east" then
return room4()
else
print("invalid move")
return room3()
end
end function room4 () print("in room4")
print("congratilations!")
end 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!