-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebGuesser.rb
More file actions
56 lines (51 loc) · 1.45 KB
/
WebGuesser.rb
File metadata and controls
56 lines (51 loc) · 1.45 KB
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
require "sinatra"
require "sinatra/reloader"
guesses_available = 5
SECRET_NUMBER = rand(99) + 1
def check_guess(guess)
if params["guess"].nil?
else
if guess > SECRET_NUMBER
if guess - 5 > SECRET_NUMBER
return "Way too high!!!"
else
return "Too high!"
end
elsif guess < SECRET_NUMBER
if guess + 5 < SECRET_NUMBER
return "Way too low!!!"
else
return "Too low!"
end
else
return "You got it right! The secret number is #{SECRET_NUMBER}"
end
end
end
def style(message)
case message
when "Way too high!!!" then "background: red"
when "Way too low!!!" then "background: red"
when "Too high!" then "background: #FF4D4D"
when "Too low!" then "background: #FF4D4D"
when "You got it right! The secret number is #{SECRET_NUMBER}" then "background: green"
else
end
end
get "/" do
if params["guess"] == SECRET_NUMBER
SECRET_NUMBER = rand(99) + 1
guesses_available = 5
message = "You got it right! The secret number is #{SECRET_NUMBER}"
elsif guesses_available > 0
guess = params["guess"].to_i
message = check_guess(guess)
style = style(message)
guesses_available = guesses_available - 1
else
SECRET_NUMBER = rand(99) + 1
guesses_available = 4
message = "You lost! A new number has been generated."
end
erb :index, :locals => {:message => message, :style => style, :guesses_available => guesses_available + 1}
end