Created
April 5, 2024 15:21
-
-
Save devmaars/e7d678535e5417d5b43f7178f61224f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local gg = gg | |
local Difficulty = { | |
EASY = 1, | |
MEDIUM = 2, | |
HARD = 3, | |
INSANITY = 4 | |
} | |
local function getMaxAttemp(difficulty) | |
if (difficulty == Difficulty.EASY) then | |
return 5 | |
elseif (difficulty == Difficulty.MEDIUM) then | |
return 10 | |
elseif (difficulty == Difficulty.HARD) then | |
return 20 | |
elseif (difficulty == Difficulty.INSANITY) then | |
return 30 | |
end | |
end | |
local function getMaxNumberToGuess(difficulty) | |
if (difficulty == Difficulty.EASY) then | |
return 10 | |
elseif (difficulty == Difficulty.MEDIUM) then | |
return 100 | |
elseif (difficulty == Difficulty.HARD) then | |
return 1000 | |
elseif (difficulty == Difficulty.INSANITY) then | |
return 10000 | |
end | |
end | |
local function getDifficulty() | |
local choice = gg.choice({ | |
string.format("Easy (1-%d)", getMaxNumberToGuess(Difficulty.EASY)), | |
string.format("Medium (1-%d)", getMaxNumberToGuess(Difficulty.MEDIUM)), | |
string.format("Hard (1-%d)", getMaxNumberToGuess(Difficulty.HARD)), | |
string.format("Insanity (1-%d)", getMaxNumberToGuess(Difficulty.INSANITY)) | |
}, 0, "Number Guess") | |
if (not choice) then return end | |
return choice | |
end | |
local function printHelp() | |
local text = | |
[[Number Guess | |
------------ | |
1. Choose difficulty | |
2. Guess the number | |
3. Enjoy the game | |
Difficulties | |
---------- | |
1. Easy (1-10) Max attemp: ]] .. getMaxAttemp(Difficulty.EASY) .. [[ | |
2. Medium (1-100) Max attemp: ]] .. getMaxAttemp(Difficulty.MEDIUM) .. [[ | |
3. Hard (1-1000) Max attemp: ]] .. getMaxAttemp(Difficulty.HARD) .. [[ | |
4. Insanity (1-10000) Max attemp: ]] .. getMaxAttemp(Difficulty.INSANITY) | |
print(text) | |
end | |
local function play() | |
local difficulty = getDifficulty() | |
if (not difficulty) then return end | |
local maxAttemp = getMaxAttemp(difficulty) | |
local maxNumberToGuess = getMaxNumberToGuess(difficulty) | |
math.randomseed(os.time()) | |
local number = math.random(1, maxNumberToGuess) | |
local attemp = 0 | |
local lastGuess = 0 | |
while attemp < maxAttemp do | |
local input = gg.prompt({ | |
string.format("Guess the number (1-%d)", maxNumberToGuess) | |
}, { lastGuess }, { "number" }) | |
if (not input) then return end | |
local guess = tonumber(input[1]) | |
if (not guess) then | |
gg.alert("Invalid input") | |
return | |
end | |
attemp = attemp + 1 | |
lastGuess = guess | |
if (guess == number) then | |
gg.alert(string.format("You win! The number is %d\nAttemp: %d/%d", number, attemp, maxAttemp)) | |
else | |
gg.alert( | |
string.format("Wrong! The number is %s\n Attemp: %d/%d", guess < number and "higher" or "lower", | |
attemp, maxAttemp) | |
) | |
end | |
end | |
end | |
local function main() | |
local choice = gg.choice({ | |
"( * ) Play", | |
"( * ) Help", | |
"( * ) Exit" | |
}, 0, "Number Guess") | |
if (not choice) then return end | |
if (choice == 1) then | |
play() | |
end | |
if (choice == 2) then | |
printHelp() | |
gg.setVisible(true) | |
os.exit() | |
end | |
if (choice == 3) then | |
print("Goodbye!") | |
os.exit() | |
end | |
end | |
while true do | |
if gg.isVisible() then | |
gg.setVisible(false) | |
main() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment