boggle_solver 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,12 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "boggle_solver"
3
3
  s.summary = "Find all words in a letter matrix"
4
- s.description = File.read(File.join(File.dirname(__FILE__), 'README.markdown'))
4
+ s.description = "Ruby program that employs graph theory to solve a word matrix puzzle (Boggle).
5
+
6
+ # Introduction
7
+ Boggle Master consists of a 5x5 grid of letters. The goal of the game is to make as many words as possible from this grid by connecting neighboring letters. To generate a random puzzle the game uses 25 dice with letters on each side. The dice are shaken and each fall into a slot in the grid."
5
8
  s.requirements = "[ 'An installed dictionary (most Unix systems have one)']"
6
- s.version = "0.0.3"
9
+ s.version = "0.0.4"
7
10
  s.author = "Casey Robinson"
8
11
  s.email = "kc@rampantmonkey.com"
9
12
  s.homepage = "http://rampantmonkey.com"
@@ -6,21 +6,29 @@ module BoggleSolver
6
6
  class Board < AdjacencyMatrix
7
7
  attr_reader :words
8
8
 
9
- def initialize(mode=:default)
9
+ def initialize(mode=:default, input, dictionary_file)
10
10
  super(25)
11
11
  connect
12
12
  @board = []
13
13
  if mode == :random
14
14
  @prng = Random.new
15
15
  generate_random_board
16
+ elsif mode == :given
17
+ @board = input.split(//)
16
18
  else
17
19
  generate_board
18
20
  end
21
+ calculate_board_hash
19
22
  puts self
20
23
  @words = []
24
+ set_dictionary dictionary_file
21
25
  load_dictionary
22
26
  end
23
27
 
28
+ def set_dictionary file
29
+ @dictionary_file = file
30
+ end
31
+
24
32
  def connect
25
33
  j=0;
26
34
  while j<25 do
@@ -38,13 +46,15 @@ module BoggleSolver
38
46
 
39
47
  def generate_board
40
48
  @board = ["a", "r", "t", "a", "c", "c", "e", "a", "r", "d", "f", "e", "w", "o", "o", "n", "p", "l", "m", "i", "c", "r", "u", "b", "l"]
41
- calculate_board_hash
49
+ end
50
+
51
+ def load_board_from_string s
52
+ @board = s.split(//) if s.length == 25
42
53
  end
43
54
 
44
55
  def generate_random_board
45
56
  @board = Array.new(25)
46
57
  @board.map!{|item| (65.+rand(25)).chr.downcase!}
47
- calculate_board_hash
48
58
  end
49
59
 
50
60
  def calculate_board_hash
@@ -53,10 +63,9 @@ module BoggleSolver
53
63
  end
54
64
 
55
65
  def load_dictionary
56
- dictionary_file = "/usr/share/dict/words"
57
66
  @dictionary = Array.new
58
67
 
59
- File.foreach(dictionary_file) do |line|
68
+ File.foreach(@dictionary_file) do |line|
60
69
  next_word = line.chomp
61
70
  @dictionary.push next_word if possible? next_word
62
71
  end
@@ -5,13 +5,14 @@ module BoggleSolver
5
5
 
6
6
  DEFAULT_DICTIONARY = "/usr/share/dict/words"
7
7
 
8
- attr_reader :dictionary, :mode
8
+ attr_reader :dictionary, :mode, :input
9
9
 
10
10
  def initialize(argv)
11
11
  @dictionary = DEFAULT_DICTIONARY
12
12
  @mode = :default
13
13
  parse(argv)
14
- @input = argv
14
+ @input = argv[0]
15
+ @mode = :given unless argv.empty?
15
16
  end
16
17
 
17
18
  def parse(argv)
@@ -29,7 +30,7 @@ module BoggleSolver
29
30
  end
30
31
 
31
32
  begin
32
- argv = ["-h"] if argv.empty?
33
+ argv = ["-r"] if argv.empty?
33
34
  opts.parse!(argv)
34
35
  rescue OptionParser::ParseError => e
35
36
  STDERR.puts e.message, "\n", opts
@@ -9,7 +9,7 @@ module BoggleSolver
9
9
  end
10
10
 
11
11
  def run
12
- board = Board.new(@options.mode)
12
+ board = Board.new(@options.mode,@options.input,@options.dictionary)
13
13
  board.find_words
14
14
  p board.words
15
15
  end
Binary file
@@ -0,0 +1,43 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+ require_relative '../lib/boggle_solver/options'
4
+
5
+ class TestOptions < Test::Unit::TestCase
6
+
7
+ context "specifying nothing" do
8
+ setup do
9
+ @opts = BoggleSolver::Options.new([])
10
+ end
11
+
12
+ should "return default dictionary" do
13
+ assert_equal BoggleSolver::Options::DEFAULT_DICTIONARY, @opts.dictionary
14
+ end
15
+
16
+ should "set mode to random" do
17
+ assert_equal @opts.mode, :random
18
+ end
19
+ end
20
+
21
+ context "specifying a dictionary" do
22
+ should "return it" do
23
+ opts = BoggleSolver::Options.new(["-d", "mydict"])
24
+ assert_equal "mydict", opts.dictionary
25
+ end
26
+ end
27
+
28
+ context "specifying the board with a string and nothing else" do
29
+ should "return the board" do
30
+ opts = BoggleSolver::Options.new(["abcdefghijklmnopqrstuvwxy"])
31
+ assert_equal "abcdefghijklmnopqrstuvwxy", opts.input
32
+ assert_equal opts.mode, :given
33
+ end
34
+ end
35
+
36
+ context "specifying the board with a string and an extra word" do
37
+ should "return only the board" do
38
+ opts = BoggleSolver::Options.new(["abcdefghijklmnopqrstuvwxy", "extra stuff"])
39
+ assert_equal "abcdefghijklmnopqrstuvwxy", opts.input
40
+ assert_equal opts.mode, :given
41
+ end
42
+ end
43
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: boggle_solver
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Casey Robinson
@@ -11,45 +11,22 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-05-15 00:00:00.000000000Z
13
13
  dependencies: []
14
- description: ! "Ruby program that employs graph theory to solve a word matrix puzzle\
15
- \ (Boggle).\n\n# Introduction\nBoggle Master consists of a 5x5 grid of letters.\
16
- \ The goal of the game is to make as many words as possible from this grid by connecting\
17
- \ neighboring letters. To generate a random puzzle the game uses 25 dice with letters\
18
- \ on each side. The dice are shaken and each fall into a slot in the grid.\n\n#\
19
- \ Example Grid\n\nBelow is an example grid. One possible word in this graph is `wear`.\
20
- \ Arrows are added for convenience.\n\n J O X T Y\n\n A C D\
21
- \ F P\n\n Y K B E O\n\n A M G P W\n \
22
- \ |\n D S R<--A<--E\n\nThe goal of this project is to find all possible\
23
- \ words. Mostly to serve as a benchmark with which to compare my performance.\n\n\
24
- \n# Coordinate System\nBelow are the indicies used to represent the dice in the\
25
- \ board:\n\n 0 1 2 3 4\n 5 6 7 8 9\n 10 11 12 13 \
26
- \ 14\n 15 16 17 18 19\n 20 21 22 23 24\n\n# Connection Matrix\nAdjacency\
27
- \ matrix representing the connections in Boggle:\n\n 0 1 0 0 0 1 1 0 0 0 0 0\
28
- \ 0 0 0 0 0 0 0 0 0 0 0 0 0\n 1 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\
29
- \ 0\n 0 1 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n 0 0 1 0 1 0 0 1\
30
- \ 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0\
31
- \ 0 0 0 0 0\n 1 1 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0\n 1 1 1 0\
32
- \ 0 1 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0\n 0 1 1 1 0 0 1 0 1 0 0 1 1 1 0 0\
33
- \ 0 0 0 0 0 0 0 0 0\n 0 0 1 1 1 0 0 1 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0\n \
34
- \ 0 0 0 1 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0\n 0 0 0 0 0 1 1 0 0 0 0 1\
35
- \ 0 0 0 1 1 0 0 0 0 0 0 0 0\n 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 1 1 0 0 0 0 0 0\
36
- \ 0\n 0 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 1 1 0 0 0 0 0 0\n 0 0 0 0 0 0 0 1\
37
- \ 1 1 0 0 1 0 1 0 0 1 1 1 0 0 0 0 0\n 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 1\
38
- \ 0 0 0 0 0\n 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0 0 0\n 0 0 0 0\
39
- \ 0 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 1 1 0 0\n 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0\
40
- \ 1 0 1 0 0 1 1 1 0\n 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 0 0 1 1 1\n \
41
- \ 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 1\n 0 0 0 0 0 0 0 0 0 0 0 0\
42
- \ 0 0 0 1 1 0 0 0 0 1 0 0 0\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 0\
43
- \ 0\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 0\n 0 0 0 0 0 0 0 0\
44
- \ 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1\
45
- \ 0 0 0 1 0\n"
14
+ description: ! 'Ruby program that employs graph theory to solve a word matrix puzzle
15
+ (Boggle).
16
+
17
+
18
+ # Introduction
19
+
20
+ Boggle Master consists of a 5x5 grid of letters. The goal of the game is to make
21
+ as many words as possible from this grid by connecting neighboring letters. To generate
22
+ a random puzzle the game uses 25 dice with letters on each side. The dice are shaken
23
+ and each fall into a slot in the grid.'
46
24
  email: kc@rampantmonkey.com
47
25
  executables:
48
26
  - boggle_solver
49
27
  extensions: []
50
28
  extra_rdoc_files: []
51
29
  files:
52
- - boggle_solver-0.0.2.gem
53
30
  - boggle_solver.gemspec
54
31
  - README.markdown
55
32
  - bin/boggle_solver
@@ -59,8 +36,10 @@ files:
59
36
  - lib/boggle_solver/runner.rb
60
37
  - pkg/boggle_solver-0.0.1.gem
61
38
  - pkg/boggle_solver-0.0.2.gem
39
+ - pkg/boggle_solver-0.0.3.gem
62
40
  - test/test_adjacency_matrix.rb
63
41
  - test/test_board.rb
42
+ - test/test_options.rb
64
43
  homepage: http://rampantmonkey.com
65
44
  licenses: []
66
45
  post_install_message:
@@ -89,4 +68,5 @@ summary: Find all words in a letter matrix
89
68
  test_files:
90
69
  - test/test_adjacency_matrix.rb
91
70
  - test/test_board.rb
71
+ - test/test_options.rb
92
72
  ...
Binary file