This repository has been archived by the owner on Feb 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
hangman.rb
92 lines (70 loc) · 2.23 KB
/
hangman.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
require 'sinatra'
require 'sinatra/static_assets'
require 'haml'
require 'json'
set :root, File.dirname(__FILE__)
#enable :sessions
use Rack::Session::Pool, :expire_after => 2592000
class Word
class << self
def get_random
content = File.read("countries.txt")
words = content.split("\n")
words[rand(words.size)].upcase
end
def masquerade(word)
word.each_char.inject([]) { |disguise, char| disguise << (char == " " ? " " : " "); disguise }
end
def reveal(last_revealed_word, char_clicked, final_word)
chars = final_word.each_char.to_a
last_revealed_word.each_index do |i|
last_revealed_word[i] = chars[i] if last_revealed_word[i] == " " and chars[i] == char_clicked
end
end
def chars_left(revealed_word)
revealed_word.count { |c| c == " " }
end
end
end
class Game
class << self
def win?(chars_left, incorrect_guesses)
chars_left == 0 and incorrect_guesses < 6
end
def correct_guess?(char_clicked, final_word)
final_word.include?(char_clicked)
end
end
end
get "/" do
haml :index
end
post "/new" do
word = Word.get_random
masquerade_word = Word.masquerade(word)
session[:word] = word
session[:incorrect_guesses] = 0
session[:chars_left] = word.size
session[:revealed_word] = masquerade_word
{:word => masquerade_word}.to_json
end
post "/check" do
final_word = session[:word]
char_clicked = params[:char_clicked]
correct_guess = Game.correct_guess?(char_clicked, final_word)
if correct_guess
session[:revealed_word] = Word.reveal(session[:revealed_word], char_clicked, final_word)
session[:chars_left] = Word.chars_left(session[:revealed_word])
else
session[:incorrect_guesses] += 1
end
win = Game.win?(session[:chars_left], session[:incorrect_guesses])
{:word => session[:revealed_word], :correct_guess => correct_guess, :incorrect_guesses => session[:incorrect_guesses], :win => win}.to_json
end
post "/answer" do
if (session[:incorrect_guesses] < 6 and session[:chars_left] > 0)
{:success => -1, :message => "You haven't finished the game yet"}.to_json
else
{:success => 1, :answer => session[:word]}.to_json
end
end