forked from Ada-C8/Word-Guess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word-guess.rb
177 lines (147 loc) · 4.08 KB
/
word-guess.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
require 'faker'
require 'artii'
require 'colorize'
class Word
attr_reader :word, :letters, :blanks
def initialize(word: Faker::Dessert.variety.downcase.sub(" ", ""))
@word = word
@letters = word.split("")
@letters << @word
@blanks = "-" * (@letters.length - 1)
end
def contains?(letter_guess)
return @letters.include?(letter_guess)
end
def replace_blanks(letter_guess)
indices = find_indices(letter_guess)
indices.each do |index|
@blanks[index] = letter_guess
end
puts @blanks
end
private
def find_indices(letter_guess)
indices = []
([email protected]).each do |index|
if letter_guess == @letters[index]
indices << index
end
end
return indices
end
end #end of class
class Game
attr_reader :game_word, :guesses, :turns
attr_writer :game_word
def initialize
#@art = newart
@game_word = Word.new
@guesses = []
@turns = 0
end
def difficulty
user_level = ""
until user_level == "high" || user_level == "medium" || user_level == "easy"
puts "What level of challenge do you want? You may choose high, medium, or easy."
user_level = gets.chomp.downcase
end
difficulty = ""
until difficulty == user_level
@game_word = Word.new
word = @game_word
if word.word.length > 8
difficulty = "high"
elsif word.word.length <=8 && word.word.length > 5
difficulty = "medium"
else
difficulty = "easy"
end
end
end
def guess(user_guess)
while user_guess.match(/[^a-z]/)
puts "\nPlease return a valid letters from A to Z.\n"
user_guess = gets.chomp.downcase
end
if @guesses.include?(user_guess)
puts "\nYou guessed that letter already!"
return
end
if @game_word.word == user_guess
you_won
end
if @game_word.contains?(user_guess)
reveal(user_guess)
win?
else
puts "\nSorry, that letter is not in your word.\nThere goes some delicious cupcake...\n\n"
@turns += 1
lose?
puts "Here's your word so far: \n #{@game_word.blanks}"
end
decompose_image
@guesses << user_guess
end
def print_guesses
if @guesses.length > 0
guess_string = ""
@guesses.each do |guess|
guess_string += guess.upcase + " "
end
puts "\nHere's what you have guessed so far: #{guess_string}"
end
end
def reveal(user_guess)
puts "\nYes! Here's your word so far:\n"
@game_word.replace_blanks(user_guess)
@game_word.blanks
end
def lose?
if @turns == 5
artii = Artii::Base.new :font => 'slant'
artii = artii.asciify("You LOST!")
puts artii.colorize(:red)
puts "Your word was #{@game_word.word}"
exit
end
end
def win?
if !@game_word.blanks.include?("-")
you_won
end
end
def you_won
artii = Artii::Base.new :font => 'slant'
artii = artii.asciify("You WIN!")
puts artii.colorize(:blue).blink
exit
end
end # end of class
# Ascii Art Methods
def ascii
file = File.open("/Users/kimberley/Desktop/asciiartfile.txt", "r")
contents = file.read
puts contents.colorize(:magenta)
end
def decompose_image
line_limit = [25, 20, 15, 10, 5]
index = (@turns)
partial_image = IO.readlines("/Users/kimberley/Desktop/asciiartfile.txt")[1...line_limit[index]].join
puts partial_image.colorize(:magenta)
end
# Begin game play
game = Game.new
# Explain rules to players
puts "Welcome to FILL IN THE BLANKS, an extra sweet word guess game!"
puts "\nRULES: \n--Each blank represents one letter of the word \n--Your guess can be one letter or the whole word \n--If you're right, those letters will be revealed \n--If you're wrong, your cupcake will start to melt \n--When your cupcake is a puddle of sugar, you lose.\n\n"
# Find out & assign difficult level of user's choice
game.difficulty
# Print ascii image and begin game play
ascii
puts "Your mystery word is (drumroll, please...)\n #{game.game_word.blanks}"
until game.win? || game.lose?
game.print_guesses
puts "What's your guess?"
guess = gets.chomp.downcase
game.guess(guess)
end