2016-01-29 81 views
2

我對Lua非常陌生,我的代碼讓我感到困惑,我只是爲了練習而製作迷宮遊戲,並且遇到了一個錯誤,每次運行我的代碼時它都會循環,而不是去下一部分。我將不勝感激任何幫助。我的代碼不斷循環

我的代碼:

print ("Welcome to the maze") 

input = "" 
while input ~= "leave" do 
    print ("What do you want to do first? Leave or inspect?") 
    input = io.read() 

    if input == "inspect" then 
     print (" You venture towards the maze.") 
    end 

    if input == "leave" then 
     print ("You turn around and run.") 
    end 
end 

input = "" 
while input ~= "turn around" do 
    print ("There is a path, which do you want to take, left, right or turn around?") 
    input = io.read() 

    if input == "left" then 
     print (" You turn left to the dark trees.") 
    end 

    if input == "right" then 
     print ("You turn right to the light pathway.") 
    end 

    if input == "turn around" then 
     print ("You turn around and run.") 
    end 
end 
+0

林不知道爲什麼代碼已經搞砸了,但心不是它是如何,其對賽特正常的我。 – matthewwaring

+0

你是第一次來嗎? –

+0

你是什麼意思? – matthewwaring

回答

1

儘管這裏的邏輯是稍微傾斜(一旦你turn around你會被要求inspectleave再次),這裏是你如何去說。第二部分 - 它需要如果您選擇inspect迷宮發生:

print ("Welcome to the maze") 

input = "" 
while input ~= "leave" do 
    print ("What do you want to do first? Leave or inspect?") 
    input = io.read() 

    if input == "inspect" then 
     print (" You venture towards the maze.") 
     while input ~= "turn around" do 
      print ("There is a path, which do you want to take, left, right or turn around?") 
      input = io.read() 

      if input == "left" then 
       print (" You turn left to the dark trees.") 
      end 

      if input == "right" then 
       print ("You turn right to the light pathway.") 
      end 

      if input == "turn around" then 
       print ("You turn around and run.") 
      end 
     end 
    end 

    if input == "leave" then 
     print ("You turn around and run.") 
    end 
end 
+0

我很困惑你如何達到這一點......請解釋。 – matthewwaring

+0

從邏輯上思考一下。你迷路 - 你有兩種選擇,檢查或離開。如果你檢查,你會進入迷宮。你不能離開迷宮,然後在迷宮中左轉或右轉。這就是你的第一個while循環所說的 - 鍵入'leave'去到下一段代碼。 這裏仍然存在一個合乎邏輯的問題,因爲在任何時候「轉身」時,您都可以在下一個輸入中神奇地「離開」迷宮,而不需要找到回到起點的路。 –

+0

所以我應該抓這個項目做一些不同的事情嗎? – matthewwaring