How Do I Change a .gsub

  • Thread starter Dylan Rodriguez
  • Start date
D

Dylan Rodriguez

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?
 
M

Martin DeMello

I'm trying to make a Hangman game

def guessed_word()

=A0 =A0 =A0 =A0word_letters =3D @word.split("")
=A0blank =3D @word.gsub(/[a-z]/, '_')

=A0return 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)
=A0 right =3D blank.gsub(/[_]/, @good_guesses.split)
end

Try maintaining the word as an array. Here's a basic sketch:

class Hangman
def initialize(word)
@word =3D word.split //
@good_guesses =3D {}
end

def display_word
@word.map {|i| @good_guesses ? i : "_"}.join(" ")
end

def guess(letter)
if @word.include? letter
@good_guesses[letter] =3D true
end
end
end

martin
 
D

Dylan Rodriguez

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...??
 
M

Martin DeMello

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
 
D

Dylan Rodriguez

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 : "_"}.join(" ")

return hidden_word
end

def to_s()
return guessed_word.split('').join(' ')
end
 
P

Paul Smith

class Hangman
def initialize(word=3Dnil)
=A0if word !=3D nil
=A0 =A0@word =3D word.split

This doesn't split the word up into individual letters - you need
@word=3Dword.split(//) for that AFAIK.
=A0else
=A0 =A0word =3D $dictionary[rand($dictionary.length)]# FIX
=A0 =A0@word =3D word.split
=A0end

=A0 =A0# the player's guesses
=A0 =A0@guesses =3D []
=A0 =A0@bad_guesses =3D []
=A0 =A0@good_guesses =3D {}

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.
=A0end

def guess(letter)

=A0@guesses << letter

=A0if @word.include?(letter)
=A0 =A0@good_guesses[letter] =3D true
=A0else
=A0 =A0@bad_guesses << letter
=A0end
end

def word()
=A0return @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
def total_guess_count()
=A0return @guess.length()
end

def bad_guess_count()
=A0return @bad_guesses.length()
end

def misses()
=A0return @bad_guesses
end

def lost?()
=A0lose_game =3D false

=A0if (bad_guess_count() =3D=3D MAX_GUESSES)
=A0 =A0lose_game =3D true
=A0end

=A0return lose_game
end

this could be written much more simply as

def lost?
bad_guess_count =3D=3D MAX_GUESSES
end

though you might find

def lost?
return bad_guess_count =3D=3D MAX_GUESSES
end

more readable
def guessed_word()

=A0hidden_word =3D @word.map {|i| @good_guesses ? i : "_"}.join(" ")

=A0return hidden_word
end

def to_s()
=A0return guessed_word.split('').join(' ')
end




--=20
Paul Smith
http://www.nomadicfun.co.uk

(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top