Created
April 24, 2010 23:35
-
-
Save jsomers/378046 to your computer and use it in GitHub Desktop.
Code to help analyze games of Ghost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Words = File.new("./pruned-words.txt").read.split("\n") | |
class String | |
def starts_with?(str) | |
self[0..str.length - 1] == str | |
end | |
end | |
# Players can never play "bones", e.g., since the game would end with "bone". | |
# The next two functions remove all 128,832 such extraneous words from our word list. | |
def prune(words) | |
rejects = [] | |
words.each_with_index do |w, i| | |
begin | |
while (nw = words[i + 1]).starts_with?(w) | |
rejects << nw unless w.length < 4 | |
i += 1 | |
end | |
rescue | |
end | |
end | |
words - rejects.uniq | |
end | |
def create_pruned_word_list | |
words, f = File.open("./WORD.LST").read.split("\n"), File.new("./pruned-words.txt", "w") | |
("a".."z").each {|l| f.puts prune(words.select {|w| w[0..0] == l}).join("\n")} | |
end | |
def alive(root, words=Words) | |
words.select {|w| w.starts_with?(root)} | |
end | |
def winner(word) | |
word.length % 2 == 0 ? "h" : "c" | |
end | |
def options(root, words=Words) | |
alive(root, words).collect {|w| root + w[root.length..-1][0..0]}.uniq | |
end | |
def scenario(root, words=Words) | |
puts (root.length % 2 == 0 ? "h" : "c") + " to move..." | |
(options(root) - [root]).each do |o| | |
puts "#{root}-#{o[-1..-1].upcase}:" | |
options(o).each do |op| | |
alive(op, words).each do |a| | |
puts " > #{a} (#{winner(a)})" | |
end | |
end | |
end | |
end | |
scenario("aeci") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment