-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplay.rb
More file actions
90 lines (71 loc) · 1.94 KB
/
play.rb
File metadata and controls
90 lines (71 loc) · 1.94 KB
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
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 gentlemen"],
["jogging", "We are not walking..."],
["celebrate", "Remembering 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|
# replace blank values with guessed letter if matches letter in word
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!"
#if guess is longer than 1 letter
elsif guess.length > 1
puts "only guess 1 letter at a time please!"
make_guess
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 } 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 long"
puts "To exit game at any point type 'exit'"
print_teaser
puts "Clue: #{ @word.last }"
make_guess
end
end
game = Hangman.new
game.begin