File Coverage

blib/lib/Games/ABC_Path/Generator/RiddleObj.pm
Criterion Covered Total %
statement 64 68 94.1
branch 20 22 90.9
condition n/a
subroutine 15 16 93.7
pod 4 4 100.0
total 103 110 93.6


line stmt bran cond sub pod time code
1             package Games::ABC_Path::Generator::RiddleObj;
2             $Games::ABC_Path::Generator::RiddleObj::VERSION = '0.6.1';
3 9     9   313154 use 5.006;
  9         29  
4              
5 9     9   42 use strict;
  9         34  
  9         194  
6 9     9   27 use warnings;
  9         13  
  9         396  
7              
8 9     9   39 use Carp;
  9         16  
  9         503  
9              
10 9     9   436 use integer;
  9         25  
  9         44  
11              
12 9     9   309 use parent 'Games::ABC_Path::Solver::Base';
  9         17  
  9         77  
13              
14 9     9   1972 use Games::ABC_Path::Solver::Constants;
  9         15  
  9         8123  
15              
16              
17             sub _solution
18             {
19 4     4   15 my $self = shift;
20              
21 4 100       18 if (@_)
22             {
23 3         25 $self->{_solution} = shift;
24             }
25              
26 4         11 return $self->{_solution};
27             }
28              
29             sub _clues
30             {
31 13     13   27 my $self = shift;
32              
33 13 100       34 if (@_)
34             {
35 3         8 $self->{_clues} = shift;
36             }
37              
38 13         36 return $self->{_clues};
39             }
40              
41             sub _A_pos
42             {
43 15     15   59 my $self = shift;
44              
45 15 100       67 if (@_)
46             {
47 3         11 $self->{_A_pos} = shift;
48             }
49              
50 15         53 return $self->{_A_pos};
51             }
52              
53             sub _init
54             {
55 3     3   134 my $self = shift;
56 3         10 my $args = shift;
57              
58 3         26 $self->_solution( $args->{solution} );
59 3         24 $self->_clues( $args->{clues} );
60 3         12 $self->_A_pos( $args->{A_pos} );
61              
62 3         8 return;
63             }
64              
65              
66             sub get_letters_of_clue
67             {
68 4     4 1 15 my ( $self, $args ) = @_;
69              
70             my $get_index = sub {
71 2     2   5 my $i = $args->{index};
72              
73 2 50       107 if ( $i !~ m{\A[01234]\z} )
74             {
75 0         0 Carp::confess('index must be in the range 0-4');
76             }
77              
78 2         8 return $i;
79 4         22 };
80              
81 4         9 my $clue_idx;
82 4         9 my $type = $args->{type};
83              
84 4 100       26 if ( $type eq 'col' )
    100          
    100          
    50          
85             {
86 1         5 $clue_idx = 2 + $LEN + $get_index->();
87             }
88             elsif ( $type eq 'row' )
89             {
90 1         4 $clue_idx = 2 + $get_index->();
91             }
92             elsif ( $type eq 'diag' )
93             {
94 1         2 $clue_idx = 0;
95             }
96             elsif ( $type eq 'antidiag' )
97             {
98 1         2 $clue_idx = 1;
99             }
100             else
101             {
102 0         0 Carp::confess("Unknown type $type.");
103             }
104              
105 4         9 return [ map { $letters[ $_ - 1 ] } @{ $self->_clues->[$clue_idx] } ];
  8         75  
  4         15  
106             }
107              
108              
109             sub get_riddle_v1_string
110             {
111 6     6 1 748 my ($self) = @_;
112              
113 6         25 my $s = ( ( ' ' x 7 ) . "\n" ) x 7;
114              
115 6         26 substr( $s, ( $self->_A_pos->y + 1 ) * 8 + $self->_A_pos->x + 1, 1 ) = 'A';
116              
117 6         55 my $clues = $self->_clues();
118 6         27 foreach my $clue_idx ( 0 .. $NUM_CLUES - 1 )
119             {
120 72 100       270 my @pos =
    100          
    100          
121             ( $clue_idx == 0 ) ? ( [ 0, 0 ], [ 6, 6 ] )
122             : ( $clue_idx == 1 ) ? ( [ 0, 6 ], [ 6, 0 ] )
123             : ( $clue_idx < ( 2 + 5 ) )
124             ? ( [ 1 + $clue_idx - (2), 0 ], [ 1 + $clue_idx - (2), 6 ] )
125             : (
126             [ 0, 1 + $clue_idx - ( 2 + 5 ) ],
127             [ 6, 1 + $clue_idx - ( 2 + 5 ) ]
128             );
129              
130 72         133 foreach my $i ( 0 .. 1 )
131             {
132 144         389 substr( $s, $pos[$i][0] * 8 + $pos[$i][1], 1 ) =
133             $letters[ $clues->[$clue_idx]->[$i] - 1 ];
134             }
135             }
136              
137 6         27 return $s;
138             }
139              
140              
141             sub get_final_layout
142             {
143 1     1 1 5 my ($self) = @_;
144              
145 1         5 return $self->_solution;
146             }
147              
148              
149             sub get_final_layout_as_string
150             {
151 0     0 1   my ( $self, $args ) = @_;
152              
153 0           return $self->_solution->as_string($args);
154             }
155              
156              
157             1; # End of Games::ABC_Path::Generator
158              
159             __END__