Class: Dat::Leaves
- Inherits:
-
Object
- Object
- Dat::Leaves
- Defined in:
- lib/dat/leaves.rb
Instance Method Summary collapse
- #[](word) ⇒ Object
- #calculate ⇒ Object
- #get(size) ⇒ Object
-
#initialize(game, depth = 4) ⇒ Leaves
constructor
A new instance of Leaves.
- #leaf?(word) ⇒ Boolean
- #level(word) ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(game, depth = 4) ⇒ Leaves
Returns a new instance of Leaves.
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/dat/leaves.rb', line 3 def initialize(game, depth=4) @game = game @logic = @game.logic @dict = @game.dict @depth = depth @leaves = Array.new(@depth+1) { {} } calculate end |
Instance Method Details
#[](word) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/dat/leaves.rb', line 14 def [](word) 1.upto(@depth) do |i| leaf = @leaves[i][word] return leaf if leaf end nil end |
#calculate ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/dat/leaves.rb', line 38 def calculate @dict.each do |k,v| result = @logic.perturb(k) size = result.size @leaves[size][k] = result if size <= @depth end end |
#get(size) ⇒ Object
33 34 35 36 |
# File 'lib/dat/leaves.rb', line 33 def get(size) return nil if size > @depth @leaves[size] end |
#leaf?(word) ⇒ Boolean
22 23 24 |
# File 'lib/dat/leaves.rb', line 22 def leaf?(word) level(word) end |
#level(word) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/dat/leaves.rb', line 26 def level(word) 1.upto(@depth) do |i| leaf = @leaves[i][word] return i if leaf end end |
#update ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dat/leaves.rb', line 46 def update @game.last.relatives.clone.add(@game.last).each do |w| #@game.used.each do |u| @logic.perturb(u.get, @game.used).each do |w| 1.upto(@depth) do |i| @leaves[i].delete(u.get) if @leaves[i][u.get] leaf = @leaves[i][w.get] if leaf && leaf.include?(w) (@leaves[i-1][w.get] = leaf.clone).delete(w) @leaves[i].delete(w.get) end end end end @leaves[0] # Immediate winning moves end |