scrabblestuff 0.1.0
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.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.document +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +81 -0
- data/LICENSE.md +21 -0
- data/README.md +18 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/assets/wwf.txt +172820 -0
- data/lib/scrabblestuff.rb +3 -0
- data/lib/scrabblestuff/solver.rb +96 -0
- data/test/helper.rb +34 -0
- data/test/test_scrabblestuff.rb +7 -0
- metadata +128 -0
@@ -0,0 +1,96 @@
|
|
1
|
+
module Scrabble
|
2
|
+
module Solver
|
3
|
+
# The name of the file to use as a word dictionary. This file can be any
|
4
|
+
# file that contains a list of words, one word per line.
|
5
|
+
# @word_file_name = File.dirname(__FILE__) + "/../../assets/words.txt"
|
6
|
+
@word_file_name = File.realpath("assets/wwf.txt")
|
7
|
+
|
8
|
+
# Reads in the words.txt file and returns an array containing all of the words
|
9
|
+
# in that file.
|
10
|
+
#
|
11
|
+
# Used by other methods
|
12
|
+
|
13
|
+
def self.word_list
|
14
|
+
@word_list ||= File.open(@word_file_name) do |file|
|
15
|
+
file.readlines.map do |line|
|
16
|
+
line.chomp
|
17
|
+
end.delete_if do |line|
|
18
|
+
line.length < 2
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
@word_list.clone
|
23
|
+
end
|
24
|
+
|
25
|
+
# Gets an array of words that would fit the current board of Scrabble
|
26
|
+
# tiles.
|
27
|
+
#
|
28
|
+
# Example:
|
29
|
+
#
|
30
|
+
# Scrabble::Solver.words_for "there"
|
31
|
+
# # => An array of words that the tiles t, h, e, r, e could make.
|
32
|
+
#
|
33
|
+
# Scrabble::Solver.words_for "there?"
|
34
|
+
# # => An array of words that the tiles t, h, e, r, e plus a blank tile
|
35
|
+
# # could make.
|
36
|
+
|
37
|
+
def self.words_for(letters, options = {})
|
38
|
+
# splits the letters into lowercase array of individual letters
|
39
|
+
letters = letters.downcase.split(//)
|
40
|
+
|
41
|
+
# cycles through the entire word list, returns a list of new words as long as they match the algorithm
|
42
|
+
words = word_list.keep_if do |word|
|
43
|
+
# Split the current dictionary word into an array of its letters.
|
44
|
+
word = word.split(//)
|
45
|
+
|
46
|
+
# cycles through each of the letters in our letters array
|
47
|
+
letters.each do |letter|
|
48
|
+
# if the letter is found...
|
49
|
+
unless word.index(letter).nil?
|
50
|
+
# delete the letter from the word, move to the next letter
|
51
|
+
word.delete_at word.index(letter)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Only return the word if there are no remaining letters
|
56
|
+
word.length == 0
|
57
|
+
end
|
58
|
+
|
59
|
+
# Filter only words that start with a specific sequence.
|
60
|
+
if options[:starts_with]
|
61
|
+
words.keep_if { |word| word.start_with? options[:starts_with] }
|
62
|
+
end
|
63
|
+
|
64
|
+
# Filter words that only end in a certain sequence.
|
65
|
+
if options[:ends_with]
|
66
|
+
words.keep_if { |word| word.end_with? options[:ends_with] }
|
67
|
+
end
|
68
|
+
|
69
|
+
# Fitler only words shorter than a given amount.
|
70
|
+
if options[:shorter_than]
|
71
|
+
words.keep_if { |word| word.length < options[:shorter_than].to_i }
|
72
|
+
end
|
73
|
+
|
74
|
+
# Filter words only longer than a given amount.
|
75
|
+
if options[:longer_than]
|
76
|
+
words.keep_if { |word| word.length > options[:longer_than].to_i }
|
77
|
+
end
|
78
|
+
|
79
|
+
# Filter words that contain a specific sequence at a given 1-based index.
|
80
|
+
if options[:contains]
|
81
|
+
# Generate a regex to match against the string. This regex will replace
|
82
|
+
# question marks with dots, so that they match any character. This
|
83
|
+
# gives the user a lot of power with the --contains --at combo.
|
84
|
+
regex = ''
|
85
|
+
regex += ('^' + ('.' * (options[:at].to_i - 1))) if options[:at]
|
86
|
+
regex += options[:contains].gsub('?', '.')
|
87
|
+
regex = Regexp.new regex
|
88
|
+
words.keep_if do |word|
|
89
|
+
word =~ regex
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
return words
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
|
3
|
+
module SimpleCov::Configuration
|
4
|
+
def clean_filters
|
5
|
+
@filters = []
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
SimpleCov.configure do
|
10
|
+
clean_filters
|
11
|
+
load_adapter 'test_frameworks'
|
12
|
+
end
|
13
|
+
|
14
|
+
ENV["COVERAGE"] && SimpleCov.start do
|
15
|
+
add_filter "/.rvm/"
|
16
|
+
end
|
17
|
+
require 'rubygems'
|
18
|
+
require 'bundler'
|
19
|
+
begin
|
20
|
+
Bundler.setup(:default, :development)
|
21
|
+
rescue Bundler::BundlerError => e
|
22
|
+
$stderr.puts e.message
|
23
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
24
|
+
exit e.status_code
|
25
|
+
end
|
26
|
+
require 'test/unit'
|
27
|
+
require 'shoulda'
|
28
|
+
|
29
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
30
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
31
|
+
require 'scrabblestuff'
|
32
|
+
|
33
|
+
class Test::Unit::TestCase
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scrabblestuff
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cody Barr
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: shoulda
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rdoc
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.12'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.12'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: jeweler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.0.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.0.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: scrabblestuff is a simple scrabble solver gem with a default WWF dictionary.
|
84
|
+
email: cody.barr@gmail.com
|
85
|
+
executables: []
|
86
|
+
extensions: []
|
87
|
+
extra_rdoc_files:
|
88
|
+
- LICENSE.md
|
89
|
+
- README.md
|
90
|
+
files:
|
91
|
+
- .DS_Store
|
92
|
+
- .document
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE.md
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- VERSION
|
99
|
+
- assets/wwf.txt
|
100
|
+
- lib/scrabblestuff.rb
|
101
|
+
- lib/scrabblestuff/solver.rb
|
102
|
+
- test/helper.rb
|
103
|
+
- test/test_scrabblestuff.rb
|
104
|
+
homepage: http://github.com/codybarr/scrabblestuff-gem
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.2.2
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Simple ruby scrabble solver
|
128
|
+
test_files: []
|