forked from saasbook/hw-sinatra-saas-wordguesser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
116 lines (98 loc) · 2.71 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require 'sinatra/base'
require 'sinatra/flash'
require './lib/hangperson_game.rb'
class HangpersonApp < Sinatra::Base
enable :sessions
register Sinatra::Flash
after do
if @game != nil
session[:word] = @game.word
session[:guesses] = @game.guesses
session[:wrong_guesses] = @game.wrong_guesses
end
end
# These two routes are good examples of Sinatra syntax
# to help you with the rest of the assignment
get '/' do
redirect '/new'
end
get '/new' do
erb :new
end
post '/create' do
# NOTE: don't change next line - it's needed by autograder!
word = params[:word] || HangpersonGame.get_random_word
# NOTE: don't change previous line - it's needed by autograder!
@game = HangpersonGame.new(word)
redirect '/show'
end
# Use existing methods in HangpersonGame to process a guess.
# If a guess is repeated, set flash[:message] to "You have already used that letter."
# If a guess is invalid, set flash[:message] to "Invalid guess."
post '/guess' do
retrieve_game_instance
letter = params[:guess].to_s[0]
if letter.nil? or letter == "" or letter =~ /[^a-z]/i
begin
guessed = @game.guess(letter)
rescue ArgumentError
flash[:message] = "Invalid guess."
end
end
if letter =~ /[a-z]/i
guessed = @game.guess(letter)
if !guessed
flash[:message] = "You have already used that letter."
end
end
redirect '/show'
end
# Everytime a guess is made, we should eventually end up at this route.
# Use existing methods in HangpersonGame to check if player has
# won, lost, or neither, and take the appropriate action.
# Notice that the show.erb template expects to use the instance variables
# wrong_guesses and word_with_guesses from @game.
get '/show' do
retrieve_game_instance
state = @game.check_win_or_lose
case state
when :lose
redirect '/lose'
when :win
redirect '/win'
else
erb :show #default :play
end
end
get '/win' do
retrieve_game_instance
state = @game.check_win_or_lose
case state
when :win
erb :win
else
redirect '/show'
end
end
get '/lose' do
retrieve_game_instance
state = @game.check_win_or_lose
case state
when :lose
erb :lose
else
redirect '/show'
end
end
def word_with_guesses(word, guesses)
@game = HangpersonGame.new(word)
@game.guesses = guesses
return @game.word_with_guesses
end
def retrieve_game_instance
word = session[:word].to_s
@game = HangpersonGame.new(word)
@game.guesses = session[:guesses].to_s
@game.wrong_guesses = session[:wrong_guesses].to_s
end
end