#!/usr/bin/env ruby # # Copyright(C) Simon Howard # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # # def read_words all_words = [] File.open("/usr/share/dict/words") do |file| file.each_line do |line| line = line.chomp all_words.push(line.downcase) end end all_words end def find_possibles(word, tried_chars) tried_chars_str = "." if tried_chars.length > 0 tried_chars_str = "[^" + tried_chars.join + "]" end word = word.gsub("_", tried_chars_str) puts word possibles = [] re = /^#{word}$/ $all_words.each do |word| if word =~ re possibles.push(word) end end possibles end $all_words = read_words def solve(input) input =~ /Hangman:\s+(\w+).*\s(\w*)\s*$/ word, tried_chars = $1.downcase, $2.downcase tried_chars = tried_chars.split(//) for a in find_possibles(word, tried_chars) puts a end end loop do puts "paste from irc:" input = gets solve(input.chomp) end