[QUIZ] Hangman (#130)

The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

···

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Brian Candler

Most people are probably familiar with the game of Hangman. The first player
picks a word or phrase, and the second player has to guess it a letter at a
time. If they make six wrong guesses (i.e. the target word does not contain the
guessed letter), they lose. If they guess the entire word before then, they win.

This quiz is to make a Hangman guessing player in Ruby. Play should proceed as
follows:

  1. The program requests a word or phrase pattern, e.g. "-------".
  
  2. The program suggests a letter, or may guess the entire word or phrase.
  
  3. The user indicates which letter positions, if any, match that letter.
     If none match, a life is lost. If six (or configurable) lives are lost,
     the program loses.

The specification is otherwise open-ended to allow you to focus on whatever part
of the problem interests you. For example:

  * You may choose the form of user interface (e.g. terminal, GUI toolkit,
    web).
  
  * You can just show the number of wrong guesses made, or you can actually
    draw the hangman.
  
  * You may concentrate on improving the play, for example by using a
    dictionary to improve the guesses made at each stage. A suitable file
    is /usr/share/dict/words on many Linux systems.
  
  * A dynamic solution could start with an empty dictionary, and guess the
    answer by chance. If it fails, it would prompt the user for the word
    or phrase they were thinking of. It would add new words or phrases to
    its dictionary so as to become a better player over time.
  
  * You could investigate ways of precomputing a hangman decision tree,
    optimizing it for the minimum number of wrong guesses along each branch.
    The aim is to produce an unbeatable guesser for a given dictionary.
  
  * You may wish to consider how best to decouple the UI from the guessing
    logic, to enable different UI's to work with the same guessing engine,
    or vice versa.

Here's my solution. I tried to tackle many of the suggested ideas:

- Extensible interface and AI. Just create a new file for the interface/ai,
   give it the right filename, and implement the appropriate methods. Then
   call hangman.rb with the correct --interface or --ai option.

- I've implemented a simple text interface and one based on Ncurses, just to
   try it out. My ncurses code is ugly, but it works ok. I almost tried out
   some animation & color a la the rain.rb ncurses example, but have no time.

- Implemented a random AI and one that tries to match items in a dictionary,
   though it does not add new words to it. It greps through the dictionary
   file on each guess, but is still pretty quick with the 52848 word one I'm
   testing with.

Downsides:

  - Not much error checking. Inputing illegal positions and such is an easy
    crash.

  - Very little documentation.

hangman.rb is the executable, which creates an interface (subclass of
Interface::Core), and an AI (subclass of AI::Core), and passes them to a
Game object which controls the basic game flow. (Heh, 9 source files for
a quiz submission, a personal record.. :slight_smile:

Usage: hangman.rb [--interface | -i INTERFACE]
                  [--interface-arg | -j INTERFACE_ARGUMENT]
                  [--ai | -a AI]
                  [--ai-arg | -b AI_ARGUMENT]

Simple UI / Dictionary AI full example:

   cat dict\.txt   CAR   CAT   DOCK   DOOR   RUBY   ACORN   HINGE   ZEBRA    ./hangman.rb -a dictionary
  Enter a phrase pattern: ---
  --- | Computer lives: 6
  I guess A. What position(s) is it in? 2
  -A- | Computer lives: 6
  I guess C. What position(s) is it in? 1
  CA- | Computer lives: 6
  I guess R. What position(s) is it in?
  CA- | Computer lives: 5
  I guess T. What position(s) is it in? 3
  CAT | Computer lives: 5

  Woot! I win!

Ncurses UI / Random AI example end screen:

┌──────────────────────────────────────────────────────────────────────────────┐
│ Hangman | │
│---------+ │
│ │
│ Phrase: V-- │
│ │
│ Computer guess: M │
│ Positions? │
│ │
│ │
│ . I lost! │
│ 0 +--------+- │
│ | | │
│ _ | │
│ | | | │
│ + | │
│ -|- | │
│ / | \ | │
│ ^ | │
│ / \ | │
│ / \ | │
│ | │
│ ==================== │
│ │
└──────────────────────────────────────────────────────────────────────────────┘

ai_core.rb (639 Bytes)

hangman.rb (1.47 KB)

interface_core.rb (312 Bytes)

interface_n.rb (3.83 KB)

interface_text.rb (694 Bytes)

phrase.rb (529 Bytes)

ai_random.rb (275 Bytes)

game.rb (753 Bytes)

ai_dictionary.rb (1.58 KB)

···

--
Jesse Merriman
jessemerriman@warpmail.net
http://www.jessemerriman.com/

Ruby Quiz wrote:

This quiz is to make a Hangman guessing player in Ruby. Play should proceed as
follows:

I focused on building a program that makes good guesses.

== Algorithm overview

The guesser reads a dictionary and then builds a database (which is
reused) with the following information about each word:
* size
* positions of each character
* number of occurrences of each character

The basic algorithm is as follows:
* Remove all words that do not have a matching length.
* While the game has not been solved:
** Pick the character included in the most words still remaining.
** If the character is not in the word: remove all words with the character.
** If the character is in the word: remove all words that do not contain
the character at exactly the revealed positions.

=== Weaknesses

The algorithm is not optimal. The character that's included in the most
words is not necessarily the character which will give the largest
reduction in number of potential words since position is not considered.
Consider a dictionary with the following:

bdc
ebc
fcb

b and c are tied for number of occurrences, but b would be the better
choice. If we pick b we will in all cases be left with one potential
word. If we pick c and the word is one of the first two we get two
potential words.

My guess is that it's a good enough heuristic in most cases though.

Also note that this program is built on the assumption that the word is
picked randomly from the dictionary. More refined solutions could weigh
in the relative frequency of different words in normal English text.

== Speed

It takes about 40 minutes to create the database for a dictionary with
4*10^5 words, but it only has to be created once.

Computing all guesses for a word (i.e. from being given the length to
having the correct word) takes about 30 to 40 seconds for a dictionary
with 4*10^5 words. That time includes about 10 seconds to reset the
database from previous uses, another 10 seconds for pruning based on
word length and the rest for the remaining search.

=== Possible improvements

Much of the initial sorting could be precomputed (e.g. split words into
different table based on length and then only work against the table
with the specified length) to cut down on the time needed reset and do
the initial pruning. The first (and possibly some additional steps)
could also be precomputed.

== Dependencies

Requires a mysql database and the mysql-gem. You need to enter your
username, passwords and database name in HangmanGuesser#db_connection below.

== The code

#!/usr/bin/env ruby
# == Synopsis

···

#
# automated_hangman: plays a game of hangman with the word of your
# choice
#
# == Usage
#
# automated_hangman [OPTION] ... WORD
#
# -h, --help:
# show help
#
# -d, --dictionary [dictionary location]:
# sets up the database to use the specified dictionary (defaults to
# /usr/share/dict/words), can take some time
#
# WORD: The word that the program should try to guess.

require 'getoptlong'
require 'rdoc/usage'
require 'mysql'

# Describes a game of hangman.
class Hangman
  LIVES = 6

  # Creates a new game of hangman where word is the target word.
  def initialize(word)
    @guesses =
    @word_characters = word.chomp.downcase.split(//)
  end

  # Returns an array containing the incorrect guessed characters.
  def incorrect_guesses
    @guesses - @word_characters
  end

  # Guesses a specified character. Returns an array of indices (possibly
  # empty) where the character was found.
  def guess(char_guess)
    @guesses << char_guess
    indices =
    @word_characters.each_with_index do |character, index|
      indices << index if character == char_guess
    end
    return indices
  end

  # Returns a string representation of the current progress.
  def to_s
    hidden_characters = @word_characters - @guesses
    return @word_characters.join(' ') if hidden_characters.empty?
    @word_characters.join(' ').gsub(
      /[#{hidden_characters.uniq.join}]/, '_')
  end

  # Checks whether the player has won.
  def won?
    (@word_characters - @guesses).empty?
  end

  # Checks whether the player has lost.
  def lost?
    incorrect_guesses.size > LIVES
  end

  # Gets the number of characters in the word.
  def character_count
    @word_characters.size
  end
end

# The guessing machine which picks the guesses.
class HangmanGuesser
  # The location of the default dictionary to use.
  DICTIONARY_FILE = '/usr/share/dict/words'
  # An array of the characters that should be considered.
  CHARACTERS = ('a'..'z').to_a
  # Set this to true to see how the search progresses.
  VERBOSE = true
  # The maximum word length accepted.
  MAX_WORD_LENGTH = 50

  # The dictionary given should be the location of a file containing one
  # word per line. The characters should be an array of all characters
  # that should be considered (i.e. no words with other characters are
  # included).
  def initialize(hangman_game, characters = CHARACTERS)
    @con = self.class.db_connection
    @characters = characters
    @hangman_game = hangman_game

    reset_tables
    prune_by_word_length @hangman_game.character_count
  end

  # Returns the guesses that the guesser would make.
  def guesses
    @guesses =
    log{ "There are #{word_count} potential words left." }
    while not @hangman_game.won?
      guess = next_guess
      raise 'The word is not in the dictionary.' if guess.nil?
      @guesses << guess
      log{ "Guessing #{guess}" }
      add_information(guess, @hangman_game.guess(guess))
      log_state
      log{ "\n" }
    end
    return @guesses
  end

  class << self
    # Creates the database and populates it with the dictionary file
    # located at the specified location. Only considers the specified
    # characters (array).
    def create_database(dictionary = DICTIONARY_FILE,
        characters = CHARACTERS)
      @con = db_connection
      @characters = characters
      @tables = ['words'] + @characters +
        @characters.map{ |c| c + '_occurrences'}
      create_tables
      populate_tables File.open(dictionary)
    end

    # Connects to the database that should store the tables.
    def db_connection
      # Replace <username> and <password> with the database username and
      # password.
      Mysql.real_connect("localhost", <username>, <password>, "hangman")
    end

    private

    # Creates the tables used to store words.
    def create_tables
      # Drop old tables.
      @tables.each do |table|
        @con.query "DROP TABLE IF EXISTS `#{table}`"
      end

      # Words table.
      @con.query <<-"end_sql"
        CREATE TABLE `words` (
          `word_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
          `word` varchar(#{MAX_WORD_LENGTH}) NOT NULL,
          `length` tinyint(3) unsigned NOT NULL,
          `removed` tinyint(1) unsigned NOT NULL DEFAULT '0',
          PRIMARY KEY (`word_id`),
          INDEX (`removed`),
          INDEX (`length`)
        ) ENGINE=MyISAM
      end_sql

      # Tables for the number of occurrences of each character.
      character_occurrences_table_template =<<-'end_template'
        CREATE TABLE `%s_occurrences` (
          `word_id` mediumint(8) unsigned NOT NULL,
          `occurrences` tinyint(3) unsigned NOT NULL,
          PRIMARY KEY (`occurrences`, `word_id`),
          INDEX (`word_id`)
        ) ENGINE=MyISAM
      end_template

      # Tables for the positions of each character.
      character_table_template =<<-'end_template'
        CREATE TABLE `%s` (
          `word_id` mediumint(8) unsigned NOT NULL,
          `position` tinyint(3) unsigned NOT NULL,
          PRIMARY KEY (`position`, `word_id`),
          INDEX (`word_id`)
        ) ENGINE=MyISAM
      end_template

      @characters.each do |character|
        @con.query character_occurrences_table_template % character
        @con.query character_table_template % character
      end
    end

    # Loads a dictionary into the database.
    def populate_tables(dictionary_file)
      # Disable the keys so that we don't update the indices while
      # adding.
      @tables.each do |table|
        @con.query("ALTER TABLE #{table} DISABLE KEYS")
      end

      # Prepare statements.
      add_word = @con.prepare(
        "INSERT INTO words (word, length) VALUES (?, ?)")
      add_character = {}
      add_character_occurrences = {}
      @characters.each do |character|
        add_character[character] = @con.prepare(
          "INSERT INTO #{character} (word_id, position) VALUES (?, ?)")
        add_character_occurrences[character] = @con.prepare(
          "INSERT INTO #{character}_occurrences " +
          "(word_id, occurrences) VALUES (?, ?)")
      end

      # Populate the database.
      previous_word = nil
      dictionary_file.each_line do |line|
        # Only consider words that only contain characters a-z. Make
        # sure we don't get duplicates.
        word = line.chomp.downcase
        next if word == previous_word or word =~ /[^a-z]/ or
          word.size > MAX_WORD_LENGTH

        # Add the word, its character positions and number of
        # occurrences.
        add_word.execute(word, word.size)
        word_id = @con.insert_id
        characters = word.split(//)
        characters.each_with_index do |character, position|
          add_character[character].execute(word_id, position)
        end
        @characters.each do |character|
          occurrences = characters.select{ |c| c == character }.size
          add_character_occurrences[character].execute(
            word_id, occurrences)
        end

        previous_word = word
      end

      # Generate the indices.
      @tables.each do |table|
        @con.query("ALTER TABLE #{table} ENABLE KEYS")
      end
    end
  end

  private

  # Logs the current state of the guessing process.
  def log_state
    log do
      messages =
      messages << @hangman_game.to_s
      count = word_count
      messages << "There are #{count} potential words left."
      if count <= 10
        res = @con.query('SELECT word FROM words WHERE removed = 0')
        res.each{ |row| messages << row[0] }
        res.free
      end
      messages.join("\n")
    end
  end

  # Logs the string produced by the block (may not be executed at all).
  def log(&block)
    puts yield() if VERBOSE
  end

  # Gets the number of potential words left.
  def word_count
    res = @con.query('SELECT COUNT(*) FROM words WHERE removed = 0')
    count = res.fetch_row[0].to_i
    res.free
    return count
  end

  # Computes the next character that should be guessed. The next guess
  # is the character (that has not yet been tried) that occurrs in the
  # most words remaining.
  def next_guess
    next_character = nil
    max_count = 0
    (@characters - @guesses).each do |character|
      res = @con.query(
        "SELECT COUNT(DISTINCT word_id) FROM #{character} " +
        "NATURAL JOIN words WHERE removed = 0")
      count = res.fetch_row[0].to_i
      res.free
      if count > max_count
        next_character = character
        max_count = count
      end
    end
    return next_character
  end

  # Adds the information about at what indices in the word the specified
  # character can be found to the guesser.
  def add_information(character, indices)
    if indices.empty?
      # The character isn't in the word.
      sql =<<-"end_sql"
        UPDATE words SET removed = 1 WHERE removed = 0 AND word_id IN (
          SELECT word_id FROM #{character}
        )
      end_sql
    else
      # Remove all words where the character isn't at the specified
      # places.
      sql =<<-"end_sql"
        UPDATE words NATURAL JOIN #{character}_occurrences
        SET removed = 1
        WHERE removed = 0
          AND (occurrences != #{indices.size}
            OR word_id IN (
              SELECT word_id FROM #{character}
              WHERE position NOT IN (#{indices.join(', ')})
            )
          )
      end_sql
    end
    @con.query(sql)
  end

  # Resets the table to start a new round of guesses.
  def reset_tables
    @con.query('UPDATE words SET removed = 0')
  end

  # Prunes all words that do not have the specified length.
  def prune_by_word_length(expected_length)
    @con.query(
      "UPDATE words SET removed = 1 WHERE length != #{expected_length}")
  end
end

opts = GetoptLong.new(
  [ '--help', '-h', GetoptLong::NO_ARGUMENT],
  ['--dictionary', '-d', GetoptLong::OPTIONAL_ARGUMENT])
opts.each do |opt, arg|
  case opt
    when '--help'
      RDoc::usage
    when '--dictionary'
      if arg != ''
        HangmanGuesser.create_database(arg)
      else
        HangmanGuesser.create_database
      end
  end
end

if ARGV.size != 1
  abort "Incorrect usage, see --help"
end

game = Hangman.new(ARGV[0])
guesses = HangmanGuesser.new(game).guesses
if game.won?
  puts 'Successfully guessed the word.'
else game.lost?
  puts 'Failed guessing the word.'
end
puts "Made the following guesses: #{guesses.join(', ')}"
puts "Expended a total of #{game.incorrect_guesses.size} lives."

--
Andreas Launila

Maybe people aren't trying this quiz because they're busy. I hope
it's not because they find it uninteresting. I banged out something
fairly quickly during the weekend and was waiting to find some time to
clean it up. It seems I won't get that, so I'm going to post what I
have now.

It's a fairly simple algorithm that uses a combination of the
"established" frequency order of letters in the English language, as
seen on Linotype machines, and a dictionary (/usr/dict/share/words, of
course) to provide the next guess. What I find interesting about this
is how many common words it cannot get before exhausting its allotment
of wrong guesses. For instance, it won't get "book". Sometimes I'm
surprised with the guesses it gives. Maybe the dictionary I'm using
is *too* large.

···

-------------------------------------------------------------------------------
#!/usr/bin/env ruby

ALLOWED_WRONG_GUESSES = 6

WORD_LIST_FILENAME = '/usr/share/dict/words'

WORD_LIST = {}

File.open(WORD_LIST_FILENAME) do |f|
  f.each { |word| WORD_LIST[word.strip.downcase] = true }
end

FREQUENCY_ORDER = %w{etaoin shrdlu cmfwyp vbgkqj xz}.collect { |elem|
elem.split('') }.flatten

GUESSES_MADE = {}

puts 'Enter a word pattern'
old_pattern = pattern = gets.chomp

loop do
  if pattern.match(/^[a-zA-Z\-]+$/)
    if pattern.match(/-/)
      if GUESSES_MADE.values.select { |val| val == false }.length >
ALLOWED_WRONG_GUESSES
        puts 'crap I lost'
        exit
      end

      regex = Regexp.new("^#{pattern.downcase.gsub(/-/, '.')}$")
      possible_words = WORD_LIST.keys.select { |word|
word.match(regex) }
      possible_letters = possible_words.collect { |word|
word.split('') }.flatten.uniq
      guess = ((FREQUENCY_ORDER - GUESSES_MADE.keys) &
possible_letters).first
      puts guess.upcase
      pattern = gets.chomp

      GUESSES_MADE[guess] = (pattern != old_pattern)
      puts "wrong guesses made: #{GUESSES_MADE.values.select { |val|
val == false }.length}"
      old_pattern = pattern
    else
      puts 'Yay I won'
      exit
    end
  else
    puts 'This pattern makes no sense.'
    exit
  end
end
-------------------------------------------------------------------------------

-yossef

Sorry it's a little late; I'm on vacation and have spent limited time on a computer with Ruby installed.
   
  My solution reads a dictionary stored in "words.txt." It uses this to construct an array of arrays containing the possible words for each word in the pattern, which is constantly updated according to feedback.
   
  I thought about making something based on making sure each word has a vowel and hardcoding in the general English language letter frequencies, but I realized that that would be compeltely unnecessary, as I could constantly calculate the true letter frequency for the possible letters. This is highly effective and surprisingly simple, although it could definitely benefit from some basic knowledge of English grammar and parts of speech.
   
  Unfortunately, this program is definitely not unbeatqable. Likewise, with only 6 lives, probably none of the others are as well. As expected, when facing the word "cwm," the program guesses AEOIUY ["A" is more frequent than "E" for three-letter words, apparently], and loses before it has a chance to guess correctly.
   
  $Words = (file=File.new("words.txt")).read.upcase.split(/\n/)
file.close
  def hangman_start
puts "Please enter word pattern."
word_pattern = gets.chomp
possible_words = []
word_pattern.split.length.times do |t|
  possible_words << $Words.select{ |word|
   word_pattern.split[t].length == word.length}
end

hangman_round word_pattern, possible_words
end
  $avail_letters= ("A".."Z").to_a
  def hangman_round(word_pattern, possible_words, lives=6)
guess(word_pattern, possible_words)
puts word_pattern
puts "Are there any #{$guess}s?\t\tComputer lives=#{lives}"
if gets.chomp=="y"
  puts "Please indicate all positions with a #{$guess}"
  puts "(0-indexed, comma-delimited)"
  gets.chomp.split(/,/).each{|pstr| word_pattern[pstr.to_i] = $guess}
  possible_words.each_index do |i|
   possible_words[i] = possible_words[i].select{|word|
    word.gsub(/[^#{$guess}]/, '_') ==
     word_pattern.split[i].gsub(/[^#{$guess}]/, '_')}
  end
else
  lives -= 1
  possible_words.each {|words| words.reject! {|word| word.index $guess}}
end
if word_pattern !~ /_/
  puts word_pattern
  puts "I win"
elsif lives > 0
  hangman_round(word_pattern, possible_words, lives)
else
  puts "You win"
end
end
  #Guesses by frequency analysis. If a letter appears in a possible word, it's a vote for
#that letter. If a word is possible more than once, that's multiple votes, but not
#if the letter appears multiple times in a possible word (it's still one possibility)
#It then removes that letter from $avail_letters and stores the guess into $guess
#for convenience
def guess(word_pattern, possible_words)
all_words = possible_words.flatten
guess = $avail_letters.sort_by {|c|
  all_words.select{|w|w.index c}.length}.last
$avail_letters -= [guess]
$guess = guess
end
  hangman_start

···

---------------------------------
Boardwalk for $500? In 2007? Ha!
Play Monopoly Here and Now (it's updated for today's economy) at Yahoo! Games.

Not many people solved this one, so I wanted to see if it was too tough. Didn't seem so:

#!/usr/bin/env ruby -wKU

puts "One moment..."
puts
words = File.open(ARGV.shift || "/usr/share/dict/words") do |dict|
   dict.inject(Hash.new) do |all, word|
     all.update(word.delete("^A-Za-z").downcase => true)
   end.keys
end

guesses = Array.new

loop do
   puts guesses.empty? ?
        "Please enter a word pattern (_ _ _ _ for example):" :
        "Please update your pattern according to my guess (_ i _ _ for example):"
   pattern = $stdin.gets.to_s.delete("^A-Za-z_")
   if pattern.include? "_"
     if (guesses - pattern.delete("_").split("")).size > 6
       puts "I'm out of guesses. You win."
       puts
       exit
     end
   else
     puts "I guessed your word. Pretty smart, huh?"
     puts
     exit
   end

   choices = words.grep(/\A#{pattern.tr("_", ".")}\Z/i)
   odds = Hash.new(0)
   choices.each do |word|
     word.split("").each do |l|
       next if guesses.include? l
       odds[l] += word.count(l)
     end
   end
   guess = odds.max { |(_, c1), (_, c2)| c1 <=> c2 }.first rescue nil

   guesses << guess
   puts "I guess the letter '#{guess}'."
   puts
end

__END__

James Edward Gray II

A second version of my code. Minor changes mainly to support me testing it. I tried different guessing algorithms, but nothing beat the original strategy with my dictionary.

First, this is a tiny words.rb lib used by both the guesser and test scripts:

#!/usr/bin/env ruby -wKU

WORDS_CASH_FILE = "words.cache"

if File.exist? WORDS_CASH_FILE
   WORDS = File.open(WORDS_CASH_FILE) { |file| Marshal.load(file) }
else
   WORDS = File.open( ARGV.find { |arg| arg =~ /\A[^-]/ } ||
                      "/usr/share/dict/words" ) do |dict|
     dict.inject(Hash.new) do |all, word|
       all.update(word.delete("^A-Za-z").downcase => true)
     end.keys
   end
   File.open(WORDS_CASH_FILE, "w") { |file| Marshal.dump(WORDS, file) }
end

__END__

Next, my guesser script which works the same as yesterday's version but is optimized here and there for testing:

#!/usr/bin/env ruby -wKU

puts "One moment..."
puts
require "words"

choices = WORDS
guesses = Array.new

loop do
   puts guesses.empty? ?
        "Please enter a word pattern (_ _ _ _ for example):" :
        "Please update your pattern according to my guess (_ i _ _ for example):"
   $stdout.flush
   pattern = $stdin.gets.to_s.delete("^A-Za-z_")

   if (guesses - pattern.delete("_").split("")).size > 5 and pattern.include? "_"
     puts "I'm out of guesses. You win."
   elsif not pattern.include? "_"
     puts "I guessed your word. Pretty smart, huh?"
   else
     choices = choices.grep(/\A#{pattern.tr("_", ".")}\Z/i)
     odds = Hash.new(0)
     choices.each do |word|
       word.split("").each do |l|
         next if guesses.include? l
         odds[l] += word.count(l)
       end
     end
     guess = odds.max { |(_, c1), (_, c2)| c1 <=> c2 }.first rescue nil

     guesses << guess
     puts "I guess the letter '#{guess}'."
     puts
     next
   end

   puts
   if ARGV.include? "--loop"
     choices = WORDS
     guesses = Array.new
   else
     break
   end
end

__END__

Finally, this is my test script:

#!/usr/bin/env ruby -wKU

require "words"

results = Hash.new(0)
at_exit do
   results[:total] = results[:right] + results[:wrong]
   puts
   puts "Words: #{results[:total]}"
   puts "Guessed: #{results[:right]}"
   puts "Missed: #{results[:wrong]}"
   printf "Accuracy: %.2f%%\n", results[:right] / results[:total].to_f * 100
   puts
end
trap("INT") { exit }

IO.popen( File.join(File.dirname(__FILE__), "hangman.rb --loop"),
           "r+" ) do |hangman|
   WORDS.each do |word|
     pattern = word.tr("a-z", "_")
     loop do
       input = String.new
       hangman.each do |line|
         input << line
         break if input =~ /^(?:I'm out|I guessed)|:\Z/
       end

       if input =~ /^I'm out/
         puts "It missed '#{word}'."
         results[:wrong] += 1
         break
       elsif input =~ /^I guessed/
         puts "It guessed '#{word}'."
         results[:right] += 1
         break
       elsif input =~ /^I guess the letter '(.)'/
         guess = $1
         word.split("").each_with_index do |letter, i|
           pattern[i, 1] = letter if letter == guess
         end
       end

       hangman.puts pattern
     end
   end
end

__END__

James Edward Gray II

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 [-w=word] [-l=lifes]

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

class Player

public

  def initialize(word)
    @word = word
    @dictionary = YAML.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 word=(value)

    if not value.include?(".") then
      #lost
      #unknown word
      if not @dictionary.include?(value) then
        @dictionary = load_dictionary()
        @dictionary << value
        File.open("dictionary.yaml", "w") { |f| YAML.dump(@dictionary, f) }
      end
    else
      if @word.eql?(value) then
        @word = value
        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] = 0 }
    @dictionary.each do |word|
      word.each_byte do |letter|
        #p letter
        l = letter.chr
        @probabilities[l] += 1 if alphabet.include?(l)
      end
    end

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

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

def random_word
  words = YAML.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] = guess
      end
    end
  end

  return masked_word
end

def play_game(word = "", lifes = 6, give_output = false)
  #user given word
  word = random_word if word == ""
  masked_word = word.gsub(/\w/, ".")
  guess = ""

  player = Player.new(masked_word)

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

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

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

  if lifes > 0 then
    won = true
  else
    #give word to player to extend dictionary
    player.word = word
    won = false
  end

  return won, word, lifes
end #of play_game

won = false
word = ""
lifes = 6
if ARGV.length > 0
  ARGV.each do |arg|
    option = arg.split("=")
    case option[0]
      when "-w"
        word = option[1]
      when "-l"
        lifes = option[1].to_i
    end
  end
end

won, word, lifes = play_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>

hangman.rb (3.79 KB)

···

2007/7/11, James Edward Gray II <james@grayproductions.net>:

A few words to my solution:
- It saves the dictionary in a yaml file. If the AI doesn't know a
word, it will add it to the dictionary
- it guesses letters which have the highest possibility

It is a bit rough. I am happy about every suggestion to improve it.

···

2007/7/11, Thomas Wieczorek <wieczo.yo@googlemail.com>:

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 [-w=word] [-l=lifes]
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.

A second version of my code.

One last tiny tweak.

First, this is a tiny words.rb lib used by both the guesser and test scripts:

#!/usr/bin/env ruby -wKU

WORDS_CASH_FILE = "words.cache"

if File.exist? WORDS_CASH_FILE
  WORDS = File.open(WORDS_CASH_FILE) { |file| Marshal.load(file) }
else
  WORDS = File.open( ARGV.find { |arg| arg =~ /\A[^-]/ } ||
                     "/usr/share/dict/words" ) do |dict|
    dict.inject(Hash.new) do |all, word|
      all.update(word.delete("^A-Za-z").downcase => true)
    end.keys

Changing that line to:

     end.keys.sort_by { |w| [w.length, w] }

helps me see where I'm at in the tests.

  end
  File.open(WORDS_CASH_FILE, "w") { |file| Marshal.dump(WORDS, file) }
end

__END__

James Edward Gray II

···

On Jul 11, 2007, at 1:47 PM, James Edward Gray II wrote:

Last tweak, I promise!

Here's a new version of the guesser script that fairs a bit better. I found the improvement by trying some changes and running my testing script:

#!/usr/bin/env ruby -wKU

puts "One moment..."
puts
require "words"

def frequency(words)
   freq = Hash.new(0)
   words.each do |word|
     word.split("").each { |letter| freq[letter] += word.count(letter) }
   end
   freq
end
FREQ = frequency(WORDS).sort_by { |_, count| -count }.map { |letter, _| letter }

choices = WORDS
guesses = Array.new

loop do
   puts guesses.empty? ?
        "Please enter a word pattern (_ _ _ _ for example):" :
        "Please update your pattern according to my guess (_ i _ _ for example):"
   $stdout.flush
   pattern = $stdin.gets.to_s.delete("^A-Za-z_")

   if (guesses - pattern.delete("_").split("")).size > 5 and pattern.include? "_"
     puts "I'm out of guesses. You win."
   elsif not pattern.include? "_"
     puts "I guessed your word. Pretty smart, huh?"
   else
     choices = choices.grep(/\A#{pattern.tr("_", ".")}\Z/i)
     guess = frequency(choices).
               reject { |letter, _| guesses.include? letter }.
               sort_by { |letter, count| [-count, FREQ.index(letter)] }.
               first.first rescue nil

     guesses << guess
     puts "I guess the letter '#{guess}'."
     puts
     next
   end

   puts
   if ARGV.include? "--loop"
     choices = WORDS
     guesses = Array.new
   else
     break
   end
end

__END__

James Edward Gray II

This time I really mean last. :wink:

I found another change that improves the algorithm:

#!/usr/bin/env ruby -wKU

puts "One moment..."
puts
require "words"

def frequency(words)
   freq = Hash.new(0)
   words.each do |word|
     word.split("").each { |letter| freq[letter] += word.count(letter) }
   end
   freq
end
FREQ = frequency(WORDS).sort_by { |_, count| -count }.map { |letter, _| letter }

choices = WORDS
guesses = Array.new

loop do
   puts guesses.empty? ?
        "Please enter a word pattern (_ _ _ _ for example):" :
        "Please update your pattern according to my guess (_ i _ _ for example):"
   $stdout.flush
   pattern = $stdin.gets.to_s.delete("^A-Za-z_")

   bad_guesses = guesses - pattern.delete("_").split("")
   if bad_guesses.size > 5 and pattern.include? "_"
     puts "I'm out of guesses. You win."
   elsif not pattern.include? "_"
     puts "I guessed your word. Pretty smart, huh?"
   else
     choices = choices.grep(
                 bad_guesses.empty? ?
                 /\A#{pattern.tr("_", ".")}\Z/i :
                 /\A(?!.*[#{bad_guesses.join}])#{pattern.tr("_", ".")}\Z/i
               )
     guess = frequency(choices).
               reject { |letter, _| guesses.include? letter }.
               sort_by { |letter, count| [-count, FREQ.index(letter)] }.
               first.first rescue nil

     guesses << guess
     puts "I guess the letter '#{guess}'."
     puts
     next
   end

   puts
   if ARGV.include? "--loop"
     choices = WORDS
     guesses = Array.new
   else
     break
   end
end

__END__

James Edward Gray II

···

On Jul 11, 2007, at 3:42 PM, James Edward Gray II wrote:

Last tweak, I promise!

Sorry, it is late

Please replace

word = "Salawzander"

with
word = ""

hangman.rb (4.45 KB)