Skip to content

Instantly share code, notes, and snippets.

@Miqueas
Last active February 24, 2021 18:25
Show Gist options
  • Save Miqueas/5104bc348f913c91ecab4a218acfd201 to your computer and use it in GitHub Desktop.
Save Miqueas/5104bc348f913c91ecab4a218acfd201 to your computer and use it in GitHub Desktop.
[Lua] Classic guessing game
-- Generates a constantly-changin number
-- Note: Lua 5.4 don't need this
math.randomseed(os.time())
local MagicNum = math.random(100)
local Lives = 3
print ("Guess the number!")
print ("Enter your choice:")
io.write("> ")
local Choice = io.stdin:read("*n")
while true do
if Choice ~= MagicNum and Lives ~= 0 then
print ("Bad! Try again:")
io.write("> ")
Choice = io.stdin:read("*n")
Lives = Lives - 1
elseif Choice ~= MagicNum and Lives == 0 then
print("You loss!")
print("The number is " .. MagicNum)
break
else
print("You win!")
break
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment