2017-05-25 65 views
2

首先,我知道這是一個混亂,無組織的一堆Lua,但我仍在學習,所以請原諒我。功能和while語句創建無限打印循環

我的問題是: 我在iPad的TouchLua +上運行我的代碼,它運行的很順利,直到我回到tinput = io.read(),無論是輸入'attack'還是'run away'I最終會在屏幕上印上「這不是一個有效的命令」,這是無限的。

我認爲這是while語句中的get_input()與我已經將tin.read()定義爲tinput而不是整個腳本的標準輸入之間的衝突。

我可以接受這一點,除了我從中學習的腳本(目前的YouTube視頻)沒有這樣做的事實,我已經比較了兩者和功能,以及'如果能夠'和他們寫的完全一樣。

我試圖找到答案在谷歌上,視頻和原始腳本存儲在github上的意見,但我不明白github,但谷歌並沒有幫助。

我做了什麼錯誤,或者錯過了,或者一般搞砸了......

另外,請原諒我的大量使用的意見,對不起。

print("Welcome to the game") 

    -- expilictly set the variable to an empty string 
    -- because it is used in a 'while' loop 
    input = "" 
    inv = {"sword", "coin", "armour"} 

    -- inventory function 
    function get_inv() 
     for i, v in pairs(inv) do 
      print(i .. " " .. v) 
     end 
    end 

    -- invalid command function 
    function inv_com() 
     print("You did not type a valid command...") 
    end 

    -- function to simplify 'while' statement throughout 
    -- entire programme 
    function get_input() 
     print("What do you want to do?") 
     i = io.read() -- get what the user types 
     return i 
    end 

    -- fuction adding item to inventory 
    function push_inv(item) 
     table.insert(inv, item) 
    end 

    -- function removing item from inventory 
    function pop_inv(item) 
     for i, v in pairs(inv) do 
      if v == item then 
       table.remove(inv, i) 
      end 
     end 
    end 

    -- '~=' means not equal to 
    while input ~= "leave cave" do 
     input = get_input() -- get what the user types 

     -- '==' means equal to 
     if input == "inspect" then 
      print("You are in a cave") 
     elseif input == "leave cave" then 
      print("You leave cave") 
     elseif input == "inv" then 
      get_inv() 
     else 
      inv_com() 
     end 
    end 

    input = "" 
    while input ~= "follow path" do 
     input = get_input() 

if input == "inspect" then 
    print("You are at the base of a hill. There is a path") 
elseif input == "follow path" then 
    print("You follow the path") 
    print("A troll appears wielding an axe") 
    print("What do you want to do") 
    tinput = io.read() 
    if tinput == "attack" then 
     print("You smack it, and it falls dead") 
    elseif tinput == "run away" then 
     print("You cowardly run away and it smack you, and steals your coin") 
     pop_inv("coin") 
    else 
     print("You stand there, it stabs you, you die") 
     os.exit() 
    end 
elseif input == "inv" then 
    get_inv() 
else 
    inv_com() 
end 
    end 


    input = "" 

    -- a boolean value can only have 1 of 2 values 
    -- 'true' or 'false', this works great for the have_key 
    -- value because there are only 2 possibilities: 
    -- you either have the key or you don't. 
    have_key = false 

    -- this stement mean: while this statement: 
    -- 'input == "open gate" 
    -- and have_key' is true 
    while not (have_key == true and input == "open gate") do 
input = get_input 


if input == "inspect" then 
    --- if we dont have the key, tell us 
    if have_key == false then 
     print("there is a path behind you, a gate infront of you and key is hidden in the grass") 
     -- if we have the key, dont tell us 
    elseif have_key == true then 
     print("There is a path behind you, and a gate infront of you") 
    else 
     print("you did not put a valid command") 
    end 

    -- we now have the key, set variable to true 
elseif input == "grab key" then 
    have_key = true 
    print("You grabbed the key") 
    table.insert(inv, "gate key") 
elseif input == "inv" then 
    get_inv() 
elseif input == "open gate" then 
    if have_key == true then 
     print("You've escaped") 
    elseif have_key == false then 
     print("The gate is locked") 
    end 
elseif input == "pick up magic" then 
    push_inv("MAGIC") 
elseif input == "inv" then 
    get_inv() 
else 
    inv_com() 
end 
    end 


    print("You won the game. Please leave!") 

回答

2

你的一個while循環中有代碼

input = get_input 

這應該是

input = get_input() 

發生了什麼事是你已經設置的變量輸入等於功能 get_input ,而不是調用它的結果,所以輸入永遠不會是有效的字符串,它總是會調用inv_com。


編輯:我知道,這是問題的範圍之外,但我有一對夫婦的提示對於特定問題之外你的程序。

  1. 在命名您的函數時請小心,以免造成歧義。您有用於庫存管理的功能,如pop_inv和push_inv,但同時具有inv_com用於無效命令。
  2. 閱讀Lua style guide。你的代碼有一些非常瘋狂的縮進,這讓其他人很難理解正在發生的事情。
  3. 試着思考如何將所有這些代碼轉換爲可能處理所有可能性的主循環,而不是一個接一個的循環和if語句。想想如果你想在以後增加更多的功能,你的程序結構將會變得多麼複雜。

你在做什麼讓我想起了很多我如何開始編程,使文本冒險。如果你對文字冒險感興趣,可以看看Zo​​rk,看看他們的遊戲循環是如何工作的。另外做一些關於狀態機的研究,因爲它似乎對你現在正在構建的東西有用。快樂編碼:)

+0

如果我的回答有幫助,可否請您將其標記爲已接受?如果它仍然處於無限循環狀態,我很樂意再看看並相應地編輯我的答案。 –