added guessing feature (buggy)
This commit is contained in:
parent
7196c6ad37
commit
f3bd5a5e5d
36
hangman.rb
36
hangman.rb
@ -16,15 +16,17 @@ def generate_hidden_letters(word)
|
|||||||
return hidden_letters
|
return hidden_letters
|
||||||
end
|
end
|
||||||
|
|
||||||
def scramble_word(word, hidden_letters)
|
def scramble_word(word, hidden_letters, scrambled_word, guess)
|
||||||
hidden_letters.each do |letter| #for each hidden letter
|
hidden_letters.each do |letter| #for each hidden letter
|
||||||
word.length.times do |i| #check where instances of the letter are in the word
|
word.length.times do |i|
|
||||||
if word[i] == letter #if you find one, replace it with a "_" in the string
|
if word[i] == letter #if you find one, replace it with a "_" in the string
|
||||||
word[i] = "_"
|
scrambled_word[i] = "_"
|
||||||
|
elsif word[i] == guess
|
||||||
|
scrambled_word[i] = guess
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return word
|
return scrambled_word
|
||||||
end
|
end
|
||||||
|
|
||||||
#loading file into an array
|
#loading file into an array
|
||||||
@ -42,10 +44,30 @@ end
|
|||||||
puts "Your word is #{word}"
|
puts "Your word is #{word}"
|
||||||
|
|
||||||
hidden_letters = generate_hidden_letters(word)
|
hidden_letters = generate_hidden_letters(word)
|
||||||
scrambled_word = scramble_word(word, hidden_letters)
|
guess = ""
|
||||||
|
scrambled_word = ""
|
||||||
|
scrambled_word << word
|
||||||
|
scrambled_word = scramble_word(word, hidden_letters, scrambled_word, guess)
|
||||||
|
puts "Your word is #{word} after =scramble"
|
||||||
|
|
||||||
#puts the array as a readable string
|
while true
|
||||||
puts scrambled_word.inspect[1...-1].gsub('"',"").gsub(',','')
|
|
||||||
|
puts "Word is #{word}"
|
||||||
|
puts "scrambled is #{scrambled_word}"
|
||||||
|
#puts the array as a readable string
|
||||||
|
puts scrambled_word.inspect[1...-1].gsub('"',"").gsub(',','')
|
||||||
|
|
||||||
|
print "Guess a letter: "
|
||||||
|
guess = gets.chop
|
||||||
|
p guess
|
||||||
|
p hidden_letters
|
||||||
|
if hidden_letters.include?(guess)
|
||||||
|
puts "INCLUDED"
|
||||||
|
hidden_letters.delete(guess)
|
||||||
|
scrambled_word = scramble_word(word, hidden_letters, scrambled_word, guess)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
#if input is included in word but not hidden word then
|
#if input is included in word but not hidden word then
|
||||||
#if word[x] == input then hidden[x] == input
|
#if word[x] == input then hidden[x] == input
|
||||||
|
Loading…
x
Reference in New Issue
Block a user