Forum: Ruby Re: Word Blender (#108)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D86ec78a7a258246684d15e09e51a170?d=identicon&s=25 David and Sharon Phillips (Guest)
on 2007-01-19 16:31
(Received via mailing list)
Hi,

I'm new to ruby and this is my first quiz entry, so
feel free to offer any feedback/suggestions etc.

Requires file english.txt (dicttionary) to be in same
dir as source.

Play is very basic as I've had to fit this in around
four young kids...
Enter a six letter word to progress to the next round
(another six letters), or as many smaller words as you
like to stay in the same round.

Cheers,
Dave

---------------------------------------------
?class Game

  def initialize
    @short_words= []
    @long_words= []
    get_dict
  end

  def get_dict
    puts "Reading File..."
    File.open("english.txt","r").each_line do |line|
      line.chomp!
      @short_words << line.downcase if
(3..5).include?(line.length)
      @long_words << line.downcase if line.length== 6
    end
    puts "Short words: " + @short_words.length.to_s
    puts "Six letter words: " +
@long_words.length.to_s
  end

  def start
    round_number= 0
    begin
      create_targets
    end while play_round(round_number+= 1)
  end

  def create_targets
    @target_word=
@long_words[rand(@long_words.length)]
    @target_letters= @target_word.clone
    10.times do
      pos= rand(6)
      @target_letters[0,1], @target_letters[pos,1]=
@target_letters[pos,1], @target_letters[0,1]
    end
  end

  def letter_match?(guess, target)
    0.upto(guess.length-1) do |i|
      letter= guess[i,1]
      if target.include?(letter) then
target[letter]=""
      else return false
      end
    end
    return true
  end

  def get_score(words_guessed, guess, target)
    points, round_end= 0, false
    if @short_words.include?(guess) ||
@long_words.include?(guess)
      if words_guessed.include?(guess)
        puts "You've used that word. No points this
time."
      else
        if letter_match?(guess, target)
          words_guessed << guess
          if guess.length==6
            puts "*** Well Done! You got the big one!
***"
            points= 1
            round_end= true
          else
            puts "Well done (+1 point)"
            points= 1
          end
        else puts "I know that word, but it doesn't
use the right letters. No points."
        end
      end
    else puts "I don't know that word"
    end
    return points, round_end
  end

  def play_round(round_number)
    words_guessed= []
    quit= false
    puts "\n\n---Round Number #{round_number}---"
    puts "\nLetters are: #{@target_letters.upcase}"
    begin
      print "\n[#{@target_letters.upcase}]
Points:#{words_guessed.length}. Guess (<Q>uit)>"
      guess= gets.downcase.chomp
      quit= (guess== 'q')
      if quit then puts "Bye"
      else points, round_end= get_score(words_guessed,
guess, @target_letters.clone)
      end
    end while not (round_end || quit)

    puts "You guessed #{words_guessed.sort.join(",
")}\nWord was #{@target_word}" if !quit
    return !quit
  end #play_round

end #class

game= Game.new
game.start




___________________________________________________________
What kind of emailer are you? Find out today - get a free analysis of
your email personality. Take the quiz at the Yahoo! Mail Championship.
http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2007-09-25 22:32
(Received via mailing list)
On Jan 11, 2007, at 3:37 PM, David and Sharon Phillips wrote:

> I'm new to ruby and this is my first quiz entry, so
> feel free to offer any feedback/suggestions etc.

I like it.  Looks nice and clean.  My only suggestion would be to
prefer string interpolation to addition.  For example:

"Short words: " + @short_words.length.to_s

Is simpler as:

"Short words: #{@short_words.length}"

Saves some to_s() calls at the very least.

James Edward Gray II
This topic is locked and can not be replied to.