forked from The-Beez-Kneez/Word-Guess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_guess_owl.rb
153 lines (137 loc) · 4.23 KB
/
word_guess_owl.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Class to print out owl ASCII art for the game.
# Could potentially be used to change difficulty level of game by limiting initial number of guesses
class Owl
def initialize(initial_zz)
@initial_zz = initial_zz
end
attr_reader :initial_zz
def sleepy_owl
puts " {~;~}"
puts " /)__)"
puts "/ ;;"
end
def zzzz(wrong_guesses)
string = "\n "
(@initial_zz - wrong_guesses).times do
string += "Z "
end
puts string
end
def awake_owl
puts " {O;O}"
puts " /)__)"
puts "/ ;;"
end
def winning
puts "\n HOOOOO-RAY!"
puts " {^;^}"
puts " /)__)"
puts "/ ;;"
end
end
# Allows user to guess letters, validates their input and prompts again if not valid,
# stores letters already guessed, and increments the number of wrong guesses,
# generates blank letter spaces, and fills in the blanks as game progresses.
class Game
def initialize (word)
@finished_word = word
@letters = word.upcase.chars
@wrong_guesses = 0
@guessed_letters = []
@word_array [email protected] {|x| "_"}
end
attr_reader :letters, :finished_word, :guessed_letters, :wrong_guesses, :word_array
def guess_letter
print "Please guess a letter: "
guess = gets.chomp.upcase
while @guessed_letters.include?(guess)
print "You already guessed that letter. Guess again: "
guess = gets.chomp.upcase
end
# only lets the user guess A through Z
guess_req = /[A-Z]/
# this is making sure that the user guess does not meet our regex requirements or more then one was put in
while !guess_req.match(guess) || guess.length > 1
print "That is not a correct input. Please guess a letter: "
guess = gets.chomp.upcase
end
if @letters.include?(guess)
@guessed_letters << guess
fill_in(guess)
return "That is a correct guess."
else
@guessed_letters << guess
@wrong_guesses += 1
return "That is a wrong guess."
end
end
def fill_in(guess)
@letters.each_with_index do |letter, i|
if letter == guess
@word_array[i] = guess
end
end
return @word_array
end
def print_word_array
printing_word = ""
@word_array.each do |letter|
printing_word += "#{letter} "
end
return printing_word
end
def what_was_guessed
user_guesses = ""
@guessed_letters.each do |letter|
user_guesses += letter + " "
end
return user_guesses
end
end
# generates random word for user to guess
def word_generator
word_options = ["BAZAAR", "BROOD", "EYRIE", "HOOTING", "LOOMING", "NEST", "STOOPING", "BROOD", "DISS", "PAIR", "PARLIAMENT", "SAGACIOUSNESS", "STARE", "WISDOM", "STABLE", "PROHIBITION", "VOLERY", "BLIZZARD"]
return word_options.sample
end
# prompts user at completion of game if they would like to intiate a new game
def new_game
print "\nDo you want to guess another word? Y/N: "
answer = gets.chomp.upcase
if answer == "Y"
replay = true
else
puts "Thanks for playing with us."
exit
end
end
# user interface:
# continues to prompt user to guess letters until the game is won or lost
# game is won if guessed letters complete the randomly selected word
# game is lost if number of guesses exceeds the number of intial zz's
# prints ASCII art to represent the number of wrong guesses
# changes ASCII art if user wins or loses
replay = true
while replay
otis = Owl.new(5)
our_word = Game.new(word_generator)
puts "Lets play a guessing game, but shhhh Otis is sleeping.\nHe doesn't like to be woken, and wrong guesses tend to disturb him."
until our_word.wrong_guesses == otis.initial_zz || our_word.word_array.join == our_word.finished_word
otis.zzzz(our_word.wrong_guesses)
otis.sleepy_owl
puts our_word.print_word_array
if !our_word.guessed_letters.empty?
puts "\nYou have guessed these letters: #{our_word.what_was_guessed}"
end
puts our_word.guess_letter
end
if our_word.wrong_guesses == otis.initial_zz
puts "\nOH NO! The word was #{our_word.finished_word}."
otis.awake_owl
puts "You woke up Otis!\nHe is nocturnal damnit.\nHe needs sleep.\n"
new_game
else
puts "\nYou won! You must truly understand how important Owls are to our ecosystems."
otis.winning
new_game
end
end