Class: Boggle::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/boggle/game.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(board = nil, opts = nil) ⇒ Game

Returns a new instance of Game.



32
33
34
35
36
37
38
39
40
41
# File 'lib/boggle/game.rb', line 32

def initialize(board=nil, opts=nil)
  if ! board.nil?
    @board = board
  else
    @board = Boggle::Board.new
  end
  @opts = opts
  @words = []
  @trie = Boggle::Trie.new
end

Class Method Details

.create_dictionary(file_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/boggle/game.rb', line 8

def self.create_dictionary(file_name)
  trie = Boggle::Trie.new
  File.open(file_name).each_line do |line|
    idx = line.index " " # get first space
    if idx.nil? # no definition
      word, defn = line, "no definition available"
    else
      word, defn = line[0..idx-1], line[idx+1..line.size]
    end
    #word.chomp! # case where we had just words on the line

    # skip words that are too small for boggle
    if word.size > 2
      trie[word.upcase] = defn
    end
  end

  dump = Marshal.dump(trie)
  dict_file = File.new(File.expand_path("../../../dicts/#{file_name.chomp('.txt')}.dict", __FILE__), "w")
  dict_file = Zlib::GzipWriter.new(dict_file)
  dict_file.write dump
  dict_file.close
end

Instance Method Details

#startObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/boggle/game.rb', line 43

def start
  dict = @opts[:dictionary]
  fill_trie(dict) # need to only load this once
  s = Boggle::Solver.new(@trie)
  @found_pairs = s.start(@board)

  display

  good = []
  bad = []
  @words.uniq.each do |w|
    if @found_pairs.delete w
      good << w
    else
      bad << w
    end
  end

  finish good, bad
end