| 1 | package Games::Trackword; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | $VERSION = '1.08'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | #---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | Games::Trackword - Find words on a Trackword grid. | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | use Games::Trackword; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | my $board = Games::Trackword->new('TRA WKC ORD'); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | foreach my $word (@wordlist) { | ||||||||||||||||||||||||||||
| 22 | print "OK $word\n" if $board->has_word($word); | ||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | This module lets you set up a Trackword grid and query whether or not it is | ||||||||||||||||||||||||||||
| 28 | possible to find words on that grid. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Note that although Trackword is similar to Boggle, you are not restricted by | ||||||||||||||||||||||||||||
| 31 | the size of the grid and you cannot move diagonally between each letter, you | ||||||||||||||||||||||||||||
| 32 | may only move up/down or left/right. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =cut | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 6 | my @directions = ([-1,0],[0,-1],[1,0],[0,1]); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub new { | ||||||||||||||||||||||||||||
| 39 | 5 | ▶ | my ($class, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 40 | bless { | ||||||||||||||||||||||||||||
| 41 | _board => _ring_fence($string), | ||||||||||||||||||||||||||||
| 42 | 5 | _has => { map { $_ => 1 } grep /\S/, split //, uc $string }, | |||||||||||||||||||||||||||
| 43 | }, $class; | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | sub qu { | ||||||||||||||||||||||||||||
| 47 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
qu: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 6 | my $this = shift; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 6 | $self->{_qu} = 1; # Boggle style | |||||||||||||||||||||||||||
| 51 | 6 | ▶ | $self->{_qu} = 0 if(defined $this && !$this); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $this and not $this
MC/DC100%2/2
defined $this and not $this
defined $this
not $this
Truth table100%3/3
A and not(B)
A= defined $thisB= not $this
| |||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub has_word { | ||||||||||||||||||||||||||||
| 55 | 39 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
has_word: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 39 | my $word = uc shift; | |||||||||||||||||||||||||||
| 57 | 39 | my $board = $self->{_board}; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 39 | ▶ | if($self->{_qu}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 12 | ▶ | return if $word =~ /Q(?!U)/; # Can't have lone Q in boggle. | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 4 | $word =~ s/QU/Q/; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 31 | ▶ | return unless $self->_have_letters($word); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 24 | my $last = chop $word; | |||||||||||||||||||||||||||
| 67 | 24 | my $rows = scalar(@$board)-1; | |||||||||||||||||||||||||||
| 68 | 24 | foreach my $posy (0..$rows) { | |||||||||||||||||||||||||||
| 69 | 88 | my $cols = scalar(@{ $board->[$posy] })-1; | |||||||||||||||||||||||||||
| 70 | 88 | foreach my $posx (0..$cols) { | |||||||||||||||||||||||||||
| 71 | 484 | ▶ | if($board->[$posy][$posx] eq $last) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 25 | ▶ | return 1 if(_can_play($board, $word.$last, $posy, $posx)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | return 0; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Quick sanity check to stop us looking for words with letters we don't | ||||||||||||||||||||||||||||
| 81 | # have. We don't check to ensure that we have ENOUGH copies of each | ||||||||||||||||||||||||||||
| 82 | # letter in the word, as that is considerably slower. | ||||||||||||||||||||||||||||
| 83 | sub _have_letters { | ||||||||||||||||||||||||||||
| 84 | 31 | ▶ | my ($self, $word) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_have_letters: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 85 | 31 | ▶ | while (my $let = chop $word) { return unless $self->{_has}->{$let}; } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 86 | 24 | return 1; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | sub _can_play { | ||||||||||||||||||||||||||||
| 90 | 258 | ▶ | my ($board, $word, $posy, $posx) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_can_play: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 91 | 258 | ▶ | if (length $word > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 207 | my $last = chop $word; | |||||||||||||||||||||||||||
| 93 | 207 | ▶ | if($board->[$posy][$posx] eq $last) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 97 | local $board->[$posy][$posx] = "-"; | |||||||||||||||||||||||||||
| 95 | 97 | foreach my $dir (@directions) { | |||||||||||||||||||||||||||
| 96 | 233 | ▶ | return 1 if(_can_play($board, $word, $posy+$dir->[0], $posx+$dir->[1])); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 122 | return 0; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 51 | ▶ | return ($board->[$posy][$posx] eq $word) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | sub _ring_fence { | ||||||||||||||||||||||||||||
| 105 | 5 | ▶ | my @block = split /\s/, uc $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
_ring_fence: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 5 | my $width = length($block[0])+2; | |||||||||||||||||||||||||||
| 107 | push my @board, [('-') x $width], | ||||||||||||||||||||||||||||
| 108 | 5 | (map {['-',split(//),'-']} @block), | |||||||||||||||||||||||||||
| 109 | [('-') x $width]; | ||||||||||||||||||||||||||||
| 110 | 5 | return \@board; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | 1; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||