-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHangman.rb
85 lines (66 loc) · 2.03 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
class Hangman
def initialize
@word = words.sample
@lives = 7
@word_teaser = ""
@word.first.size.times do
@word_teaser += "_ "
end
end
def words
[
["cricket", "A game played by gentleman"],
["jogging", "We are not walking.."],
["celebrate", "Remember special moments"],
["continent", "There are 7 of these"],
["exotic", "Not from around here..."],
]
end
def print_teaser last_guess = nil
update_teaser(last_guess) unless last_guess.nil?
puts @word_teaser
end
def update_teaser last_guess
new_teaser = @word_teaser.split
new_teaser.each_with_index do |letter, index|
if letter == '_' && @word.first[index] == last_guess
new_teaser[index] = last_guess
end
end
@word_teaser = new_teaser.join(' ')
end
def make_guess
if @lives > 0
puts "Enter a letter"
guess = gets.chomp
good_guess = @word.first.include? guess
if guess == "exit"
puts "Thank you for playing!"
elsif good_guess
puts "You are correct"
print_teaser guess
if @word.first == @word_teaser.split.join
puts "Congratulations... you have won this round!"
else
make_guess
end
else
@lives -= 1
puts "Sorry... you have #{@lives} left. Try again"
make_guess
end
else
puts "Game Over... Better Luck Next Time!"
end
end
def begin
# ask user for a letter
puts "New Game started... your word is #{ @word.first.size } characters"
puts "To exit game at any point, type 'exit'"
print_teaser
puts "Clue is: #{ @word.last}"
make_guess
end
end
game = Hangman.new
game.begin