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?
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…??
On Wed, Dec 2, 2009 at 11:46 AM, Dylan R. [email protected]
wrote:
the array for the correct guesses made by the user is @good_guesses and
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
On Wed, Dec 2, 2009 at 9:55 PM, Dylan R. [email protected]
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
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
On Wed, Dec 2, 2009 at 4:45 PM, Dylan R. [email protected]
wrote:
class Hangman
def initialize(word=nil)
if word != nil
@word = word.split
This doesn’t split the word up into individual letters - you need
@word=word.split(//) for that AFAIK.
else
word = $dictionary[rand($dictionary.length)]# FIX
@word = word.split
end
the player’s guesses
@guesses = []
@bad_guesses = []
@good_guesses = {}
Why are you storing the guesses and the bad guesses and the good
guesses? You’re at least storing things twice here!
A single hash of guesses, where good guesses map to true and bad
guesses map to false is probably better.
end
end
def word()
return @word
end
attr_accessor :word at the top of the class def is more idiomatic.
For that matter, so i leaving off the empty () for method calls
return @bad_guesses
end
this could be written much more simply as
def lost?
bad_guess_count == MAX_GUESSES
end
though you might find
def lost?
return bad_guess_count == MAX_GUESSES
end
more readable
end
Posted via http://www.ruby-forum.com/.
–
Paul S.
http://www.nomadicfun.co.uk
[email protected]