------art_7791_32033177.1184181018598
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

2007/7/11, James Edward Gray II <james / grayproductions.net>:
> Not many people solved this one, so I wanted to see if it was too
> tough.  Didn't seem so:
>
I was busy until today.

Here's my solution and my first quiz solution since I learn Ruby. It
wasn't that hard. I learned a lot about Ruby with it.

>ruby hangman.rb [-wrd] [-lfes]
If you don't pass a word it will choose a random word from the
dictionary. Lifes is set to 6 if you don't provide it.

<code>
#Ruby Quiz 130
#solution by Thomas Wieczorek <wieczo.yo / googlemail.com>
#11.07.2007
require 'yaml'
$debug  rue

class Player

public

  def initialize(word)
    @word  ord
    @dictionary  AML.load_file(DICTIONARYFILE)
    @letters  'a'..'z').to_a
    @guessed  ]
    scan_dictionary(word)
  end

  def guess()
    return @dictionary[0] if @dictionary.length 1
    while (true)
      letter  probabilities.pop
      next if @guessed.include?(letter[0])
      @guessed << letter[0]
      break
    end
    return letter[0]
  end

  def wordalue)

    if not value.include?(".") then
      #lost
      #unknown word
      if not @dictionary.include?(value) then
        @dictionary  oad_dictionary()
        @dictionary << value
        File.open("dictionary.yaml", "w") { |f| YAML.dump(@dictionary, f) }
      end
    else
      if @word.eql?(value) then
        @word  alue
        scan_dictionary(value)
      end
    end
  end

private

  DICTIONARYFILE  dictionary.yaml"

  def scan_dictionary(masked)
    @dictionary  dictionary or load_dictionary()
    @dictionary  dictionary.grep(Regexp.new("^#{@word}$"))
    set_probability()
  end

  def set_probability
    alphabet  'a'..'z').to_a
    @probabilities  }
    alphabet.each { |l| @probabilities[l]   }
    @dictionary.each do |word|
      word.each_byte do |letter|
        #p letter
        l  etter.chr
        @probabilities[l] +  if alphabet.include?(l)
      end
    end

    @probabilities  probabilities.sort {|a,b| a[1]<[1]}
  end

  def load_dictionary()
    return YAML.load_file(DICTIONARYFILE)
  end
end #of Player

def random_word
  words  AML.load_file("dictionary.yaml")
  return words[rand(words.length)]
end

def check_for_letters(word, guess, masked_word)
  if word.include?(guess) then
    #puts "#{guess} is in #{word}"
    word.length.times do |i|
      if word[i].chr guess then
        masked_word[i]  uess
      end
    end
  end

  return masked_word
end

def play_game(word  ", lifes  , give_output  alse)
  #user given word
  word  andom_word if word ""
  masked_word  ord.gsub(/\w/, ".")
  guess  "

  player  layer.new(masked_word)

  while(lifes > 0)
    #AI guesses a letter or word
    puts "AI is looking for >#{masked_word}<" if give_output
    guess  layer.guess()
    new_word  "
    won  alse
    puts "AI guessed '#{guess}'"  if give_output
    if guess.length 1 then
      masked_word  heck_for_letters(word, guess, masked_word)

    else
      if guess.length > 1 then
        break if guess word
        lifes - 
        next
      else
        #nil
      end
    end

    #wrong guess
    if not masked_word.include?(guess) then
      lifes - 
      puts "AI lost a life. #{lifes} lifes left."
    else
      #found word
      if masked_word word then
        break
      else
        #found a letter
        player.word  asked_word
        next
      end
    end
  end

  if lifes > 0 then
    won  rue
  else
    #give word to player to extend dictionary
    player.word  ord
    won  alse
  end

  return won, word, lifes
end #of play_game

won  alse
word  "
lifes  
if ARGV.length > 0
  ARGV.each do |arg|
    option  rg.split("
    case option[0]
      when "-w"
        word  ption[1]
      when "-l"
        lifes  ption[1].to_i
    end
  end
end

won, word, lifes  lay_game(word, lifes, true)

if won then
  puts "AI won! It guessed \"#{word}\" with #{lifes} lifes left."
else
  puts "Awww! Lost! AI couldn't guess \"#{word}\"."
end

</code>

------art_7791_32033177.1184181018598
Content-Type: application/octet-stream; name=hangman.rb
Content-Transfer-Encoding: base64
X-Attachment-Id: f_f406se2t
Content-Disposition: attachment; filename="hangman.rb"

I1J1YnkgUXVpeiAxMzANCiNzb2x1dGlvbiBieSBUaG9tYXMgV2llY3pvcmVrIDx3aWVjem8ueW9A
Z29vZ2xlbWFpbC5jb20+DQojdGhlIGRpY3Rpb25hcnkgY29tZXMgZnJvbSBodHRwOi8vd3d3Lmp1
bmUyOS5jb20vSURQL2ZpbGVzL0dlcm1hbi50eHQgSSByZW1vdmVkIHRoZSBkdXBsaWNh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