| 1 | package Games::ABC_Path::Generator::RiddleObj; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.006; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | |||||||||||||||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | 6 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | |||||||||||||||||||||||
| 10 | 6 | ▶ | use integer; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 6 | ▶ | use base 'Games::ABC_Path::Solver::Base'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 6 | ▶ | use Games::ABC_Path::Solver::Constants; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | |||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||
| 17 | |||||||||||||||||||||||
| 18 | Games::ABC_Path::Generator::RiddleObj - represents a riddle. | ||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | =head1 VERSION | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | Version 0.1.0 | ||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||
| 25 | |||||||||||||||||||||||
| 26 | 6 | our $VERSION = '0.1.0'; | |||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | use Games::ABC_Path::Generator; | ||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | my $gen = Games::ABC_Path::Generator->new({seed => 1}); | ||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | # Returns a Games::ABC_Path::Generator::RiddleObj object. | ||||||||||||||||||||||
| 35 | my $riddle = $gen->calc_riddle(); | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | print $riddle->get_riddle_v1_string(); | ||||||||||||||||||||||
| 38 | print $riddle->get_riddle_string_with_header(); | ||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | =head2 my $riddle = Games::ABC_Path::Generator::RiddleObj->new({%args}); | ||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | Initialised a new riddle. Arguments are: | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | =over 4 | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | =item * solution | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | The solution layout. | ||||||||||||||||||||||
| 52 | |||||||||||||||||||||||
| 53 | =item * clues | ||||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | An array of the clues. | ||||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | =item * A_pos | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | The position of the A cell. | ||||||||||||||||||||||
| 60 | |||||||||||||||||||||||
| 61 | =back | ||||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||
| 64 | |||||||||||||||||||||||
| 65 | sub _solution | ||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||
| 67 | 6 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
_solution: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 6 | ▶ | if (@_) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||
| 71 | 3 | $self->{_solution} = shift; | |||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | 6 | return $self->{_solution}; | |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | sub _clues | ||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||
| 79 | 13 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
_clues: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | 13 | ▶ | if (@_) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 82 | { | ||||||||||||||||||||||
| 83 | 3 | $self->{_clues} = shift; | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | 13 | return $self->{_clues}; | |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | sub _A_pos | ||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||
| 91 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
_A_pos: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 15 | ▶ | if (@_) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||
| 95 | 3 | $self->{_A_pos} = shift; | |||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||
| 97 | |||||||||||||||||||||||
| 98 | 15 | return $self->{_A_pos}; | |||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | sub _init | ||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||
| 103 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 104 | 3 | my $args = shift; | |||||||||||||||||||||
| 105 | |||||||||||||||||||||||
| 106 | 3 | $self->_solution($args->{solution}); | |||||||||||||||||||||
| 107 | 3 | $self->_clues($args->{clues}); | |||||||||||||||||||||
| 108 | 3 | $self->_A_pos($args->{A_pos}); | |||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | 3 | return; | |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | =head2 my [$letter1, $letter2] = $riddle->get_letters_of_clue({ type => $type, index => $index, }) | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | Returns the two letters (as an array reference) associated with the | ||||||||||||||||||||||
| 116 | clue represented by the hash reference. Type can be: | ||||||||||||||||||||||
| 117 | |||||||||||||||||||||||
| 118 | =over 4 | ||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | =item * 'col' | ||||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | A column clue. C<'index'> points to the X coordinate. | ||||||||||||||||||||||
| 123 | |||||||||||||||||||||||
| 124 | =item * 'row' | ||||||||||||||||||||||
| 125 | |||||||||||||||||||||||
| 126 | A row clue. C<'index'> points to the Y coordinate. | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | =item * 'diag' | ||||||||||||||||||||||
| 129 | |||||||||||||||||||||||
| 130 | The diagonal clue. C<'index'> is ignored. | ||||||||||||||||||||||
| 131 | |||||||||||||||||||||||
| 132 | =item * 'antidiag' | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | The anti-diagonal clue. C<'index'> is ignored. | ||||||||||||||||||||||
| 135 | |||||||||||||||||||||||
| 136 | =back | ||||||||||||||||||||||
| 137 | |||||||||||||||||||||||
| 138 | Some examples: | ||||||||||||||||||||||
| 139 | |||||||||||||||||||||||
| 140 | my $letters_aref = $riddle->get_letters_of_clue({ type => 'col', index => 2, }); | ||||||||||||||||||||||
| 141 | my $letters_aref = $riddle->get_letters_of_clue({ type => 'row', index => 1, }); | ||||||||||||||||||||||
| 142 | my $letters_aref = $riddle->get_letters_of_clue({ type => 'diag', }); | ||||||||||||||||||||||
| 143 | my $letters_aref = $riddle->get_letters_of_clue({ type => 'antidiag', }); | ||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||
| 146 | |||||||||||||||||||||||
| 147 | sub get_letters_of_clue | ||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||
| 149 | 4 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||
|
Subroutine
get_letters_of_clue: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||||||||||||||
| 150 | |||||||||||||||||||||||
| 151 | my $get_index = sub { | ||||||||||||||||||||||
| 152 | 2 | ▶ | my $i = $args->{index}; | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | 2 | ▶ | if ($i !~ m{\A[01234]\z}) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||
| 156 | 0 | Carp::confess ('index must be in the range 0-4'); | |||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||
| 158 | |||||||||||||||||||||||
| 159 | 2 | return $i; | |||||||||||||||||||||
| 160 | 4 | }; | |||||||||||||||||||||
| 161 | |||||||||||||||||||||||
| 162 | 4 | my $clue_idx; | |||||||||||||||||||||
| 163 | 4 | my $type = $args->{type}; | |||||||||||||||||||||
| 164 | |||||||||||||||||||||||
| 165 | 4 | ▶ | if ($type eq 'col') | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 166 | { | ||||||||||||||||||||||
| 167 | 1 | $clue_idx = 2 + $LEN + $get_index->(); | |||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||
| 169 | elsif ($type eq 'row') | ||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||
| 171 | 1 | $clue_idx = 2 + $get_index->(); | |||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||
| 173 | elsif ($type eq 'diag') | ||||||||||||||||||||||
| 174 | { | ||||||||||||||||||||||
| 175 | 1 | $clue_idx = 0; | |||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||
| 177 | elsif ($type eq 'antidiag') | ||||||||||||||||||||||
| 178 | { | ||||||||||||||||||||||
| 179 | 1 | $clue_idx = 1; | |||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||
| 181 | else | ||||||||||||||||||||||
| 182 | { | ||||||||||||||||||||||
| 183 | 0 | Carp::confess ("Unknown type $type."); | |||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||
| 185 | |||||||||||||||||||||||
| 186 | 4 | return [map { $letters[$_-1] } @{$self->_clues->[$clue_idx]}]; | |||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||
| 188 | |||||||||||||||||||||||
| 189 | =head2 my $string = $riddle->get_riddle_v1_string() | ||||||||||||||||||||||
| 190 | |||||||||||||||||||||||
| 191 | Returns the riddle version 1 string (without the header). See the documentation | ||||||||||||||||||||||
| 192 | of L<Games::ABC_Path::Solver::Board> for explanation. | ||||||||||||||||||||||
| 193 | |||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||
| 195 | |||||||||||||||||||||||
| 196 | sub get_riddle_v1_string | ||||||||||||||||||||||
| 197 | { | ||||||||||||||||||||||
| 198 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||
|
Subroutine
get_riddle_v1_string: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||
| 199 | |||||||||||||||||||||||
| 200 | 6 | my $s = ((' ' x 7)."\n")x7; | |||||||||||||||||||||
| 201 | |||||||||||||||||||||||
| 202 | 6 | substr($s, ($self->_A_pos()->[$Y]+1) * 8 + $self->_A_pos->[$X]+1, 1) = 'A'; | |||||||||||||||||||||
| 203 | |||||||||||||||||||||||
| 204 | 6 | my $clues = $self->_clues(); | |||||||||||||||||||||
| 205 | 6 | foreach my $clue_idx (0 .. $NUM_CLUES-1) | |||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||
| 207 | 72 | ▶ | my @pos = | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 208 | ($clue_idx == 0) ? ([0,0],[6,6]) | ||||||||||||||||||||||
| 209 | : ($clue_idx == 1) ? ([0,6],[6,0]) | ||||||||||||||||||||||
| 210 | : ($clue_idx < (2+5)) ? ([1+$clue_idx-(2), 0], [1+$clue_idx-(2), 6]) | ||||||||||||||||||||||
| 211 | : ([0, 1+$clue_idx-(2+5)], [6, 1+$clue_idx-(2+5)]) | ||||||||||||||||||||||
| 212 | ; | ||||||||||||||||||||||
| 213 | |||||||||||||||||||||||
| 214 | 72 | foreach my $i (0 .. 1) | |||||||||||||||||||||
| 215 | { | ||||||||||||||||||||||
| 216 | 144 | substr ($s, $pos[$i][0] * 8 + $pos[$i][1], 1) | |||||||||||||||||||||
| 217 | = $letters[$clues->[$clue_idx]->[$i] - 1]; | ||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||
| 220 | |||||||||||||||||||||||
| 221 | 6 | return $s; | |||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||
| 223 | |||||||||||||||||||||||
| 224 | =head2 my $string = $riddle->get_final_layout() | ||||||||||||||||||||||
| 225 | |||||||||||||||||||||||
| 226 | Returns the final layout as a L<Games::ABC_Path::Generator::FinalLayoutObj> . | ||||||||||||||||||||||
| 227 | |||||||||||||||||||||||
| 228 | =cut | ||||||||||||||||||||||
| 229 | |||||||||||||||||||||||
| 230 | sub get_final_layout | ||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||
| 232 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||
|
Subroutine
get_final_layout: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 233 | |||||||||||||||||||||||
| 234 | 1 | return $self->_solution; | |||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||
| 236 | |||||||||||||||||||||||
| 237 | |||||||||||||||||||||||
| 238 | =head2 my $string = $riddle->get_final_layout_as_string({%args}) | ||||||||||||||||||||||
| 239 | |||||||||||||||||||||||
| 240 | Returns the final layout as a string. %args is included for future extension. | ||||||||||||||||||||||
| 241 | |||||||||||||||||||||||
| 242 | =cut | ||||||||||||||||||||||
| 243 | |||||||||||||||||||||||
| 244 | sub get_final_layout_as_string | ||||||||||||||||||||||
| 245 | { | ||||||||||||||||||||||
| 246 | 2 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||
|
Subroutine
get_final_layout_as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 247 | |||||||||||||||||||||||
| 248 | 2 | return $self->_solution->as_string($args); | |||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||
| 250 | |||||||||||||||||||||||
| 251 | =head1 AUTHOR | ||||||||||||||||||||||
| 252 | |||||||||||||||||||||||
| 253 | Shlomi Fish, L<http://www.shlomifish.org/> . | ||||||||||||||||||||||
| 254 | |||||||||||||||||||||||
| 255 | =head1 BUGS | ||||||||||||||||||||||
| 256 | |||||||||||||||||||||||
| 257 | Please report any bugs or feature requests to C<bug-games-abc_path-generator at rt.cpan.org>, or through | ||||||||||||||||||||||
| 258 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-ABC_Path-Generator>. I will be notified, and then you'll | ||||||||||||||||||||||
| 259 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||
| 260 | |||||||||||||||||||||||
| 261 | |||||||||||||||||||||||
| 262 | |||||||||||||||||||||||
| 263 | |||||||||||||||||||||||
| 264 | =head1 SUPPORT | ||||||||||||||||||||||
| 265 | |||||||||||||||||||||||
| 266 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||
| 267 | |||||||||||||||||||||||
| 268 | perldoc Games::ABC_Path::Generator | ||||||||||||||||||||||
| 269 | |||||||||||||||||||||||
| 270 | |||||||||||||||||||||||
| 271 | You can also look for information at: | ||||||||||||||||||||||
| 272 | |||||||||||||||||||||||
| 273 | =over 4 | ||||||||||||||||||||||
| 274 | |||||||||||||||||||||||
| 275 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||
| 276 | |||||||||||||||||||||||
| 277 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-ABC_Path-Generator> | ||||||||||||||||||||||
| 278 | |||||||||||||||||||||||
| 279 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||
| 280 | |||||||||||||||||||||||
| 281 | L<http://annocpan.org/dist/Games-ABC_Path-Generator> | ||||||||||||||||||||||
| 282 | |||||||||||||||||||||||
| 283 | =item * CPAN Ratings | ||||||||||||||||||||||
| 284 | |||||||||||||||||||||||
| 285 | L<http://cpanratings.perl.org/d/Games-ABC_Path-Generator> | ||||||||||||||||||||||
| 286 | |||||||||||||||||||||||
| 287 | =item * Search CPAN | ||||||||||||||||||||||
| 288 | |||||||||||||||||||||||
| 289 | L<http://search.cpan.org/dist/Games-ABC_Path-Generator/> | ||||||||||||||||||||||
| 290 | |||||||||||||||||||||||
| 291 | =back | ||||||||||||||||||||||
| 292 | |||||||||||||||||||||||
| 293 | |||||||||||||||||||||||
| 294 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||
| 295 | |||||||||||||||||||||||
| 296 | |||||||||||||||||||||||
| 297 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||
| 298 | |||||||||||||||||||||||
| 299 | Copyright 2011 Shlomi Fish. | ||||||||||||||||||||||
| 300 | |||||||||||||||||||||||
| 301 | This program is distributed under the MIT (X11) License: | ||||||||||||||||||||||
| 302 | L<http://www.opensource.org/licenses/mit-license.php> | ||||||||||||||||||||||
| 303 | |||||||||||||||||||||||
| 304 | Permission is hereby granted, free of charge, to any person | ||||||||||||||||||||||
| 305 | obtaining a copy of this software and associated documentation | ||||||||||||||||||||||
| 306 | files (the "Software"), to deal in the Software without | ||||||||||||||||||||||
| 307 | restriction, including without limitation the rights to use, | ||||||||||||||||||||||
| 308 | copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||||||||||||||
| 309 | copies of the Software, and to permit persons to whom the | ||||||||||||||||||||||
| 310 | Software is furnished to do so, subject to the following | ||||||||||||||||||||||
| 311 | conditions: | ||||||||||||||||||||||
| 312 | |||||||||||||||||||||||
| 313 | The above copyright notice and this permission notice shall be | ||||||||||||||||||||||
| 314 | included in all copies or substantial portions of the Software. | ||||||||||||||||||||||
| 315 | |||||||||||||||||||||||
| 316 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||||||||||||||||||||||
| 317 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||||||||||||||||||||||
| 318 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||||||||||||||||||||||
| 319 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||||||||||||||||||||||
| 320 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||||||||||||||||||||||
| 321 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||||||||||||||||||||||
| 322 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||||||||||||||||||||||
| 323 | OTHER DEALINGS IN THE SOFTWARE. | ||||||||||||||||||||||
| 324 | |||||||||||||||||||||||
| 325 | |||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||
| 327 | |||||||||||||||||||||||
| 328 | 6 | 1; # End of Games::ABC_Path::Generator | |||||||||||||||||||||