-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ampers: Nicoleta && Kate #14
base: master
Are you sure you want to change the base?
Changes from all commits
c2298e0
a5d210a
af015cf
6f6980b
552ebad
61a1595
16dfb96
29bc1bf
c6d801a
10d9d2d
06b44fa
2184851
baf207b
7f768ee
e07360d
20c598a
a507a39
8468887
4f5ee89
79f5a1b
04c3e6c
f5810a1
ed4fe35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Z Z Z Z Z | ||
|
||
|\ _,,,---,,_ | ||
/,`.-'`' -. ;-;;,_ | ||
|,4- ) )-,_..;\ ( `'-' | ||
'---''(_/--' `-'\_) | ||
|
||
_ | ||
| \ | ||
| | | ||
| | | ||
|\ | | | ||
/, ~\ / / | ||
X `-.....-------./ / | ||
~-. ~ ~ | | ||
\ / | | ||
\ /_ ___\ / | ||
| /\ ~~~~~ \ | | ||
| | \ || | | ||
| |\ \ || ) | ||
(_/ (_/ ((_/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
class Game | ||
attr_accessor :words_array, :word, :guess, :tries | ||
|
||
def initialize(words_array) | ||
@words_array = words_array | ||
@tries = 5 | ||
end | ||
|
||
def choose_word | ||
@word = Word.new(words_array.sample) | ||
end | ||
|
||
## Start Guess Method | ||
def guess(letters_array) | ||
art = Art.new | ||
bad_guesses = [] | ||
while @tries > 0 && word.blanks_array.include?("_") | ||
puts art.play_art | ||
puts "\t#{word.blanks_array.join(" ")}" | ||
print "\nPlease guess a letter: " | ||
guess = gets.chomp | ||
puts "\n------------------------------------------" | ||
|
||
|
||
if word.letters_array.include?(guess) # if guess is right | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can break out the code that handles the logic of checking to see if the letter is in the word and updating the guesses & blanks_array into a helper method and let this method focus on the user input. |
||
puts "Good guess!" | ||
# finding blanks and replacing them with the guess | ||
word.letters_array.length.times do |i| | ||
if word.letters_array[i] == guess | ||
word.blanks_array[i] = guess | ||
end | ||
end | ||
else # if guess is wrong | ||
@tries -= 1 | ||
puts "\nSorry try again." | ||
puts "You have #{@tries} tries left." | ||
bad_guesses << guess | ||
end | ||
if bad_guesses.length != 0 && word.blanks_array.include?("_") | ||
puts "\nAlready guessed letters are: \n#{bad_guesses.join(", ")}\n" | ||
end | ||
|
||
end | ||
return @tries | ||
end | ||
## End Guess Method | ||
end | ||
|
||
## Start Word Class | ||
class Word | ||
attr_accessor :blanks_array, :chosen_word, :letters_array | ||
|
||
def initialize(word) | ||
@chosen_word = word | ||
end | ||
|
||
def split | ||
@letters_array = @chosen_word.split(//) | ||
return @letters_array | ||
end | ||
|
||
def create_blanks | ||
@blanks_array = [] | ||
@letters_array.each do | ||
@blanks_array << "_" | ||
end | ||
return @blanks_array | ||
end | ||
end | ||
## End Word Class | ||
|
||
## Start Art Class | ||
class Art | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the |
||
attr_accessor :art | ||
|
||
def initialize | ||
@art = art | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sets @Art to itself, doing nothing. |
||
end | ||
|
||
def play_art | ||
@play_art = " | ||
|
||
Z Z Z Z Z | ||
|
||
|\\ _,,,---,,_ | ||
/,`.-'`' -. ;-;;,_ | ||
|,4- ) )-,_..;\\ ( `'-' | ||
'---''(_/--' `-'\\_)" | ||
|
||
return @play_art | ||
end | ||
|
||
def end_art | ||
@end_art = " | ||
_ | ||
| \\ | ||
| | | ||
| | | ||
|\\ | | | ||
/, ~\\ / / | ||
X `-.....-------./ / | ||
~-. ~ ~ | | ||
\\ / | | ||
\\ /_ ___\\ / | ||
| /\\ ~~~~~ \\ | | ||
| | \\ || | | ||
| |\\ \\ || ) | ||
(_/ (_/ ((_/" | ||
|
||
return @end_art | ||
end | ||
end | ||
## End Art Class | ||
|
||
## Start Game | ||
game = Game.new(["ampers","octos","code","ada"]) | ||
|
||
puts "\nWelcome to our Don't Wake the Cat Guessing Game!" | ||
puts "Can you guess my word before the cat wakes up?" | ||
game.choose_word # => word from array | ||
game.word.split # word split | ||
puts "The word is #{game.word.split.length} letters long." | ||
game.word.create_blanks | ||
game.guess(game.word) | ||
if game.tries == 0 # ran out of tries | ||
puts "\nSorry the cat is now awake" | ||
our_art = Art.new | ||
puts our_art.end_art | ||
else # if word correct | ||
puts "\n\t#{game.word.split.join("")}!" | ||
puts "\nCongratulations you've guessed the word! You win!\n" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
class Game | ||
attr_accessor :words_array, :word | ||
|
||
def initialize(words_array) | ||
@words_array = words_array | ||
end | ||
|
||
def choose_word | ||
@word = Word.new(words_array.sample) | ||
end | ||
|
||
end | ||
|
||
class Word | ||
attr_accessor :blanks, :word, :letters_array | ||
|
||
def initialize(word) | ||
@word = word | ||
end | ||
|
||
def split | ||
# @letters_array = @word | ||
@letters_array = @word.split(//) | ||
return @letters_array | ||
end | ||
|
||
def create_blanks | ||
blanks = "" | ||
@letters_array.each do | ||
blanks += "_ " | ||
end | ||
return blanks | ||
end | ||
end | ||
|
||
class Art | ||
attr_accessor :art | ||
|
||
def initialize | ||
@art = art | ||
end | ||
|
||
def play_art | ||
play_art = " | ||
|\\ _,,,---,,_ | ||
/,`.-'`' -. ;-;;,_ | ||
|,4- ) )-,_..;\\( `'-' | ||
'---''(_/--' `-'\\_)" | ||
return play_art | ||
end | ||
|
||
def end_art | ||
end_art = " | ||
_ | ||
| \\ | ||
| | | ||
| | | ||
|\\ | | | ||
/, ~\\ / / | ||
X `-.....-------./ / | ||
~-. ~ ~ | | ||
\\ / | | ||
\\ /_ ___\\ / | ||
| /\\ ~~~~~ \\ | | ||
| | \\ || | | ||
| |\\ \\ || ) | ||
(_/ (_/ ((_/" | ||
return end_art | ||
end | ||
|
||
end | ||
|
||
def guess(game_word) | ||
puts "I'm in the guess method. I passed in an instance of Word called my_word_instance. This is its value:" | ||
puts game_word | ||
|
||
# start guess loop | ||
print "Please guess a letter: " | ||
guess = gets.chomp | ||
|
||
puts "I want some letters_array. What is the letters array?" | ||
puts game_word.letters_array | ||
|
||
if game_word.letters_array.include?(guess) # if guess is right | ||
puts "Good guess!" | ||
else # if guess is wrong | ||
# change art | ||
puts "Sorry try again" | ||
end | ||
end | ||
|
||
game = Game.new(["ampers","octos","code","ada"]) | ||
|
||
our_art = Art.new | ||
print our_art.play_art | ||
|
||
puts "\nWelcome to our Don't Wake the Cat Guessing Game!" | ||
puts "Can you guess my word before the cat wakes up?" | ||
game.choose_word | ||
p game.word.split | ||
p game.word.create_blanks | ||
# add art | ||
|
||
puts "I have an instance of Game:" | ||
puts game | ||
puts "My instance of Game called game has an instance of word in game.word" | ||
puts game.word | ||
|
||
guess(game.word) | ||
# if word correct | ||
puts "Congratulations you won!" | ||
# if word incorrect | ||
puts "Sorry the cat is now awake" | ||
puts our_art.end_art |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is important to "trap" user input so they can't enter an invalid letter. You could also wrap the act of guessing into a helper method.