-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.rb
77 lines (67 loc) · 1.43 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
require 'sinatra'
# require 'sinatra/reloader' if development?
configure do
enable :sessions
end
get '/' do
if session[:secret_code] == nil
redirect to('/newgame')
else
if session[:history].length >= 12
redirect to('/lose')
else
@history = session[:history]
erb :main
end
end
end
post '/' do
@message = evaluate_code(params[:guess])
session[:history] << params[:guess]
redirect to('/')
end
get '/win' do
@secret_code = session[:secret_code]
erb :win
end
get '/newgame' do
session[:history] = []
session[:secret_code] = set_code
redirect to('/')
end
get '/lose' do
@secret_code = session[:secret_code]
erb :lose
end
helpers do
def set_code
@secret_code = []
4.times {@secret_code.push((rand(5) + 1).to_s)}
@secret_code
end
def evaluate_code(user_guess)
guess_array = user_guess.scan(/./)
puts session[:secret_code].to_s
computer_response = []
temp_code = session[:secret_code].clone
if temp_code == guess_array
# @game_over = true
redirect to('/win')
else
guess_array.each_with_index do |guess, index|
if guess == temp_code[index]
computer_response.push("+")
guess_array[index],temp_code[index] = " ","x"
end
end
guess_array.each_with_index do |guess, index|
if temp_code.include?(guess)
computer_response.push("-")
temp_code[temp_code.index(guess)] = "x"
end
end
puts computer_response.to_s
computer_response
end
end
end