From: Paul Smith on
On Wed, Dec 2, 2009 at 4:45 PM, Dylan Rodriguez <jovijunki84(a)yahoo.com> 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
>
> def guess(letter)
>
>  @guesses << letter
>
>  if @word.include?(letter)
>    @good_guesses[letter] = true
>  else
>    @bad_guesses << letter
>  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

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

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

>
> 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/.
>
>



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

paul(a)pollyandpaul.co.uk

First  |  Prev  | 
Pages: 1 2
Prev: Hangman Help
Next: Simple http.get() script fails