-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.rb
51 lines (42 loc) · 1.21 KB
/
hangman.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'json'
require_relative 'hangman/game'
module Hangman
UNKNOWN_LETTER = '*'
class << self
private
def build_tree(chars, words, belongs_to_key)
# No more guess needed if key doesn't contain any unknown letter
unless belongs_to_key.include? UNKNOWN_LETTER
return {}
end
# 从可能单词中选出猜中几率最大的字母
optimal_c = chars.max_by do |char|
words.count { |word| word.include? char }
end
chars -= [optimal_c] # Do NOT ues Array#delete
reg = Regexp.new("[#{chars.join}]")
tree = words.group_by { |word| word.gsub reg, UNKNOWN_LETTER }
# build sub tree
tree.each do |key, value|
tree[key] = build_tree chars, value, key
end
# Return optimal solution and sub tree
{
:c => optimal_c,
:t => tree
}
end
end
def self.build_decision_tree(word_list_file)
words = File.read("./data/en.txt").upcase.strip.split
tree = words.group_by do |word|
'*' * word.size
end
tree.each do |key, value|
tree[key] = build_tree ('A'..'Z').to_a, value, key
end
end
def self.play(game)
game.play
end
end