Skip to content

Instantly share code, notes, and snippets.

@faizaanshamsi
Created November 17, 2013 00:18
Show Gist options
  • Save faizaanshamsi/7507206 to your computer and use it in GitHub Desktop.
Save faizaanshamsi/7507206 to your computer and use it in GitHub Desktop.
Things to improve: 1. You may only guess the word once, any input of more than 1 character will be treated as an attempt to guess the word. 2. There is no input validation, invalid input is treated as a guess 3. This doesn't track guesses you've already made
#!/usr/bin/env ruby
word_bank = ["this", "is", "the", "word", "bank"]
word = word_bank.sample
remaining_guesses = 10
guess = ""
display = "_" * word.length
until guess == word || remaining_guesses == 0 || display == word
puts "Word : #{display}"
puts "Chances remaining: #{remaining_guesses}"
print "Guess a single letter (a-z) or the entire word: "
guess = gets.chomp
if guess.length == 1
if word.include?(guess)
index = 0
match = []
occurences = word.scan(/#{guess}/).length
for i in 0...occurences do
match << word.index(guess, index)
index = match[i]+1
end
for i in 0...match.length
display[match[i]] = word[match[i]]
end
puts "#{occurences} occurences of #{guess} found"
else
puts "Sorry, no #{guess}'s found"
remaining_guesses -= 1
end
else #guess is a word
if guess == word
display = word
else
puts "Sorry, that's not the word"
remaining_guesses == 1
end
end
end
puts "Congratulations, you won!" if display == word
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment