blib/lib/Games/Boggle.pm

filter: Statementstmt 100% 36 / 36 Branchbran 100% 12 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 100% 57 / 57 CC 7 SCAR 9.3
CC
7
Cov
100%
has_word
4.0
_have_letters
3.0
_can_play
2.0
CRAP 2.5
↑ Summary
1 package Games::Boggle;
2  
3 =head1 NAME
4  
5 Games::Boggle - find words on a boggle board
6  
7 =head1 SYNOPSIS
8  
9 use Games::Boggle;
10  
11 my $board = Games::Boggle->new("TRTO XIHP TEEB MQYP");
12  
13 foreach my $word (@wordlist) {
14 print "OK $word\n" if $board->has_word($word);
15 }
16  
17 =head1 DESCRIPTION
18  
19 This module lets you set up a Boggle board, and then query it for whether
20 or not it is possible to find words on that board.
21  
22 =head1 METHODS
23  
24 =head2 new
25  
26 my $board = Games::Boggle->new("TRTO XIHP TEEB MEQP");
27  
28 You initialize the board with a series of 16 letters representing the
29 letters that are shown. Optional spaces may be inserted to make the
30 board string more readable.
31  
32 A 'Qu' should be entered solely as a 'Q'.
33  
34 =head2 has_word
35  
36 print "OK $word\n" if $board->has_word('tithe');
37 print "NOT OK $word\n" unless $board->has_word('queen');
38  
39 Given any word, we return whether or not that word can be found on the
40 board following the normal rules of Boggle.
41  
42 In scalar context this returns the number of possible ways of finding
43 this word. In list context it returns the starting squares from which this
44 word can be found (but only once per square, no matter how many times it
45 can be found there).
46  
47 Words containing the letter Q should be entered in full ('Queen', rather
48 than 'qeen'). Words containing a 'Q' not immediately followed by a 'U'
49 are never playable.
50  
51 =head1 AUTHOR
52  
53 Tony Bowden
54  
55 =head1 BUGS and QUERIES
56  
57 Please direct all correspondence regarding this module to:
58 bug-Games-Boggle@rt.cpan.org
59  
60 =head1 COPYRIGHT AND LICENSE
61  
62 Copyright (C) 2002-2005 Tony Bowden.
63  
64 This program is free software; you can redistribute it and/or modify it under
65 the terms of the GNU General Public License; either version 2 of the License,
66 or (at your option) any later version.
67  
68 This program is distributed in the hope that it will be useful, but WITHOUT
69 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
70 FOR A PARTICULAR PURPOSE.
71  
72 =head1 SEE ALSO
73  
74 Advanced Perl Programming, 2nd Edition, by Simon Cozens
75  
76 =cut
77  
78 2 $VERSION = '1.01';
79  
80 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
82  
83 5 ▶sub _unique {
Subroutine _unique: called CC=1 SCAR=0.0
84 5 my %list = map { $_ => 1 } @_;
85 5 return sort { $a <=> $b } keys %list;
86 }
87  
88 2 my $play = [
89 [1 .. 16],
90 [2,5,6],[1,3,5..7],[2,4,6..8],[3,7,8],
91 [1,2,6,9,10],[1..3,5,7,9..11],[2..4,6,8,10..12],[3,4,7,11,12],
92 [5,6,10,13,14],[5..7,9,11,13..15],[6..8,10,12,14..16],[7,8,11,15,16],
93 [9,10,14],[9..11,13,15],[10..12,14,16],[11,12,15]
94 ];
95  
96 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
97 1 my ($class, $string) = @_;
98 1 my @board = grep /\S/, split //, uc $string;
99 bless {
100 _board => ["-", @board],
101 1 _has => { map { $_ => 1 } @board },
102 }, $class;
103 }
104  
105 16 ▶sub has_word {
Subroutine has_word: called CC=4 SCAR=13.9
Pod documented
106 16 my $self = shift;
107 16 my $word = uc shift;
108 16 ▶ return if $word =~ /Q(?!U)/; # Can't have lone Q in boggle.
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /Q(?!U)/ 1 15 16
109 15 $word =~ s/QU/Q/;
110 15 ▶ return unless $self->_have_letters($word);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_have_letters($word) 2 13 15
111 13 my @starts = _can_play($self->{_board}, $word, 0);
112 13 ▶ return wantarray ? _unique @starts : scalar @starts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 8 13
113 }
114  
115 # Quick sanity check to stop us looking for words with letters we don't
116 # have. We don't check to ensure that we have ENOUGH copies of each
117 # letter in the word, as that is considerably slower.
118 15 ▶sub _have_letters {
Subroutine _have_letters: called CC=3 SCAR=11.0
119 15 my ($self, $word) = @_;
120 15 ▶ while (my $let = chop $word) { return unless $self->{_has}->{$let}; }
Branch: 2 branches
BranchTrueFalseTotal
if (my $let = chop $word) 57 13 70
unless $self->{"_has"}{$let} 2 55 57
121 13 return 1;
122 }
123  
124 204 ▶sub _can_play {
Subroutine _can_play: called CC=2 SCAR=6.9
125 204 my ($board, $word, $posn) = @_;
126 204 ▶ if (length $word > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word > 1) 83 121 204
127 83 my $last = chop $word;
128 return map {
129 83 local $board->[$_] = "-";
130 108 _can_play($board, $word, $_);
131 } _can_play($board, $last, $posn);
132 }
133 121 return grep $board->[$_] eq $word, @{ $play->[$posn] };
134 }
135  
136 2 return q/
137 AGGReGaTeD HeRBS ALLoW EXoTiC FLaVoR; OVeRZeaLouS PeoPLe ReaLiZe We USe
138 PReMiXeD CaViaR & DRiNK UP HuMBLeD GRoG IN MeGaDoSeS
139 /;
↑ Summary