forked from Ada-C5/Word-Guess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_guess.rb
213 lines (185 loc) · 6.18 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require 'pry'
require 'colorize'
# Word Guess 2.25.16 Risha Allen & Justine Winnie
# Determine what type of ASCII art your team would like to utilize - be creative!
# Think through how you think you should structure your code. Your code should utilize class(es) and methods to keep functionality contained.
# Create a program that will accept one user guess input and print it out in the terminal.
#
# Primary Requirements
#
# Game play
#
# The user should be able to input a single letter at a time.
# Between each guess, the board should be redrawn to the terminal output (Ascii art!).
# Display the letters that have already been guessed before each player guesses a new letter.
# Technical
#
# You should be able to play your game by running it using ruby from the terminal.
# Use Ruby class objects to contain your game logic. You may be able to implement your solution using just one class, but consider how isolating related functionality into separate classes may increase your code's readability and maintainability.
#Create an array of possible words for the player to guess. Use sample to get one word from array.
# mystery_words = %w(get red kitty nose)
#
# #Print ASCII art and mystery word.
# puts frogarray[0]
# puts mystery_words.sample
#Ask player to guess a letter.
#Capture the guess and push to array "letters you've guessed"
#Checks whether letter is in mystery word.
#If true:
#Print "Got it right!"
#Print mystery word with correct guessed letter inserted.
# variable current guess. When == mystery, they win!
#Loop back to Guess.
#Else/if false, print "Wrong!" and remove one section of ASCII art.
#Print "Try again!"
#Loop back to Guess.
# a method that will print an updated image to the string. that informs of the amount left
# store the art in an array which will be indexed [0].....ect
# syntax: print pie array with index[1]
#def froggy
#end
# #
# @ . . @
# (-------)
# ( >___< )
# ^^ ~ ~ ^^
# \\\\\\\///////
# #
# # @ . . @
# (-------)
# ( >___< )
#
#
#
#
# ^^ ~ ~ ^^
# \\\\\\\///////
#
# @ . . @
# (-------)
# ( >___< )
# ^^ ~ ~ ^^
# \\\\\\\///////
#Reprint the ASCII image, the guessed letters the status of the mystery word
# (create a method)
#When word mystery word is complete
# puts the correct word
# print ASCII art
# puts "you won!"
# exit
#When ASCII art is gone!
# puts "you lost"
# exit
class WordGuess
frog1 = " @ . . @
(-------)
( >___< )
^^ ~ ~ ^^ ".colorize(:green) +
"\n //////\\\\\\\///////
~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~ ".colorize(:blue)
frog2 = " @ . . @
(-------)
( >___< )
^^ ~ ~ ^^ ".colorize(:green) +
"\n ~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~ ".colorize(:blue)
frog3 = " @ . . @
(-------)
( >___< ) ".colorize(:green) +
"\n ~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~".colorize(:blue)
frog4 = " @ . . @
(-------)".colorize(:green) +
"\n ~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~ ".colorize(:blue)
frog5 = " @ . . @".colorize(:green) +
"\n ~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~ ".colorize(:blue)
frog6 = " ~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~
~~~~~~~~~~~~~~ ".colorize(:blue)
frogarray = []
frogarray.push(frog1, frog2, frog3, frog4, frog5, frog6)
#Create an array of possible words for the player to guess. Use sample to get one word from array.
mystery_words = %w(get red kitty nose)
puts "Welcome to Word Guess.\nYour goal is to guess the correct word.\nEach time you guess a wrong letter not in the mystery word, the frog will sink into the water a little more."
puts "You must guess the correct word before the frog is submerged."
puts "Let's get started!"
#Print ASCII art and mystery word.
puts frogarray[0]
# guesses = false
# until guesses == words_display ||
# sample is a method on the array class
# chars is a method on the string class and returns an array of characters
words_display = mystery_words.sample.chars
# print words_display
# replace the letter in mystery word with dashes
n = words_display.length
dashed_word = ("-" * n).chars.to_a
p dashed_word
# index where they have guessed
#guessed_letter = []
wrong_guesses = []
# display for each turn
# store the wrong guess in an array
while dashed_word.include?("-") && wrong_guesses.length < 5
puts "What letter do you want to guess?"
guessed_letter = gets.chomp
if words_display.include?(guessed_letter)
puts "That is correct!"
puts frogarray[wrong_guesses.length]
# puts frogarray[g]
# where is the guessed_letter in words_display
# replace the dash (a string) at that index with guessed_letter
# ["r", "e", "d"].index("r")
# => 0
# a = words_display.index(guessed_letter)
# puts a
# a = 0
# dashed_word[a] = guessed_letter
a = []
words_display.each_with_index do |val, ind| # this method will keep looping through every thing in the array until gets all
if val == guessed_letter # "r"
a.push(ind) # 0
end
a.each do |i|
dashed_word[i] = guessed_letter
end
end
elsif wrong_guesses.include?(guessed_letter) && dashed_word.include?(guessed_letter)
puts "You have already guessed this letter, please guess again."
puts frogarray[wrong_guesses.length]
else puts "No! #{guessed_letter} is wrong\n"
wrong_guesses.push("#{guessed_letter}")
puts frogarray[wrong_guesses.length]
end
# wrong_guesses.length means "how many things in the array"
# print the frog starting to drown.
p "The mystery word: "
p dashed_word
p "Wrong letters you have guessed so far: "
p wrong_guesses
end
# when you've guessed wrong 5 times, you lose.
if wrong_guesses.length == 5
puts "Sorry, you lost. The frog is totally submerged!"
exit
end
# or, if there are no more dashes left, you win!
if dashed_word.include?("-") == false
puts "You won!"
end
end