-
-
Save davidpaulhunt/9898396 to your computer and use it in GitHub Desktop.
#import time | |
require 'active_support/core_ext/integer/inflections' | |
#declare fruits and clues, choose answer | |
@ary = [["banana", "It's yellow"], ["orange", "It's a color, too"], ["grape", "From vine to wine"], ["apple", "Johnny loved to plant these"], ["pear", "Rhymes with bear"], ["mango", "Hey man, go"], ["lemon", "Very very sour"], ["date", "Very gulf coast and socially awkward"]] | |
def choose_an_answer | |
@answer = @ary[rand(7)][0] | |
end | |
#greeting | |
def greet_the_user | |
puts "Welcome to an AMAZING guessing game!" | |
sleep(1) | |
puts "\n\nI'm thinking of a delicious fruit. You have 5 chances to guess | |
what it is, take a shot...\n\n" | |
end | |
#display, get first answer | |
def show_possible_answers | |
puts "(hint: it's one of these!)" | |
@ary.each { |a| print a[0], " " } | |
end | |
def get_the_users_guess | |
puts "\n\n" | |
@guess = gets.chomp | |
end | |
def find_common_letters | |
@common_letters = [] | |
@guess_letters = @guess.split("") | |
@guess_letters.each do |g| | |
@common_letters << g if @answer.include?(g) | |
end | |
@common_letters.uniq! | |
puts "\nBut the answer does contain: #{@common_letters}\n" | |
end | |
def check_the_guess | |
if @guess.downcase == @answer.downcase | |
puts "\n\nYes, that's it! Great job." | |
exit | |
end | |
end | |
def show_random_letter | |
puts "\n\nHere's a hint." | |
pos = rand(@answer.length) | |
letter = @answer[pos] | |
pos += 1 | |
puts "The #{pos.ordinalize} letter is #{letter}" | |
end | |
def remove_a_wrong_choice | |
@removables = rand(7) | |
while @ary[@removables] == @answer || @removables > @ary.length | |
@removables = rand(7) | |
end | |
@ary.delete_at(@removables) | |
puts "Let's take one away...\n" | |
end | |
choose_an_answer | |
greet_the_user | |
show_possible_answers | |
get_the_users_guess | |
tries = 5 | |
while tries > 0 | |
check_the_guess | |
puts "Sorry, that's not right" | |
if tries == 4 | |
find_common_letters | |
elsif tries == 3 | |
puts "\n\nHere's a hint." | |
case @answer | |
when @ary[0][0] #don't forget about copies!!!! | |
puts @ary[0][1] | |
when @ary[1][0] | |
puts @ary[1][1] | |
when @ary[2][0] | |
puts @ary[2][1] | |
when @ary[3][0] | |
puts @ary[3][1] | |
when @ary[4][0] | |
puts @ary[4][1] | |
when @ary[5][0] | |
puts @ary[5][1] | |
when @ary[6][0] | |
puts @ary[6][1] | |
else | |
puts @ary[7][1] | |
end | |
elsif tries == 2 || tries == 1 | |
show_random_letter | |
end | |
remove_a_wrong_choice | |
show_possible_answers | |
get_the_users_guess | |
tries -= 1 | |
end | |
puts "\n\nSorry you ran out of chances, you lose. The correct answer was #{@answer}" | |
exit |
Found a bug:
Let's take one away...
(hint: it's one of these!)
orange grape lemon
> dskfjasdf
Sorry you ran out of chances, you lose. The correct answer was mango
Mango was the right answer but wasn't one of the choices given to me in the last array.
Also, you start from five for tries and count down til tries are 0. This is fine, but to me it seems like the reverse order of doing it. Interesting. This is just my opinion. I think maybe a better way to achieve the same thing would be to say 5.times do / end. That way you're not having to keep track of a counter variable, and you can break out of the loop whenever you have a correct guess.
This code:
def remove_a_wrong_choice
@removables = rand(7)
while @ary[@removables] == @answer || @removables > @ary.length
@removables = rand(7)
end
@ary.delete_at(@removables)
puts "Let's take one away...\n"
end
seems like a very indirect way of getting a random removable item that's not the correct answer (and also clearly doesn't work as evidenced by the bug I pointed out). I'd try something like this:
def remove_a_wrong_choice
loop do
removable = rand(7) # just using a local variable since it doesn't look like you're using @removable anywhere else outside this method. also making it singular since its only one thing that you're removing
break if @ary[removable] != @answer and removable < @ary.length
end
@ary.delete_at(removable)
puts "Let's take one away...\n"
end
This way you find a random number and if it matches the conditions (i.e. it's not the same as the answer and it's not outside the length of the array) then you break the loop and delete the item at that location.
Overall, good stuff.
Good job overall, very good feel and user experience.
You use camelcase a lot (e.g. hasLetters). This is common to some languages, but it doesn't follow Ruby convention. Use underscores (e.g. has_letters) instead to knock yourself of the habit, because it may cause problems when we get to Rails, where it won't expect camelcase. Also, camelcase may give some Rubyists a headache. In fact, I don't want to see any camelcase anymore unless you're writing Javascript.
Your variable names are also very javascripty and imply functions rather than objects. We'll go over this tomorrow.
Lines 41-51 are a complicated way to do something pretty simple. All you're trying to do is find which letters are common between them. E.g.,
Good stuff.