2014-02-16 43 views
1

我正在研究一個'不可能'的遊戲,你基本上被問到一堆詭計問題。
我第一次運行腳本時,一切都很完美。如果用戶通過鍵入'y'決定再次播放,它將重新運行mainScript函數。但是,腳本會在第二次完成後自動重新運行mainScript,而不需要用戶輸入。我可能會犯一個簡單的錯誤,我不確定。下面是腳本:(對不起,我知道這是一種長)Lua - 重複函數出錯?

math.randomseed(os.time()) 
local lives = 3 
local points = 0 

Questions = { 
    {"What is the magic word?", "A) Please", "B) Abra-Cadabra", "C) Lotion", "D) Cheese", "c"}, 
    {"Does anyone love you?", "A) Yes", "B) No", "C) Everyone loves me!", "D) My mother does", "b"}, 
    {"How many fingers do you have?", "A) None", "B) Eight", "C) Seventeen", "D) Ten", "d"}, 
    {"What is 1 + 1?", "A) Window", "B) Door", "C) Two", "D) That's a stupid question", "a"} 
} 

savedQuestions = {}      --Load the Questions table into savedQuestions 
for i, v in pairs(Questions) do 
    table.insert(savedQuestions, v) 
end 

function loadTable()     --Load the savedQuestions into Questions 
    for i = 1, #savedQuestions do 
     table.insert(Questions, savedQuestions[i]) 
    end 
end 

function waitForStart() 
    local chk = io.read() tostring(chk) 
    if (chk:sub(1, 5)):lower() == "start" then 
     return true 
    end 
    waitForStart() 
end 

function lookForAnswer(ans) 
    table.remove(Questions, number) 
    local input = io.read() tostring(input) 
    if input:lower() == ans then 
     points = points + 1 
     return true 
    end 
    lives = lives - 1 
    return false 
end 

function mainScript() 
    lives = 3 
    points = 0 
    print("Welcome to the Impossible quiz!") 
    print("Type 'start' when you are ready to begin\n") 
    waitForStart() io.write("\n") 

    for i = 1, #Questions do 
     number = math.random(1, #Questions) 
     local prob = Questions[number] 
     local q = prob[1] 
     local a = prob[6] 
     print(q) 
     print(prob[2] .. "\n" .. prob[3] .. "\n" .. prob[4] .. "\n" .. prob[5] .. "\n") 
     if lookForAnswer(a) then 
      print("Correct! Points: " .. points .. " Lives: " .. lives .. "\n\n") 
     else 
      print("WRONG! Points: " .. points .. " Lives: " .. lives .. "\n\n") 
      if lives <= 0 then 
       return false 
      end 
     end 
    end 
    return true 
end 

function checkForReplay() 
    print("Would you like to play again? (Y/N)") 
    local chk = io.read() tostring(chk) 
    if (chk:sub(1, 1)):lower() == "y" then 
     return true 
    end 
    return false 
end 

function checkWin() 
    if mainScript() then 
     print("You won!") 
     print("Points: " .. points .. "\n") 
     if checkForReplay() then 
      Questions = {} 
      loadTable() 
      mainScript() 
     else 
      exit() 
     end 
    else 
     print("You lose!") 
     print("Points: " .. points .. "\n") 
     if checkForReplay() then 
      Questions = {} 
      loadTable() 
      mainScript() 
     else 
      exit() 
     end 
    end 
end 

while true do 
    checkWin() 
end 
+0

'mainScript()'出現3次'checkWin()' –

+0

哦裏面,我從來沒有注意到....謝謝! – user3314993

回答

1

你應該分解出「startover」邏輯進入死循環,你會看到更好。然後,你發現怎麼會有共同的代碼都,如果你的checkWin,係數塊說出來太:

function checkWin() 
    if mainScript() then 
     print("You won!") 
    else 
     print("You lose!") 
    end 
    print("Points: " .. points .. "\n") 
    if not checkForReplay() then 
     exit() 
    end 
end 

while true do 
    checkWin() 

    -- if you get here you have not exited so start over: 
    Questions = {} 
    loadTable() 
    mainScript() -- oops! what's that doing here? 
end 

還要注意的是,最好是讓腳本返回比調用os.exit()(假設是什麼exit()是你的代碼 - 例如參見How to terminate Lua script?):

function checkWin() 
    if mainScript() then 
     print("You won!") 
    else 
     print("You lose!") 
    end 

    print("Points: " .. points .. "\n") 

    return checkForReplay() 
end 

local playAgain = checkWin() 
while playAgain do 
    Questions = {} 
    loadTable() 
    playAgain = checkWin() 
end