forked from DarthJDG/NumGuess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
numguess.lua
92 lines (74 loc) · 1.91 KB
/
numguess.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
theSeed = os.time()
math.randomseed(theSeed)
math.random(1, 10) -- Burn first random number, workaround for same number bug.
print("Welcome to NumGuess Lua terminal version!\n")
io.write("Enter your name: ")
name = io.read()
if name == "" then
name = "Player"
end
io.write("\nWelcome " .. name .. ", enter limit: ")
limit = io.read()
if tonumber(limit) then
limit = tonumber(limit)
if limit < 10 then
limit = 10
end
else
limit = 10
end
max_tries = math.floor(math.log(limit) / math.log(2)) + 1
while true do
tries = 0
number = math.random(1, limit)
print("\nGuess my number between 1 and " .. limit .. "!\n")
while true do
io.write("Guess: ")
guess = tonumber(io.read())
if not tonumber(guess) then
print("That's just plain wrong.")
else
guess = tonumber(guess)
if guess > limit or guess < 1 then
print("Out of range.")
else
if guess < number then
tries = tries + 1
print("Too low!")
elseif guess > number then
tries = tries + 1
print("Too high!")
else
tries = tries + 1
break
end
end
end
end
tries_declination = "try"
if tries > 1 then
tries_declination = "tries"
end
print("\nWell done " .. name .. ", you guessed my number from " .. tries .. " " .. tries_declination .. "!")
custom_message = ""
if tries == 1 then
custom_message = "You're one lucky bastard!"
elseif tries == max_tries then
custom_message = "You are a machine!"
elseif tries > max_tries and tries <= max_tries * 1.1 then
custom_message = "Very good result!"
elseif tries > max_tries * 1.1 and tries <= limit then
custom_message = "Try harder, you can do better!"
elseif tries > limit then
custom_message = "I find your lack of skill disturbing!"
else
custom_message = "You are the master of this game!"
end
print(custom_message)
io.write("Play again [y/N]? ")
again = io.read()
if string.upper(again) ~= 'Y' then
break
end
end
print("\nOkay, bye.")