-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.rb
54 lines (46 loc) · 1.14 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
class Hangman
# This class will be used to run an instance of a hangman game
# It is only concerned with one run through of the game
# It is not responsible for human interaction.
attr_accessor :words, :word, :spaces, :chances, :guess, :guess_count, :guess_index
def initialize
@words = ['array', 'dog', 'chow']
@word = @words.sample.to_s.downcase
@chances = 8
@guess_count = 0
puts "Hangman game!"
end
def word_to_letters
@word = @word.split(//)
end
def space_converter
@spaces = word.map { |x| "_" }
@more_spaces = @spaces.join(' ')
end
def guess
while @guess_count <= @chances
puts "Please enter a guess"
@guess = gets.chomp
if @word.include?(@guess)
sub
if @spaces == @word
puts "You've won!"
break
end
elsif [email protected]?(@guess)
@guess_count += 1
puts "Letter not included"
end
end
end
def sub
@guess_index = @word.index(@guess)
@spaces[@guess_index] = @guess
puts "Good choice!"
p @spaces
end
end
hangman = Hangman.new
hangman.word_to_letters
hangman.space_converter
hangman.guess