Last active
August 29, 2015 13:57
-
-
Save davidpaulhunt/9898396 to your computer and use it in GitHub Desktop.
guessing_game
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
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found a bug:
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:
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:
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.