WordBrain.pm

filter: Statementstmt 100% 146 / 146 Branchbran 96.6% 29 / 30 Conditioncond 90.0% 9 / 10 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 21 / 21 Podpod 100% 6 / 6 totaltotal 98.6% 216 / 219 CC 29 SCAR 15.3
CC
29
Cov
99%
solve
10.0
find_near_letters
7.0
new
4.0
CRAP 4.6
« WordBrain/Prefix.pm ↑ Summary WordBrain/Speller.pm »
1 package Game::WordBrain;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Game::WordBrain::Letter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Game::WordBrain::Word;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Game::WordBrain::Solution;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Game::WordBrain::WordToFind;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Game::WordBrain::Prefix;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Game::WordBrain::Speller;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 7 ▶use Storable qw( dclone );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use List::Util qw( reduce first );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use List::MoreUtils qw( first_index );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 our $VERSION = '0.2.2'; # VERSION
18 # ABSTRACT: Solver for Mag Interactive's WordBrain Mobile Game
19  
20 =head1 NAME
21  
22 Game::WordBrain - Solver for the Mobile App "WordBrain"
23  
24 =head1 SYNOPSIS
25  
26 # Create a new Game::WordBrain
27 my @letters;
28 push @letters, Game::WordBrain::Letter->new({ letter => 't', row => 0, col => 0 });
29 push @letters, Game::WordBrain::Letter->new({ letter => 'a', row => 0, col => 1 });
30 push @letters, Game::WordBrain::Letter->new({ letter => 'l', row => 1, col => 0 });
31 push @letters, Game::WordBrain::Letter->new({ letter => 'k', row => 1, col => 1 });
32  
33 my $words_to_find = [ Game::WordBrain::WordToFind->... ];
34 my $speller = Game::WordBrain::Speller->...;
35 my $prefix = Game::WordBrain::Prefix->...;
36  
37 my $game = Game::WordBrain->new({
38 letters => \@letters,
39 words_to_find => $words_to_find,
40 speller => $speller, # optional
41 prefix => $prefix, # optional
42 });
43  
44 # Solve a Game
45 $game->solve();
46 print "Number of Solutions Found: " . ( scalar @{ $game->{solutions} } ) . "\n";
47  
48 # Construct a game without a word
49 my $already_used_word = Game::WordBrain::Word->...;
50 my $sub_game = $game->construct_game_without_word( $already_used_word );
51  
52 # Get letter at position
53 my $letter = $game->get_letter_at_position({
54 row => 2,
55 col => 3,
56 });
57  
58  
59 # Find Near letters
60 my $near_letters = $game->find_near_letters({
61 used => [ Game::WordBrain::Letter->... ],
62 row_number => 1,
63 col_number => 1,
64 });
65  
66  
67 # Find Near Words
68 my $near_words = $game->find_near_words({
69 letter => WordBrain::Letter->...,
70 used => [ ], # Optional
71 max_word_length => 5, # Optional
72 });
73  
74 =head1 DESCRIPTION
75  
76 Game::WordBrain is a solver created to generation potential solutions for L<Mag Interactive|http://maginteractive.com>'s WordBrain. WordBrain is available for:
77  
78 =over 4
79  
80 =item L<iOS|https://itunes.apple.com/us/app/wordbrain/id708600202?mt=8>
81  
82 =item L<Android|https://play.google.com/store/apps/details?id=se.maginteractive.wordbrain&hl=en>
83  
84 =back
85  
86 This module is currently functional for I<simple> games ( 4x4 and less ) but it requires B<SIGNIFICANT> time to process larger ones. Feel free to propose improvements at the L<GitHub|https://github.com/drzigman/game-wordbrain> for this repo!
87  
88 If you are new to WordBrain or simply want a jumpstart on how this module works and it's limitations (and evolution) please see:
89  
90 =over 4
91  
92 =item L<YAPC::2016 Presentation on YouTube - "Solving WordBrain - A Depth First Search of a Problem Of Great Breadth"|https://www.youtube.com/watch?v=_1fnTyJg7uA>
93  
94 =item L<Presentation Slides|https://drive.google.com/file/d/0B7wWgMKY-dDxb1RwMWxRZ3lEWFE/view>
95  
96 =back
97  
98 =head1 ATTRIBUTES
99  
100 =head2 B<letters>
101  
102 ArrayRef of L<Game::WordBrain::Letter>s that comprise the game field
103  
104 =head2 B<words_to_find>
105  
106 ArrayRef of L<Game::WordBrain::WordToFind>s that indicate the number of words to find as well as the length of each word.
107  
108 =head2 speller
109  
110 An instance of L<Game::WordBrain::Speller> that is used to spell check potential words.
111  
112 If this is not provided it will be automagically built. You generally do not need to provide this but if you wish to use something other than the provided wordlist creating your own L<Game::WordBrain::Speller> and providing it in the call to new would be how to accomplish that.
113  
114 =head2 prefix
115  
116 An instance of L<Game::WordBrain::Prefix> used to speed up game play.
117  
118 If not provided, the max word_to_find will be detected and used to construct it. You generally do not need to provide this but if you wish to use something other than the provided wordlist creating your own L<Game::WordBrain::Prefix> and providing it in the call to new would be how to accomplish that.
119  
120 =head2 solutions
121  
122 Generated after a call to ->solve has been made. This is an ArrayRef of L<Game::WordBrain::Solution>s.
123  
124 =head1 METHODS
125  
126 =head2 new
127  
128 my $letters = [ Game::WordBrain::Letter->... ];
129 my $words_to_find = [ Game::WordBrain::WordToFind->... ];
130 my $speller = Game::WordBrain::Speller->...;
131 my $prefix = Game::WordBrain::Prefix->...;
132  
133 my $game = Game::WordBrain->new({
134 letters => $letters,
135 words_to_find => $words_to_find,
136 speller => $speller, # optional
137 prefix => $prefix, # optional
138 });
139  
140 Given an ArrayRef of L<Game::WordBrain::Letter>s, an ArrayRef of L<Game::WordBrain::WordToFind>, and optionally an instance of L<Game::WordBrain::Speller> and L<Game::WordBrain::Prefix> constructs and returns a new WordBrain game.
141  
142 B<NOTE> While it is also possible to pass solutions => [ Game::WordBrain::Solution->...], there is really no reason for a consumer to do so.
143  
144 =cut
145  
146 129 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
147 129 my $class = shift;
148 129 my $args = shift;
149  
150 129 ▶ if( !exists $args->{solutions} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"solutions"}) 129 0 129
151 129 $args->{solutions} = undef;
152 }
153  
154 129 ▶ if( !exists $args->{speller} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"speller"}) 7 122 129
155 7 $args->{speller} = Game::WordBrain::Speller->new();
156 }
157  
158 129 ▶ if( !exists $args->{prefix} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"prefix"}) 7 122 129
159 3 ▶ my $largest_word_to_find = reduce {
Subroutine __ANON__: called CC=2 SCAR=6.9
160 3 ▶ $a->{num_letters} > $b->{num_letters} ? $a : $b
Branch: 1 branch
BranchTrueFalseTotal
$Game::WordBrain::a->{'num_letters'} > $Game::WordBrain::b->{'num_letters'} ? : 2 1 3
161 7 } @{ $args->{words_to_find} };
162  
163 $args->{prefix} = Game::WordBrain::Prefix->new({
164 max_prefix_length => $largest_word_to_find->{num_letters}
165 7 });
166 }
167  
168 129 return bless $args, $class;
169 }
170  
171 =head2 solve
172  
173 my @letters;
174 push @letters, WordBrain::Letter->new({ letter => 't', row => 0, col => 0 });
175 push @letters, WordBrain::Letter->new({ letter => 'a', row => 0, col => 1 });
176 push @letters, WordBrain::Letter->new({ letter => 'l', row => 1, col => 0 });
177 push @letters, WordBrain::Letter->new({ letter => 'k', row => 1, col => 1 });
178  
179 my @words_to_find;
180 push @words_to_find, WordBrain::WordToFind->new({ num_letters => 4 });
181  
182 my $game = Game::WordBrain->new({
183 letters => \@letters,
184 words_to_find => \@words_to_find,
185 });
186  
187 $game->solve();
188  
189 print "Number of Solutions Found: " . ( scalar @{ $game->{solutions} } ) . "\n";
190  
191 The solve method is the real meat of L<Game::WordBrain>. When called on a fully formed game this method will enumerate potential solutions and set the $game->{solutions} attribute.
192  
193 B<NOTE> Depending on the size of the game grid, this method can take a very long time to run.
194  
195 =cut
196  
197 123 ▶sub solve {
Subroutine solve: called CC=10 SCAR=23.0
Pod documented
198 123 my $self = shift;
199  
200 123 my $max_word_length = 0;
201 123 for my $word_to_find (@{ $self->{words_to_find} }) {
202 124 ▶ if( $max_word_length < $word_to_find->{num_letters} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_word_length < $word_to_find->{"num_letters"}) 123 1 124
203 123 $max_word_length = $word_to_find->{num_letters};
204 }
205 }
206  
207 123 my @solutions;
208 123 for my $letter (@{ $self->{letters} }) {
209 594 my $possible_words = $self->find_near_words({
210 letter => $letter,
211 max_word_length => $max_word_length,
212 });
213  
214 594 my @actual_words;
215 594 for my $possible_word (@{ $possible_words }) {
216 6823 ▶ if( grep { $_->{num_letters} == length ( $possible_word->word ) } @{ $self->{words_to_find} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->{"num_letters"} == length $possible_word->word;} @{$$self{"words_to_find"};}) 1286 5537 6823
217 1286 ▶ if( $self->{speller}->is_valid_word( $possible_word ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"speller"}->is_valid_word($possible_word)) 195 1091 1286
218 195 push @actual_words, $possible_word;
219 }
220 }
221 }
222  
223  
224 594 for my $word ( @actual_words ) {
225 195 ▶ if( scalar @{ $self->{words_to_find} } > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{'words_to_find'};} > 1) { } 121 74 195
226 121 my $updated_game = $self->construct_game_without_word( $word );
227 121 my $updated_game_solutions = $updated_game->solve();
228  
229 121 for my $updated_game_solution (@{ $updated_game_solutions }) {
230 push @solutions, Game::WordBrain::Solution->new({
231 73 words => [ $word, @{ $updated_game_solution->{words} } ],
232 });
233 }
234 }
235 else {
236 74 push @solutions, Game::WordBrain::Solution->new({
237 words => [ $word ],
238 });
239 }
240 }
241 }
242  
243 123 $self->{solutions} = \@solutions;
244 }
245  
246 =head2 construct_game_without_word
247  
248 my $word = Game::WordBrain::Word->...;
249 my $game = Game::WordBrain->...;
250  
251 my $sub_game = $game->construct_game_without_word( $word );
252  
253 In WordBrain, once a word is matched the letters for it are removed from the playing field, causing all other letters to shift down (think of it like gravity pulling the letters straight down). This method exists to simplify the process of generating a new instance of a L<Game::WordBrain> from an existing instance minus the found word.
254  
255 There really isn't a reason for a consumer to call this method directly, rather it is used by the solve method during solution enumeration.
256  
257 =cut
258  
259 122 ▶sub construct_game_without_word {
Subroutine construct_game_without_word: called CC=4 SCAR=13.9
Pod documented
260 122 my $self = shift;
261 122 my $found_word = shift;
262  
263 122 my $words_to_find = dclone $self->{words_to_find};
264 220 ▶ my $index_of_found_word = first_index {
Subroutine __ANON__: called CC=1 SCAR=0.0
265 220 $_->{num_letters} == scalar @{ $found_word->{letters} }
266 122 } @{ $self->{words_to_find} };
267  
268 122 splice @{ $words_to_find }, $index_of_found_word, 1;
269  
270 122 my @new_letters;
271 122 for my $letter (@{ $self->{letters} }) {
272 1098 ▶ if( grep { $_ == $letter } @{ $found_word->{letters} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ == $letter;} @{$$found_word{"letters"};}) 512 586 1098
273 512 next;
274 }
275  
276 my $num_letters_used_below = grep {
277 $_->{col} == $letter->{col}
278 && $_->{row} > $letter->{row}
279 586 ▶ } @{ $found_word->{letters} };
Condition100%3/3
$_->{'col'} == $letter->{'col'} && $_->{'row'} > $letter->{'row'}
!ll&&!rl&&r
1824276340
MC/DC100%2/2
$_->{'col'} == $letter->{'col'} && $_->{'row'} > $letter->{'row'}
$_->{'col'} == $letter->{'col'} $_->{'row'} > $letter->{'row'}
Truth table100%3/3
A && B
A= $_->{'col'} == $letter->{'col'}B= $_->{'row'} > $letter->{'row'}
ABresult
0X0
100
111
280  
281 push @new_letters, Game::WordBrain::Letter->new({
282 letter => $letter->{letter},
283 row => $letter->{row} + $num_letters_used_below,
284 col => $letter->{col},
285 586 });
286 }
287  
288 return Game::WordBrain->new({
289 letters => \@new_letters,
290 words_to_find => $words_to_find,
291 speller => $self->{speller},
292 prefix => $self->{prefix},
293 122 });
294 }
295  
296 =head2 get_letter_at_position
297  
298 my $game = Game::WordBrain->...
299 my $letter = $game->get_letter_at_position({
300 row => 2,
301 col => 3,
302 });
303  
304 Simple convenience method to retrieve the instance of L<Game::WordBrain::Letter> at a given row and col.
305  
306 =cut
307  
308 36514 ▶sub get_letter_at_position {
Subroutine get_letter_at_position: called CC=1 SCAR=0.0
Pod documented
309 36514 my $self = shift;
310 36514 my $args = shift;
311  
312 161761 ▶ return first {
Subroutine __ANON__: called CC=2 SCAR=6.9
313 $_->{row} == $args->{row}
314 && $_->{col} == $args->{col}
315 36514 ▶ } @{ $self->{letters} };
Branch: 1 branch
BranchTrueFalseTotal
$_->{"row"} == $args->{"row"} and $_->{"col"} == $args->{"col"} 45031 116730 161761
316 }
317  
318 =head2 find_near_letters
319  
320 my $game = Game::WordBrain->...
321 my $near_letters = $game->find_near_letters({
322 used => [ Game::WordBrain::Letter->... ],
323 row_number => 1,
324 col_number => 1,
325 });
326  
327 Given an ArrayRef of already used (for other words) L<Game::WordBrain::Letter>s, and the row and col number of a position, returns an ArrayRef of L<Game::WordBrain::Letter>s that are "near" the specified position. By "near" we mean a letter that is touching the specified position in one of the 8 cardinal directions and has not already been used.
328  
329 =cut
330  
331 4564 ▶sub find_near_letters {
Subroutine find_near_letters: called CC=7 SCAR=19.5
Pod documented
332 4564 my $self = shift;
333 4564 my $args = shift;
334  
335 4564 my @near_letters;
336 4564 for my $row_offset ( -1, 0, 1 ) {
337 13692 for my $col_offset ( -1, 0, 1 ) {
338 41076 ▶ if( $row_offset == 0 && $col_offset == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row_offset == 0 and $col_offset == 0) 4564 36512 41076
Condition100%3/3
$row_offset == 0 and $col_offset == 0
!ll&&!rl&&r
2738491284564
MC/DC100%2/2
$row_offset == 0 and $col_offset == 0
$row_offset == 0 $col_offset == 0
Truth table100%3/3
A and B
A= $row_offset == 0B= $col_offset == 0
ABresult
0X0
100
111
339 ### Skipping Center Letter
340 4564 next;
341 }
342  
343 36512 my $near_row_number = $args->{row_number} + $row_offset;
344 36512 my $near_col_number = $args->{col_number} + $col_offset;
345  
346 36512 my $letter = $self->get_letter_at_position({
347 row => $near_row_number,
348 col => $near_col_number,
349 });
350  
351 36512 ▶ if( !$letter ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($letter) 23521 12991 36512
352 23521 next;
353 }
354  
355 12991 ▶ if( grep { $_ == $letter } @{ $args->{used} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ == $letter;} @{$$args{"used"};}) 6098 6893 12991
356 ### Skipping Already Used Letter
357 6098 next;
358 }
359  
360 6893 push @near_letters, $letter;
361 }
362 }
363  
364 4564 return \@near_letters;
365 }
366  
367 =head2 find_near_words
368  
369 my $game = Game::WordBrain->...;
370 my $near_words = $game->find_near_words({
371 letter => WordBrain::Letter->...,
372 used => [ ], # Optional
373 max_word_length => 5, # Optional
374 });
375  
376 Similiar to find_near_letters, but returns an ArrayRef of L<Game::WordBrain::Word>s that can be constructed from the given L<Game::WordBrain::Letter>, ArrayRef of used L<Game::WordBrain::Letter>s and the max_word_length that should be searched for ( this should be the max L<Game::WordBrain::WordToFind>->{num_letters} ).
377  
378 =cut
379  
380 598 ▶sub find_near_words {
Subroutine find_near_words: called CC=3 SCAR=11.0
Pod documented
381 598 my $self = shift;
382 598 my $args = shift;
383  
384 598 ▶ $args->{used} //= [ ];
Condition50%1/2
$args->{"used"} //= []
l!l
0598
MC/DC0%0/1
$args->{"used"} //= []
$args->{"used"}
Truth table50%1/2
A
A= $args->{"used"}
Aresult
11
00
385 598 ▶ $args->{max_word_length} //= scalar @{ $self->{letters} };
Condition100%2/2
$args->{"max_word_length"} //= scalar @{$self->{"letters"};}
l!l
5944
MC/DC100%1/1
$args->{"max_word_length"} //= scalar @{$self->{"letters"};}
$args->{"max_word_length"}
Truth table100%2/2
A
A= $args->{"max_word_length"}
Aresult
11
00
386  
387 return $self->_find_near_words({
388 word_root => Game::WordBrain::Word->new({ letters => [ $args->{letter} ] }),
389 letter => $args->{letter},
390 used => $args->{used},
391 max_word_length => $args->{max_word_length},
392 598 });
393 }
394  
395 7467 ▶sub _find_near_words {
Subroutine _find_near_words: called CC=4 SCAR=13.9
396 7467 my $self = shift;
397 7467 my $args = shift;
398  
399 7467 push @{ $args->{used} }, $args->{letter};
400  
401 7467 ▶ if( scalar @{ $args->{word_root}->{letters} } >= $args->{max_word_length} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$args->{"word_root"}{"letters"};} >= $args->{"max_word_length"}) 942 6525 7467
402 942 return [ ];
403 }
404  
405 6525 ▶ if( !$self->{prefix}->is_start_of_word( $args->{word_root} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"prefix"}->is_start_of_word($args->{"word_root"})) 1969 4556 6525
406 1969 return [ ];
407 }
408  
409 4556 my @words;
410 my $near_letters = $self->find_near_letters({
411 used => $args->{used},
412 game => $args->{game},
413 row_number => $args->{letter}{row},
414 col_number => $args->{letter}{col},
415 4556 });
416  
417 4556 for my $near_letter (@{ $near_letters }) {
418 my $new_word_root = Game::WordBrain::Word->new({
419 6869 letters => [ @{ $args->{word_root}{letters} }, $near_letter ]
420 });
421  
422 6869 push @words, $new_word_root;
423  
424 6869 my $near_letter_used = dclone $args->{used};
425  
426 push @words, @{
427 6869 $self->_find_near_words({
428 word_root => $new_word_root,
429 letter => $near_letter,
430 used => $near_letter_used,
431 max_word_length => $args->{max_word_length},
432 6869 });
433 };
434 }
435  
436 4556 return \@words;
437 }
438  
439 =head1 AUTHORS
440  
441 Robert Stone, C<< <drzigman AT cpan DOT org > >>
442  
443 =head1 CONTRIBUTORS
444  
445 Special thanks to the following individuals who submitted bug reports, performance ideas, and/or pull requests.
446  
447 =over 4
448  
449 =item Todd Rinaldo
450  
451 =item Mohammad S Anwar C< mohammad.anwar@yahoo.com >
452  
453 =back
454  
455 =head1 ACKNOWLEDGMENTS
456  
457 Special thanks to L<BrainStorm Incubator|http://brainstormincubator.com> for funding the development of this module and providing test resources.
458  
459 Further thanks to L<Houston Perl Mongers|http://houston.pm.org> for providing input and ideas for improvement.
460  
461 =head1 COPYRIGHT & LICENSE
462  
463 Copyright 2016 Robert Stone
464  
465 This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU Lesser General Public License as published by the Free Software Foundation; or any compatible license.
466  
467 See http://dev.perl.org/licenses/ for more information.
468  
469 =cut
470  
471 7 1;
« WordBrain/Prefix.pm ↑ Summary WordBrain/Speller.pm »