letter_press_is_not_as_good_as_boggle 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/example.rb ADDED
@@ -0,0 +1,17 @@
1
+ $LOAD_PATH.unshift File.expand_path '../lib', __FILE__
2
+ require 'letter_press_is_not_as_good_as_boggle'
3
+
4
+ LetterPressIsNotAsGoodAsBoggle do
5
+ board %w[h z e o f
6
+ h p h y i
7
+ h w e b r
8
+ x z u g o
9
+ b i o g f]
10
+
11
+ guesses %w[groupie
12
+ hogger]
13
+
14
+ # get the list of all known words that can be made on the board
15
+ # unless hey have already been guessed
16
+ words.each { |word| puts word }
17
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
3
+ require "letter_press_is_not_as_good_as_boggle/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "letter_press_is_not_as_good_as_boggle"
7
+ s.version = LetterPressIsNotAsGoodAsBoggle::VERSION
8
+ s.authors = ["Josh Cheek"]
9
+ s.email = ["josh.cheek@gmail.com"]
10
+ s.homepage = "https://github.com/JoshCheek/letter_press_is_not_as_good_as_boggle"
11
+ s.summary = %q{Find all known words for a letterpress board}
12
+ s.description = %q{Find all known words for a letterpress board (iPhone 5 game that is like boggle, but worse)}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "rspec", "~> 2.4"
20
+ s.add_development_dependency "simplecov", "~> 0.7.1"
21
+ end
@@ -0,0 +1,58 @@
1
+ require 'letter_press_is_not_as_good_as_boggle/board_traverser'
2
+ require 'letter_press_is_not_as_good_as_boggle/word_list'
3
+ require 'letter_press_is_not_as_good_as_boggle/word_list/searcher'
4
+ require 'set'
5
+
6
+ def LetterPressIsNotAsGoodAsBoggle(&block)
7
+ LetterPressIsNotAsGoodAsBoggle.new(&block).words
8
+ end
9
+
10
+ class LetterPressIsNotAsGoodAsBoggle
11
+ def self.all_words
12
+ words = File.readlines File.expand_path '../../data/word_list', __FILE__
13
+ words.map &:chomp!
14
+ words
15
+ end
16
+
17
+ def initialize(all_words=LetterPressIsNotAsGoodAsBoggle.all_words, &definition)
18
+ self.definition = definition
19
+ self.searcher = searcher_for all_words
20
+ instance_eval &definition
21
+ end
22
+
23
+ def searcher_for(all_words)
24
+ WordList::Searcher.new WordList.new all_words
25
+ end
26
+
27
+ def board(chars)
28
+ self.chars = chars
29
+ end
30
+
31
+ def guesses(guesses=nil)
32
+ guesses && (@guesses = guesses)
33
+ @guesses
34
+ end
35
+
36
+ def words
37
+ @words ||= begin
38
+ words = Set.new
39
+ board_traverser.each_with_recur do |word, char, recurser|
40
+ next unless searcher.down? char
41
+ searcher.down char
42
+ words << word if searcher.word? && !@guesses.include?(word)
43
+ recurser.call
44
+ searcher.up
45
+ end
46
+ words.sort_by { |word| word.length }
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ attr_accessor :chars, :searcher, :definition
53
+
54
+ def board_traverser
55
+ @board_traverser ||= BoardTraverser.new chars
56
+ end
57
+ end
58
+
@@ -0,0 +1,27 @@
1
+ class LetterPressIsNotAsGoodAsBoggle
2
+ class BoardTraverser
3
+
4
+ attr_accessor :characters
5
+
6
+ def initialize(characters)
7
+ self.characters = characters
8
+ end
9
+
10
+ def each_with_recur(&block)
11
+ recursive_each_with_recur "", [], block
12
+ end
13
+
14
+ def recursive_each_with_recur(current, path, block)
15
+ characters.each do |char|
16
+ next if path.any? { |path_char| path_char.equal? char }
17
+ new_current = current + char
18
+ path.push char
19
+ recurser = lambda { recursive_each_with_recur new_current, path, block }
20
+ block.call new_current, char, recurser
21
+ path.pop
22
+ end
23
+ end
24
+
25
+ end
26
+ end
27
+
@@ -0,0 +1,3 @@
1
+ class LetterPressIsNotAsGoodAsBoggle
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,21 @@
1
+ require 'letter_press_is_not_as_good_as_boggle/word_list/node'
2
+
3
+ class LetterPressIsNotAsGoodAsBoggle
4
+ class WordList
5
+ attr_reader :root
6
+
7
+ def initialize(all_words=[])
8
+ @root = Node.new ''
9
+ all_words.each { |word| self << word }
10
+ end
11
+
12
+ def <<(word)
13
+ root.add word.chars.to_a
14
+ self
15
+ end
16
+
17
+ def words
18
+ root.words
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,59 @@
1
+ class LetterPressIsNotAsGoodAsBoggle
2
+ class WordList
3
+ class Node
4
+ attr_reader :parent
5
+
6
+ def initialize(char, parent=nil)
7
+ @char, @parent, @children = char, parent, Hash.new
8
+ end
9
+
10
+ def add(chars)
11
+ if chars.empty?
12
+ is_word!
13
+ return
14
+ end
15
+ char = chars.shift
16
+ add_child char, chars
17
+ end
18
+
19
+ def child?(char)
20
+ @children.has_key? char
21
+ end
22
+
23
+ def child(char)
24
+ @children.fetch char
25
+ end
26
+
27
+ def word?
28
+ @is_word
29
+ end
30
+
31
+ def to_s
32
+ @parent.to_s << @char
33
+ end
34
+
35
+ def words
36
+ words = []
37
+ words << to_s if word?
38
+ @children.each { |char, child| words.concat child.words }
39
+ words
40
+ end
41
+
42
+ def inspect
43
+ "#<Dictionary::Node #{to_s}>"
44
+ end
45
+
46
+ private
47
+
48
+ def add_child(char, chars)
49
+ @children[char] ||= Node.new char, self
50
+ @children[char].add chars
51
+ end
52
+
53
+ def is_word!
54
+ @is_word = true
55
+ end
56
+ end
57
+ end
58
+ end
59
+
@@ -0,0 +1,37 @@
1
+ class LetterPressIsNotAsGoodAsBoggle
2
+ class WordList
3
+ class Searcher
4
+ NoPath = Class.new StandardError
5
+
6
+ attr_reader :word_list
7
+
8
+ def initialize(word_list)
9
+ @node = word_list.root
10
+ end
11
+
12
+ def down?(char)
13
+ @node.child? char
14
+ end
15
+
16
+ def down(char)
17
+ raise NoPath, "No path #{@node.to_s << char}." unless down? char
18
+ @node = @node.child char
19
+ end
20
+
21
+ def current
22
+ @node.to_s
23
+ end
24
+
25
+ def up
26
+ new_node = @node.parent
27
+ raise NoPath, "Can't go up from root." unless new_node
28
+ @node = new_node
29
+ end
30
+
31
+ # rename to on_word?
32
+ def word?
33
+ @node.word?
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'some acceptance specs' do
4
+ specify 'words are returned sorted by length asc and do not include duplicates or previous guesses' do
5
+ unguessed = %w[ab abc dac]
6
+ guessed = %w[adc dab]
7
+ nonmatching = %w[x nek]
8
+ all_words = unguessed + guessed + nonmatching
9
+
10
+ letter_press = LetterPressIsNotAsGoodAsBoggle.new all_words do
11
+ board %w[a b c d a]
12
+ guesses %w[adc dab]
13
+ end
14
+
15
+ letter_press.words.should == %w[ab abc dac]
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require 'letter_press_is_not_as_good_as_boggle'
2
+
3
+ describe 'traversing the board' do
4
+ it 'traverses each possibility, yielding the current word, current char and a recur object' do
5
+ words = []
6
+ LetterPressIsNotAsGoodAsBoggle::BoardTraverser.new(%w[a b c]).each_with_recur do |word, char, recurser|
7
+ word.should end_with char
8
+ words << word
9
+ recurser.call
10
+ end
11
+ words.should =~ %w[a b c ab ac abc acb ba bc bac bca ca cb cab cba]
12
+ end
13
+
14
+ it "recurses when called, and doesn't when not called" do
15
+ words = []
16
+ LetterPressIsNotAsGoodAsBoggle::BoardTraverser.new(%w[a b c]).each_with_recur do |word, char, recurser|
17
+ words << word
18
+ recurser.call if word.end_with?("a")
19
+ end
20
+ words.should =~ %w[a b c ab ac]
21
+ end
22
+ end
@@ -0,0 +1,74 @@
1
+ require 'letter_press_is_not_as_good_as_boggle/word_list/searcher'
2
+
3
+ class LetterPressIsNotAsGoodAsBoggle
4
+ describe WordList::Searcher do
5
+ let(:word_list) { WordList.new }
6
+ let(:searcher) { WordList::Searcher.new word_list }
7
+
8
+ before do
9
+ word_list << 'they'
10
+ word_list << 'then'
11
+ end
12
+
13
+ it 'can go down a char' do
14
+ searcher.down('t')
15
+ searcher.down('h')
16
+ searcher.current.should == 'th'
17
+ end
18
+
19
+ it 'can go back up a char' do
20
+ searcher.down('t')
21
+ searcher.down('h')
22
+ searcher.up
23
+ searcher.current.should == 't'
24
+ end
25
+
26
+ it "raises an error if told to go down to a char doesn't make a word" do
27
+ searcher.down 't'
28
+ expect { searcher.down('a') }.to raise_error WordList::Searcher::NoPath, /ta/
29
+ end
30
+
31
+ it 'raises an error if told to go up from the root' do
32
+ expect { searcher.up }.to raise_error WordList::Searcher::NoPath, /root/
33
+ end
34
+
35
+ it 'knows when it is on a word in the word list' do
36
+ searcher.should_not be_word
37
+ searcher.down('t')
38
+ searcher.should_not be_word
39
+ searcher.down('h')
40
+ searcher.should_not be_word
41
+ searcher.down('e')
42
+ searcher.should_not be_word
43
+ searcher.down('n')
44
+ searcher.should be_word
45
+ end
46
+
47
+ it 'knows what paths it can go down' do
48
+ searcher.should be_down 't'
49
+ searcher.should_not be_down 'a'
50
+ end
51
+
52
+ example 'acceptance' do
53
+ searcher.should_not be_word
54
+ searcher.down 't'
55
+ searcher.should_not be_word
56
+ searcher.down 'h'
57
+ searcher.should_not be_word
58
+ searcher.down 'e'
59
+ searcher.should_not be_word
60
+ searcher.down 'n'
61
+ searcher.should be_word
62
+ searcher.current.should == 'then'
63
+ searcher.up
64
+ searcher.down 'y'
65
+ searcher.should be_word
66
+ searcher.current.should == 'they'
67
+ searcher.up
68
+ searcher.up
69
+ searcher.up
70
+ searcher.up
71
+ expect { searcher.up }.to raise_error WordList::Searcher::NoPath
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+ require 'letter_press_is_not_as_good_as_boggle'
@@ -0,0 +1,12 @@
1
+ require 'letter_press_is_not_as_good_as_boggle/word_list'
2
+
3
+ describe LetterPressIsNotAsGoodAsBoggle::WordList do
4
+ it 'knows all the words that were added to it' do
5
+ word_list = LetterPressIsNotAsGoodAsBoggle::WordList.new
6
+ word_list.words.should == []
7
+ word_list << "abc"
8
+ word_list << "abcd"
9
+ word_list << "ecx"
10
+ word_list.words.should == ["abc", "abcd", "ecx"]
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: letter_press_is_not_as_good_as_boggle
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Cheek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70224887901560 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.4'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70224887901560
25
+ - !ruby/object:Gem::Dependency
26
+ name: simplecov
27
+ requirement: &70224887901060 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.7.1
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70224887901060
36
+ description: Find all known words for a letterpress board (iPhone 5 game that is like
37
+ boggle, but worse)
38
+ email:
39
+ - josh.cheek@gmail.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Readme.md
47
+ - data/word_list
48
+ - example.rb
49
+ - letter_press_is_not_as_good_as_boggle.gemspec
50
+ - lib/letter_press_is_not_as_good_as_boggle.rb
51
+ - lib/letter_press_is_not_as_good_as_boggle/board_traverser.rb
52
+ - lib/letter_press_is_not_as_good_as_boggle/version.rb
53
+ - lib/letter_press_is_not_as_good_as_boggle/word_list.rb
54
+ - lib/letter_press_is_not_as_good_as_boggle/word_list/node.rb
55
+ - lib/letter_press_is_not_as_good_as_boggle/word_list/searcher.rb
56
+ - spec/acceptance_spec.rb
57
+ - spec/board_traverer_spec.rb
58
+ - spec/searcher_spec.rb
59
+ - spec/spec_helper.rb
60
+ - spec/word_list_spec.rb
61
+ homepage: https://github.com/JoshCheek/letter_press_is_not_as_good_as_boggle
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.17
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Find all known words for a letterpress board
85
+ test_files:
86
+ - spec/acceptance_spec.rb
87
+ - spec/board_traverer_spec.rb
88
+ - spec/searcher_spec.rb
89
+ - spec/spec_helper.rb
90
+ - spec/word_list_spec.rb