On Fri, 05 Jan 2007 22:05:52 +0900, Ruby Quiz wrote:
> This is a riff on the Jumble puzzle found in many (US) newspapers. More
> specifically, it's based on the game TextTwist[1], made by GameHouse[2] and
> published in various places around the web.
> 
> The mechanic of TextTwist is simple. The player is given six letters and is
> tasked with unscrambling those letters into as many words as possible. If the
> player can use all six letters in a word, they proceed to the next round.
> 
> Your task is to build the back-end engine to run a TextTwist clone. Effectively,
> this means that you must generate a list of three- to six-letter words that can
> all be constructed from the same six letters. This list must contain at least
> one six-letter word.
> 
> Bonus points for building a completely functional game!
> 
> 	[1]: http://games.yahoo.com/games/texttwist.html (just one example, java)
> 	[2]: http://www.gamehouse.com/

require 'rubygems'
require 'facets/core/enumerable/permutation'
require 'set'

#usage: texttwist [word]
# specifying a word will use /usr/share/dict/words to solve a TextTwist 
# problem. If no word is specified, a random word will be selected, to
# generate an round of texttwist.

#load dictionary
matcher=Set.new
allwords=Array.new
open("/usr/share/dict/words") do |f|
   f.each do |line|
      line.chomp!
      next if line !~ /^[a-z]+$/
      matcher << line if line.length<=6
      allwords << line if line.length==6
   end
end

#generate subwords of a word
word=ARGV[0] || allwords[rand(allwords.length)]
thiswordmatcher=Set.new
word.split(//).each_permutation do |perm|
   perm=perm.join
   (3..6).each do |len|
      candidate=perm[0,len]
      if matcher.include?(candidate)
	 thiswordmatcher << candidate
      end
   end
end

#output
puts word
puts "======"
thiswordmatcher.each do |subword|
   puts subword
end

-- 
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/