-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforca.rb
93 lines (74 loc) · 2.22 KB
/
forca.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
class Forca
def initialize(palavra = "", dificult = :easy)
configure_word(palavra)
configure_dificult(dificult)
output
end
def input(letra)
if valid_letter(letra)
replace_letter(letra)
else
@dificult.add_status
end
output
end
def output
if @jogo[:exibicao] == @jogo[:palavra]
puts "Você ganhou parabens palavra: #{@jogo[:palavra].to_s}"
elsif [email protected]_try?
puts "Seu Jogo terminou tente novamente"
else
puts "Letras conseguidas : #{@jogo[:exibicao].join(' ')}"
puts ""
puts "Status:#{@dificult}"
puts ""
puts "Quantidade de Chances: #{@dificult.number_of_chances}"
end
end
private
def configure_word(word)
@jogo = { :palavra => word.split(//), :exibicao => word.gsub(/\w/, '_').split(//)}
end
def configure_dificult(dificult)
@dificult = ([:easy, :medium, :hard].include? dificult) ? DificultFactory.get_dificult(dificult) : DificultFactory.get_dificult_by_word(@jogo[:palavra])
end
def valid_letter(letra)
return (letra.length == 1) && (!(letra =~ /\w/).nil?) && (@jogo[:palavra].include? letra)
end
def replace_letter(letra)
@jogo[:palavra].each_with_index {|letter,index| @jogo[:exibicao][index] = letter if letter.downcase == letra.downcase}
end
end
class Dificult
@status = []
@actual_status = []
def initialize(dificults = %w("cabeça corpo mãos pernas rosto"))
@status = dificults
@actual_status = []
end
def add_status
@actual_status << @status[@actual_status.length]
end
def can_try?
number_of_chances > 0
end
def number_of_chances()
@status.length - @actual_status.length
end
def to_s
@actual_status.join(' ')
end
end
class DificultFactory
@dificults = {:easy => %w("cabeça corpo mãos pernas rosto"), :medium => %w("cabeça corpo resto"), :hard => %w("cabeça corpo") }
def self.get_dificult(dificult)
Dificult.new(@dificults[dificult] || @dificults[:hard])
end
def self.get_dificult_by_word(word)
case word.size
when 0..5: return Dificult.new(@dificults[:easy])
when 6..10: return Dificult.new(@dificults[:medium])
else return Dificult.new(@dificults[:hard])
end
end
end