Skip to content

Instantly share code, notes, and snippets.

@judesamp
Last active August 29, 2015 13:57
Show Gist options
  • Save judesamp/9900166 to your computer and use it in GitHub Desktop.
Save judesamp/9900166 to your computer and use it in GitHub Desktop.
a simple text guessing game...
@array_of_secret_words = [
[{word: "call", hint_1: 'rhymes with small', hint_2: 'Who you gonna _____?'}, {word: "sham", hint_1: 'rhymes with wham', hint_2: '_____Wow'}, {word: "dogs", hint_1: 'rhymes with cogs', hint_2: 'The _____ are barking!'}, {word: "what", hint_1: 'rhymes with but', hint_2: 'rhymes with mutt'}],
[{word: "faces", hint_1: 'rhymes with laces', hint_2: 'Til we have ___.'}, {word: "blame", hint_1: 'rhymes with shame', hint_2: "Don\'t ______ me."}, {word: "shelf", hint_1: 'rhymes with elf', hint_2: 'book'}, {word: "metro", hint_1: 'goes with opolis', hint_2: 'area'}],
[{word: "actual", hint_1: 'in the real', hint_2: 'Morpheus says blue pill!'}, {word: "orange", hint_1: '____ you glad', hint_2: 'is the new black!'}, {word: "famous", hint_1: 'Almost _____', hint_2: '15 minutes and counting'}]
]
@player_misses = 0
word_length = 0
def choose_random_word_hash(num)
random_word_hash = @array_of_secret_words[num].sample
end
def choose_random_word(num)
random_word_hash = choose_random_word_hash(num)
random_word_hash[:word]
end
def retrieve_and_process_word_length_response
word_length = ''
until word_length_valid?(word_length)
word_length = gets.chomp
puts "Please enter a 4, 5, or 6 to continue.\n\n" unless word_length_valid?(word_length)
end
word_length
end
def word_length_valid?(word_length)
word_length == "4" || word_length == "5" || word_length == "6"
end
def set_game_word(word_length)
if word_length == "4"
puts "\n\nFour letter word, huh? Familiar with those, are you?\n\n"
@game_word = choose_random_word(0)
elsif word_length == "5"
puts "\n\nAh, the middle way. I approve.\n\n"
@game_word = choose_random_word(1)
else
puts "\n\nYou like a challenge, I see. Let's get started.\n\n"
@game_word = choose_random_word(2)
end
end
def process_guess(guess)
if guess.length == 1
process_letter_guess(guess)
else
if guess == @game_word
process_correct_word_guess
else
process_player_word_miss
end
end
end
def retrieve_first_hint
game_word_length = @game_word.length
if game_word_length == 4
word_hash = @array_of_secret_words[0].select {|word_hash| word_hash[:word] == @game_word}
puts "\nHint: #{word_hash[0][:hint_1]}\n\n"
elsif game_word_length == 5
word_hash = @array_of_secret_words[1].select {|word_hash| word_hash[:word] == @game_word}
puts "\nHint: #{word_hash[0][:hint_1]}\n\n"
else
word_hash = @array_of_secret_words[2].select {|word_hash| word_hash[:word] == @game_word}
puts "\nHint: #{word_hash[0][:hint_1]}\n\n"
end
end
def retrieve_second_hint
game_word_length = @game_word.length
if game_word_length == 4
word_hash = @array_of_secret_words[0].select {|word_hash| word_hash[:word] == @game_word}
puts "\nHint: #{word_hash[0][:hint_2]}\n\n"
elsif game_word_length == 5
word_hash = @array_of_secret_words[1].select {|word_hash| word_hash[:word] == @game_word}
puts "\nHint: #{word_hash[0][:hint_2]}\n\n"
else
word_hash = @array_of_secret_words[2].select {|word_hash| word_hash[:word] == @game_word}
puts "\nHint: #{word_hash[0][:hint_2]}\n\n"
end
end
def process_letter_guess(guess)
letter_indices_array = @game_word_array.each_index.select { |index| @game_word_array[index] == guess }
if letter_indices_array.length == 0
process_player_letter_miss
else
process_player_letter_hit(letter_indices_array, guess)
if @user_progress == @game_word
process_player_completes_word
end
puts "\n\nWell, you've guessed a letter. Wonders upon wonders. Right now, your progress looks like this: #{@user_progress}\n\n"
end
end
def process_correct_word_guess
puts "\n\nWow. Didn't see that coming. You got it."
exit
end
def process_player_word_miss
@player_misses += 1
if @player_misses == 1
puts "\n\nYeah, that is not the word. You now have #{@player_misses} miss. Five and you are out. Guess again!\n\n"
elsif @player_misses == 2
puts "\n\nI feel like you need a hint. Perhaps more than one. Your word begins with the letter #{@game_word_array[0]}. Guess again!\n\n"
elsif @player_misses == 3
retrieve_first_hint
elsif @player_misses == 4
retrieve_second_hint
else
puts "\n\nFail, my friend. Fail. That letter is not in the word. You have #{@player_misses} misses. Guess again!\n\n"
end
end
def process_player_letter_miss
@player_misses += 1
if @player_misses == 1
puts "\n\nFail, my friend. Fail. That letter is not in the word. You have #{@player_misses} miss. Guess again!\n\n"
elsif @player_misses == 2
puts "\n\nI feel like you need a hint. Perhaps more than one. Your word begins with the letter #{@game_word_array[0]}. Guess again!\n\n"
elsif @player_misses == 3
retrieve_first_hint
elsif @player_misses == 4
retrieve_second_hint
else
puts "\n\nFail, my friend. Fail. That letter is not in the word. You have #{@player_misses} misses. Guess again!\n\n"
end
end
def process_player_letter_hit(letter_indices_array, guess)
letter_indices_array.each do |letter_index|
@user_progress[letter_index] = guess
end
end
def process_player_completes_word
puts "\n\nAmazing. You've guessed all of the letters: #{@user_progress}. As a wise man once said, 'Winner, winner, chicken dinner.'\n\n"
exit
end
###############
system 'clear'
puts "\n\nDo you want to guess a 4, 5, or 6-letter word?"
print ' >'
preferred_word_length = retrieve_and_process_word_length_response
set_game_word(preferred_word_length)
@game_word_array = @game_word.split('')
@user_progress = @game_word.gsub(/[A-Za-z]/, "_")
puts "Okay, a secret word has been chosen. You can try to guess the word or guess a letter in the word. Five misses and you're out. (And let's be honest, this is the likely outcome.) Guess away.\n\n"
until @player_misses == 5
print ' >'
guess = gets.chomp.downcase
process_guess(guess)
end
puts "\n\nAs I suspected might happen, you've lost. Better luck next time...emphasis on luck."
@switzersc
Copy link

Good stuff, but I want more hints!

Some ways to simplify this:

Lines 8-14 can really be changed to make more human sense and be less complicated:

word_length = gets.chomp
until word_length == "4" or word_length == "5" or word_length == "6"
  puts "Please enter a 4, 5, or 6 to continue."
  word_length = gets.chomp
end

You can use "/n" to insert a new line, instead of doing an empty puts.

Sweet. +1

@switzersc
Copy link

Whoops, I mean \n gives you a new line in the same string. :)

@switzersc
Copy link

Really good job refactoring this into clear, simple methods. My only comment is that you still use this explicit loop do / end syntax, which I guess is fine but it's not really very common and there are some cleaner ways of doing it.

Your code:

loop do
  word_length = gets.chomp
  if word_length_valid?(word_length)
    break
  end
  puts "Please enter a 4, 5, or 6 to continue.\n\n"
end

Some better ways to do this:

word_length = ""
until word_length_valid?(word_length)
  puts "Please enter a 4, 5, or 6 to continue.\n\n"
  word_length = gets.chomp
end

or if you insist on explicitly declaring a basic loop,

loop do 
  word_length = gets.chomp
  break if word_length_valid?(word_length)
  puts "Please enter a 4, 5, or 6 to continue.\n\n"
end

Actually, looking at my two suggestions, I rather like the latter one with the basic loop do/end. But either way is totally fine, and your original way is fine as well, just a bit verbose.

The only other thing I have to say is that your spacing is way weird, so get that straightened out. Otherwise, very well done!

@switzersc
Copy link

Oh, my only other comment at the moment is that your methods process_player_word_miss and process_player_letter_miss serve almost exactly the same purpose. You might consider refactoring this so that you have only one method and pass an argument that will just determine which message to output:

def process_player_miss(miss_type)
  @player_misses += 1
   if @player_misses == 1
     if miss_type == "letter"
   puts "\n\nFail, my friend. Fail. That letter is not in the word. You have #{@player_misses} miss. Guess again!\n\n"
     else 
       puts < message for missed word >
      end
   elsif @player_misses == 2
  puts "\n\nI feel like you need a hint. Perhaps more than one. Your word begins with the letter #{@game_word_array[0]}. Guess again!\n\n"
   elsif @player_misses == 3
  retrieve_first_hint
    elsif @player_misses == 4
  retrieve_second_hint
else
  puts "\n\nFail, my friend. Fail. That letter is not in the word. You have #{@player_misses} misses. Guess again!\n\n"
end
end

@switzersc
Copy link

Oh, my only other comment at the moment is that your methods process_player_word_miss and process_player_letter_miss serve almost exactly the same purpose. You might consider refactoring this so that you have only one method and pass an argument that will just determine which message to output:

def process_player_miss(miss_type)
  @player_misses += 1
   if @player_misses == 1
     if miss_type == "letter"
   puts "\n\nFail, my friend. Fail. That letter is not in the word. You have #{@player_misses} miss. Guess again!\n\n"
     else 
       puts < message for missed word >
      end
   elsif @player_misses == 2
  puts "\n\nI feel like you need a hint. Perhaps more than one. Your word begins with the letter #{@game_word_array[0]}. Guess again!\n\n"
   elsif @player_misses == 3
  retrieve_first_hint
    elsif @player_misses == 4
  retrieve_second_hint
else
  puts "\n\nFail, my friend. Fail. That letter is not in the word. You have #{@player_misses} misses. Guess again!\n\n"
end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment