reordered code and made the word scrabler follow the rules of hangman by hiding a set of letters instead of random spots in the string
This commit is contained in:
parent
66a7e236a8
commit
7196c6ad37
54
hangman.rb
54
hangman.rb
@ -1,16 +1,34 @@
|
||||
#hangman game
|
||||
#01/21/2022
|
||||
|
||||
#load in dictionary file
|
||||
#select random word between 5 and 12 letters long
|
||||
a = Array.new
|
||||
|
||||
def pick_word(a)
|
||||
rand_int = rand(0..a.size)
|
||||
return a[rand_int].chop!
|
||||
end
|
||||
|
||||
def generate_hidden_letters(word)
|
||||
hidden_letters = Array.new
|
||||
word.length.times do |i|
|
||||
if rand(0..1) == 0 #do this 50% of the time
|
||||
hidden_letters.push(word[i]) #create an array of letters to be hidden
|
||||
end
|
||||
end
|
||||
return hidden_letters
|
||||
end
|
||||
|
||||
def scramble_word(word, hidden_letters)
|
||||
hidden_letters.each do |letter| #for each hidden letter
|
||||
word.length.times do |i| #check where instances of the letter are in the word
|
||||
if word[i] == letter #if you find one, replace it with a "_" in the string
|
||||
word[i] = "_"
|
||||
end
|
||||
end
|
||||
end
|
||||
return word
|
||||
end
|
||||
|
||||
#loading file into an array
|
||||
a = Array.new
|
||||
dict = File.open('5desk.txt')
|
||||
dict.each do |row|
|
||||
a.push(row)
|
||||
@ -21,14 +39,14 @@ word = pick_word(a)
|
||||
until word.length.between?(5, 12)
|
||||
word = pick_word(a)
|
||||
end
|
||||
|
||||
puts "Your word is #{word}"
|
||||
|
||||
#turn word into an array
|
||||
#hide 50% of letters randomly
|
||||
#ask user to guess letters
|
||||
#have two arrays, word and hidden word
|
||||
#display hidden word
|
||||
hidden_letters = generate_hidden_letters(word)
|
||||
scrambled_word = scramble_word(word, hidden_letters)
|
||||
|
||||
#puts the array as a readable string
|
||||
puts scrambled_word.inspect[1...-1].gsub('"',"").gsub(',','')
|
||||
|
||||
#if input is included in word but not hidden word then
|
||||
#if word[x] == input then hidden[x] == input
|
||||
#do this over the array
|
||||
@ -37,19 +55,3 @@ puts "Your word is #{word}"
|
||||
#else
|
||||
# increase hangman counter
|
||||
|
||||
def scramble_word(word)
|
||||
scrambled_word = Array.new
|
||||
word.length.times do |i|
|
||||
if rand(0..1) == 0 #do this 50% of the time
|
||||
scrambled_word.push("_")
|
||||
else
|
||||
scrambled_word.push(word[i])
|
||||
end
|
||||
end
|
||||
return scrambled_word
|
||||
end
|
||||
|
||||
scrambled_word = scramble_word(word)
|
||||
#puts the array as a readable string
|
||||
puts scrambled_word.inspect[1...-1].gsub('"',"").gsub(',','')
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user