-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
94 lines (82 loc) · 1.98 KB
/
app.rb
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
93
94
require "sinatra"
require "sinatra/reloader" if development?
enable :sessions
get "/" do
erb :index, layout: :main
end
get "/game" do
session["status"] ||= start_game
@status = session["status"]
@message = session.delete(:message)
erb :game, layout: :main
end
post "/game" do
@status = session["status"]
@guess = params["guess"]
session["status"] = round(@status, @guess)
game_over?
end
get "/win" do
@status = session.delete("status")
erb :win, layout: :main
end
get "/lose" do
@status = session.delete("status")
erb :lose, layout: :main
end
# Initialize the game status parameters.
def start_game
dictionary = File.readlines("hipster-dictionary.txt")
secret_word = get_word(dictionary).split("")
word = secret_word.map do |x|
x == "-" ? x = "-" : x = "_"
end
{
secret_word: secret_word,
word: word,
incorrect_guesses: [],
images: ["Full Hipster.svg",
"Hipster 4.svg",
"Hipster 3.svg",
"Hipster 2.svg",
"Hipster 1.svg"]
}
end
# Select a random word of 5 to 12 letters from the dictionary file.
def get_word(dictionary)
word = dictionary[rand(dictionary.length)]
# Ensure that the word is between 5 and 12 letters long.
if word.length.between?(5,12)
return word.downcase.strip
else
get_word(dictionary)
end
end
# Play a round of hangman
def round(status, guess)
if guess.length > 1
if guess == @status[:secret_word]
redirect "/win"
else
session[:message] = "Sorry not sorry. That's not the right word. Keep guessing."
end
else
if status[:secret_word].include?(guess)
status[:secret_word].each_with_index do |letter, i|
status[:word][i] = guess if status[:secret_word][i] == guess
end
else
status[:incorrect_guesses] << guess
end
end
return status
end
def game_over?
if @status[:secret_word] == @status[:word]
redirect "/win"
elsif @status[:incorrect_guesses].length >= 5
redirect "/lose"
else
redirect "/game"
end
end