user3314993
user3314993

Reputation: 287

Lua - Error repeating a function?

I'm working on an 'impossible' game, where you are basically asked a bunch of trick questions.
The first time I run the script, everything works perfectly. If the user decides to play again by typing in 'y', it will re-run the mainScript function. However, the script automatically re-runs the mainScript after finishing it a second time, without taking user input. I might be making a simple mistake, I'm not sure. Here is the script: (Sorry, I know it is kind of long)

    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

Upvotes: 1

Views: 74

Answers (1)

Oliver
Oliver

Reputation: 29473

You should factor out the "startover" logic into the loop, you would see better. Then you notice how there is common code in both if chunks of your checkWin, factor that out too:

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

Note also that it is better to let the script return than call os.exit() (assuming that is what exit() is in your code -- see for example 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

Upvotes: 1

Related Questions