Quantcast
Channel: VBForums - Game Demos
Viewing all articles
Browse latest Browse all 56

[LUA] Number Guessing Game

$
0
0
I've been studying LUA lately and decided to post an example of how to create a number guessing game in LUA. The objective is to correctly chose the number that the computer picked from 1 - 100. Here is the source code:

Code:

-- Create the random seed for the random function
math.randomseed(os.time())

while true do
        -- Instructions
        io.write("This is a number guessing game.\n")
        io.write("I will pick a number between 1 - 100 and it is your job to guess that number.\n")
        io.write("If you enter in a number to high then I'll tell you that you've guessed to high.\n")
        io.write("If you enter in a number to low then I'll tell you that you've guessed to low.\n")
        io.write("Whenever you finally guess the correct number I will tell you how good you did.\n")
        io.write("\n")
       
        -- Pick a random number from 1 - 100
        computerPick = math.random(1, 100)
        userGuess = -1
        guesses = 0

        while computerPick ~= userGuess do
                -- Ask the user to pick a number
                io.write("Pick a number(0 - 100): ")
                -- Set the number guessed
                temp = io.read()
               
                if tonumber(temp) then
                        -- Convert the string to a number
                        temp = tonumber(temp)
                        -- Check if it's between 1 - 100
                        if temp >= 1 and temp <= 100 then
                                userGuess = temp
                                -- Increment the amount of times the user has guessed
                                guesses = guesses + 1
                               
                                -- Let the user know if they picked the correct number or not
                                if userGuess == computerPick then
                                        io.write("You've guessed correctly. I picked " .. computerPick .. "\n\n")
                                elseif userGuess < computerPick then
                                        io.write(userGuess .. " is to low.\n\n")
                                else
                                        io.write(userGuess .. " is to high.\n\n")
                                end
                        else
                                io.write(temp .. " is not between 1 and 100.\n")
                        end
                else
                        -- Let the user know that they have not entered in a number
                        io.write(temp .. " is not a valid number.\n")
                end
               
               
        end

        if guesses == 1 then
                io.write("Lucky guess!")
        elseif guesses < 10 then
                io.write("Hey, you're pretty good.")
        elseif guesses < 25 then
                io.write("It's OK, you'll do better next time.")
        else
                io.write("Don't quit your day job!")
        end
       
        io.write("\nPress enter to restart")
        io.read()
       
        -- I operate in windows, if you use a version of Unix then replace cls with 'clear'
        os.execute("cls")
end


Viewing all articles
Browse latest Browse all 56

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>