Last active
August 19, 2016 04:52
-
-
Save Erin007/5d69a8b0d9ed9a5b7845829957762e7a to your computer and use it in GitHub Desktop.
word guess with fish
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Game | |
attr_accessor :mystery_word, :fish_name, :mystery_word_array, :blanks_array, :letter, :hash, :display_blanks, :display_mystery_word_array, :fish_tank | |
def initialize (mystery_word = "gypsy", fish_name = "Fishy") | |
@mystery_word = mystery_word.to_s | |
@fish_name = fish_name | |
@mystery_word_array = mystery_word.split("") | |
@blanks_array = mystery_word_array.fill ("_ ") | |
@letter = letter | |
@display_blanks = blanks_array.join("") | |
@display_mystery_word_array = display_mystery_word_array | |
@hash = hash | |
@fish_tank = [ " | |
________________________________________________________________________________ | |
\\ | / | |
\\_|__________________/ | |
| | |
| | |
| | |
><(((º> ~J | |
________________________________________________________________________________", | |
# #fish after 1 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 2 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 3 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 4 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 5 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 6 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 7 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________"] | |
#fish introduction | |
puts "Welcome to Word Guess!" | |
puts | |
puts "This is your fish: ><(((º> " | |
puts | |
puts "Try to prevent #{@fish_name} from being caught by the fisherman." | |
puts | |
puts @fish_tank[0] | |
puts | |
puts "Every time you make a incorrect letter guess, #{@fish_name} will swim closer to the hook. If #{@fish_name} reaches the hook, #{@fish_name} will be eaten. Good luck!" | |
puts | |
#blanks for each letter in the mystery word | |
puts "Here are blanks for each of the letters in the mystery word:" | |
puts display_blanks | |
end #end of initialize | |
#ask the user to guess a letter | |
def ask | |
counter = 0 | |
until @blanks_array.join("") == mystery_word || counter == fish_tank.length | |
puts "Which letter would you like to guess?" | |
@letter = gets.chomp.downcase | |
if @mystery_word.include?(letter) | |
puts "Yes, there is at least one #{@letter} in the mystery word." | |
#replace the blanks in the mystery with the guessed letter | |
if @mystery_word.count(letter) > 1 | |
puts "Turns out there are #{@mystery_word.count(letter)} #{@letter}'s'" | |
index1 = @mystery_word.index(@letter) | |
index2 = @mystery_word.rindex(@letter) | |
@blanks_array[index1] = @letter | |
@blanks_array[index2] = @letter | |
puts @blanks_array.join("") | |
else | |
index1 = @mystery_word.index(@letter) | |
@blanks_array[index1] = @letter | |
puts @blanks_array.join("") | |
end | |
puts fish_tank[counter] | |
puts "Fishy stays in place." | |
else | |
counter += 1 | |
puts "Sorry, no. There is not an #{@letter} in the mystery word." | |
puts fish_tank[counter] | |
puts "Fishy swims toward danger!" | |
end | |
end #end of until loop | |
if counter == fish_tank.length | |
puts "Sorry, Fishy is for dinner!" | |
else | |
puts "Congratulations! You won! Fishy is safe." | |
end | |
end#end of ask method | |
end #end of class | |
test_game = Game.new | |
test_game.ask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'io/console' | |
class Game | |
attr_accessor :mystery_word, :mystery_word_array, :blanks_array, :letter, :fish_tank | |
def initialize | |
puts "Welcome to Word Guess! It works like hangman." | |
puts | |
puts "Player 1 will be the guesser." | |
puts "Player 2, what do you want the mystery word to be?" | |
@mystery_word = STDIN.noecho(&:gets).chomp | |
@mystery_word_array = mystery_word.split("") | |
@blanks_array = mystery_word_array.fill ("_ ") | |
@letter = letter | |
@fish_tank = [ " | |
________________________________________________________________________________ | |
\\ | / | |
\\_|__________________/ | |
| | |
| | |
| | |
><(((º> ~J | |
________________________________________________________________________________", | |
# #fish after 1 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 2 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 3 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 4 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 5 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 6 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 7 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________"] | |
#introduction | |
puts | |
puts "This is your fish: ><(((º> " | |
puts | |
puts "Try to prevent Fishy from being caught by the fisherman." | |
puts | |
puts @fish_tank[0] | |
puts | |
puts "Every time you make a incorrect letter guess, Fishy will swim closer to the hook. If Fishy reaches the hook, Fishy will be eaten. Good luck!" | |
puts | |
end #end of initialize | |
#ask the user to guess a letter | |
def ask | |
counter = 0 | |
guesses = [] | |
until @blanks_array.join("") == mystery_word || counter == fish_tank.length | |
puts @blanks_array.join("") | |
puts "The letters you've guessed are: #{guesses.sort.join(", ")}" | |
puts "Which letter would you like to guess?" | |
@letter = gets.chomp | |
@letter.downcase! | |
if @mystery_word.include?(letter) | |
puts "Yes, there is at least one #{@letter} in the mystery word." | |
#replace the blanks in the mystery with the guessed letter | |
if @mystery_word.count(letter) > 1 | |
puts "Turns out there are #{@mystery_word.count(letter)} #{@letter}'s'" | |
index1 = @mystery_word.index(@letter) | |
index2 = @mystery_word.rindex(@letter) | |
@blanks_array[index1] = @letter | |
@blanks_array[index2] = @letter | |
puts @blanks_array.join("") | |
else | |
index1 = @mystery_word.index(@letter) | |
@blanks_array[index1] = @letter | |
puts @blanks_array.join("") | |
end#end of the conditional to see if the guessed letter occurs more than once in the mystery word | |
puts fish_tank[counter] | |
puts "Fishy stays in place." | |
else | |
counter += 1 | |
puts "Sorry, there is no #{@letter} in the mystery word." | |
puts fish_tank[counter] | |
puts "Fishy swims toward danger!" | |
end #end of conditional to see if the letter guessed is in the mystery_word | |
end #end of until loop | |
#This outputs whether the user lost or won | |
if counter == fish_tank.length | |
puts "Sorry, Fishy is for dinner!" | |
else | |
puts "Congratulations! You won! Fishy is safe." | |
end#end of win or lose conditional | |
end#end of ask method | |
end #end of class | |
test_game = Game.new | |
test_game.ask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'io/console' | |
class Game | |
attr_accessor :mystery_word, :mystery_word_array, :blanks_array, :letter, :fish_tank | |
def initialize | |
puts "Welcome to Word Guess! It works like hangman." | |
puts | |
puts "Player 1 will be the guesser." | |
puts "Player 2, what do you want the mystery word to be?" | |
@mystery_word = STDIN.noecho(&:gets).chomp | |
@mystery_word_array = mystery_word.split("") | |
@blanks_array = mystery_word_array.fill ("_ ") | |
@letter = letter | |
@fish_tank = [ " | |
________________________________________________________________________________ | |
\\ | / | |
\\_|__________________/ | |
| | |
| | |
| | |
><(((º> ~J | |
________________________________________________________________________________", | |
# #fish after 1 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 2 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 3 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 4 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 5 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 6 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________", | |
#fish after 7 wrong guesses | |
" | |
________________________________________________________________________________ | |
\\ | / | |
\\|__________________/ | |
| | |
| | |
| | |
.·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·..·´¯`·.><(((º> ~J | |
________________________________________________________________________________"] | |
#introduction | |
puts | |
puts "This is your fish: ><(((º> " | |
puts | |
puts "Try to prevent Fishy from being caught by the fisherman." | |
puts | |
puts @fish_tank[0] | |
puts | |
puts "Every time you make an incorrect letter guess, Fishy will swim closer to the hook. If Fishy reaches the hook, Fishy will be eaten. Good luck!" | |
puts | |
end #end of initialize | |
#ask the user to guess a letter | |
def ask | |
counter = 0 | |
guesses = [] | |
until @blanks_array.join("") == mystery_word || counter == fish_tank.length | |
puts @blanks_array.join("") | |
puts "The letters you've guessed are: #{guesses.sort.join(", ")}" | |
puts "Which letter would you like to guess?" | |
@letter = gets.chomp | |
@letter.downcase! | |
alphabet_array = [*('a'..'z')] | |
if alphabet_array.include? letter | |
if guesses.include? letter | |
puts "You've already guessed that letter!" | |
else | |
guesses.push (@letter) | |
if @mystery_word.include?(letter) | |
puts "Yes, there is at least one #{@letter} in the mystery word." | |
#replace the blanks in the mystery with the guessed letter | |
if @mystery_word.count(letter) > 1 | |
puts "Turns out there are #{@mystery_word.count(letter)} #{@letter}'s'" | |
index1 = @mystery_word.index(@letter) | |
index2 = @mystery_word.rindex(@letter) | |
@blanks_array[index1] = @letter | |
@blanks_array[index2] = @letter | |
puts @blanks_array.join("") | |
else | |
index1 = @mystery_word.index(@letter) | |
@blanks_array[index1] = @letter | |
puts @blanks_array.join("") | |
end#end of the conditional to see if the guessed letter occurs more than once in the mystery word | |
puts fish_tank[counter] | |
puts "Fishy stays in place." | |
puts | |
puts | |
else | |
counter += 1 | |
puts "Sorry, there is no #{@letter} in the mystery word." | |
puts fish_tank[counter] | |
puts "Fishy swims toward danger!" | |
puts | |
puts | |
end #end of conditional to see if the letter guessed is in the mystery_word | |
end #end of conditional to see if the letter has already been guessed | |
else | |
puts "You must guess a letter, a - z." | |
end #end of conditional to see if user input is a letter | |
end #end of until loop | |
#This outputs whether the user lost or won | |
if counter == fish_tank.length | |
puts "Sorry, Fishy is for dinner!" | |
else | |
puts "Congratulations! You won! Fishy is safe." | |
end#end of win or lose conditional | |
end#end of ask method | |
end #end of class | |
test_game = Game.new | |
test_game.ask |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wordguess_update2.rb is the most current. I didn't add in your code about checking for more than 2 instances because when I did Ruby went haywire and threw a bunch of end errors. Something was off with the ends and the elses... How did you do it?