forked from Ada-C8/Word-Guess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-guess-main.rb
86 lines (72 loc) · 1.77 KB
/
word-guess-main.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
class Game
def initialize(input_word, word_descriptions)
@word = input_word.chars.to_a
@hidden_word = @word.join.gsub(/[A-Za-z]/, "_")
@bad_guesses = 0
@word_descriptions = word_descriptions
end
def guess(letter)
if @word.include?(letter) == false
@bad_guesses += 1
puts "I'm sorry, that letter is not in this word!"
else
puts "Excellent choice!!"
until @word.include?(letter) == false
index = @word.index(letter)
@word[index] = "*"
@hidden_word[index] = letter
end
end
end
def word_descriptions
return "#{@word_descriptions}"
end
def print_guessed_word # prints hidden word
puts " Word: #{@hidden_word}"
end
def print_picture
count = 5 - @bad_guesses
puts "(@)" * count
puts " ,\\,\\,|,/,/,"
puts " _\\|/_"
puts " |_____|"
puts" | |"
puts " |___|"
end
def is_game_lost
if @bad_guesses >= 5
return true
else
return false
end
end
def is_game_won
if @hidden_word.include?("_")
return false
else
return true
end
end
end
# Main program
word_array = ["towel", "plant", "apples", "yoga", "banana", "mississippi"]
word_descriptions = ["A household item", "It's alive!", "You can eat it!", "It's a sport.", "It's a fruit.", "It's a state."]
rand_num = rand(0..(word_array.length - 1))
g1 = Game.new(word_array[rand_num], word_descriptions[rand_num])
puts g1.word_descriptions
while true
puts "***********"
g1.print_guessed_word
puts "***********"
puts "Please enter a letter:"
letter = gets.chomp.downcase
g1.guess(letter)
g1.print_picture
if (g1.is_game_lost)
puts "I'm sorry, you lost! :("
break
elsif g1.is_game_won
puts "HOORAY! You WON!! :) "
break
end
end