Prev: Hangman Help
Next: Simple http.get() script fails
From: Dylan Rodriguez on 2 Dec 2009 01:16 I'm trying to make a Hangman game def guessed_word() word_letters = @word.split("") blank = @word.gsub(/[a-z]/, '_') return blank end the array for the correct guesses made by the user is @good_guesses and incorrect is @bad_guesses So, my game will print out the right number of blanks for however many letters are in the word...but I don't know how to add the elements from @good_guesses into the correct spots. I've tried things like if @word.include?(@good.guesses) right = blank.gsub(/[_]/, @good_guesses.split) end and things in that nature i always end up getting an error i have no idea what to do at this point and i've tried like 1000 combinations and i can't find an explanation anywhere please help? -- Posted via http://www.ruby-forum.com/.
From: Martin DeMello on 2 Dec 2009 01:43 On Wed, Dec 2, 2009 at 11:46 AM, Dylan Rodriguez <jovijunki84(a)yahoo.com> wrote: > I'm trying to make a Hangman game > > def guessed_word() > > word_letters = @word.split("") > blank = @word.gsub(/[a-z]/, '_') > > return blank > end > > the array for the correct guesses made by the user is @good_guesses and > incorrect is @bad_guesses > > So, my game will print out the right number of blanks for however many > letters are in the word...but I don't know how to add the elements from > @good_guesses into the correct spots. > > I've tried things like > > if @word.include?(@good.guesses) > right = blank.gsub(/[_]/, @good_guesses.split) > end Try maintaining the word as an array. Here's a basic sketch: class Hangman def initialize(word) @word = word.split // @good_guesses = {} end def display_word @word.map {|i| @good_guesses[i] ? i : "_"}.join(" ") end def guess(letter) if @word.include? letter @good_guesses[letter] = true end end end martin
From: Dylan Rodriguez on 2 Dec 2009 11:25 I tried what you suggested and it will give either a can't covert string to integer error. and idk why. or it'll add correct guesses to incorrect guesses...?? -- Posted via http://www.ruby-forum.com/.
From: Martin DeMello on 2 Dec 2009 11:34 On Wed, Dec 2, 2009 at 9:55 PM, Dylan Rodriguez <jovijunki84(a)yahoo.com> wrote: > I tried what you suggested and it will give either a can't covert string > to integer error. and idk why. or it'll add correct guesses to incorrect > guesses...?? Paste your complete code in and I'll take a look. martin
From: Dylan Rodriguez on 2 Dec 2009 11:45
class Hangman def initialize(word=nil) if word != nil @word = word.split else word = $dictionary[rand($dictionary.length)]# FIX @word = word.split end # the player's guesses @guesses = [] @bad_guesses = [] @good_guesses = {} end def guess(letter) @guesses << letter if @word.include?(letter) @good_guesses[letter] = true else @bad_guesses << letter end end def word() return @word end def total_guess_count() return @guess.length() end def bad_guess_count() return @bad_guesses.length() end def misses() return @bad_guesses end def lost?() lose_game = false if (bad_guess_count() == MAX_GUESSES) lose_game = true end return lose_game end def guessed_word() hidden_word = @word.map {|i| @good_guesses[i] ? i : "_"}.join(" ") return hidden_word end def to_s() return guessed_word.split('').join(' ') end -- Posted via http://www.ruby-forum.com/. |