| 1 | package Games::Solitaire::Verify::App::CmdLine::From_Patsolve; | ||||||||||||||||||||||||||||
| 2 | 2 | $Games::Solitaire::Verify::App::CmdLine::From_Patsolve::VERSION = '0.2301'; | |||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use parent 'Games::Solitaire::Verify::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Games::Solitaire::Verify::VariantsMap (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Games::Solitaire::Verify::Solution (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Games::Solitaire::Verify::State::LaxParser (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Games::Solitaire::Verify::Move (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use List::MoreUtils qw(firstidx); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Getopt::Long qw(GetOptionsFromArray); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | __PACKAGE__->mk_acc_ref( | |||||||||||||||||||||||||||
| 19 | [ | ||||||||||||||||||||||||||||
| 20 | qw( | ||||||||||||||||||||||||||||
| 21 | _st | ||||||||||||||||||||||||||||
| 22 | _filename | ||||||||||||||||||||||||||||
| 23 | _sol_filename | ||||||||||||||||||||||||||||
| 24 | _variant_params | ||||||||||||||||||||||||||||
| 25 | _buffer_ref | ||||||||||||||||||||||||||||
| 26 | ) | ||||||||||||||||||||||||||||
| 27 | ] | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub _init | ||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||
| 32 | 2 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=21.8
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | my $argv = $args->{'argv'}; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | my $variant_map = Games::Solitaire::Verify::VariantsMap->new(); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | my $variant_params = $variant_map->get_variant_by_id("freecell"); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | GetOptionsFromArray( | ||||||||||||||||||||||||||||
| 41 | $argv, | ||||||||||||||||||||||||||||
| 42 | 'g|game|variant=s' => sub { | ||||||||||||||||||||||||||||
| 43 | 2 | ▶ | my ( undef, $game ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | $variant_params = $variant_map->get_variant_by_id($game); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | if ( !defined($variant_params) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||
| 49 | 0 | die "Unknown variant '$game'!\n"; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | 'freecells-num=i' => sub { | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my ( undef, $n ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 54 | 0 | $variant_params->num_freecells($n); | |||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | 'stacks-num=i' => sub { | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my ( undef, $n ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 0 | $variant_params->num_columns($n); | |||||||||||||||||||||||||||
| 59 | }, | ||||||||||||||||||||||||||||
| 60 | 'decks-num=i' => sub { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ( undef, $n ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ( !( ( $n == 1 ) || ( $n == 2 ) ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$n == 1 or $n == 2
MC/DC0%0/2
$n == 1 or $n == 2
$n == 1
$n == 2
Truth table0%0/3
A or B
A= $n == 1B= $n == 2
| |||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | 0 | die "Decks should be 1 or 2."; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | $variant_params->num_decks($n); | |||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||
| 70 | 'sequences-are-built-by=s' => sub { | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | my ( undef, $val ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | my %seqs_build_by = ( | ||||||||||||||||||||||||||||
| 74 | 0 | ( map { $_ => $_ } (qw(alt_color suit rank)) ), | |||||||||||||||||||||||||||
| 75 | "alternate_color" => "alt_color", | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | my $proc_val = $seqs_build_by{$val}; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if ( !defined($proc_val) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | 0 | die "Unknown sequences-are-built-by '$val'!"; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | $variant_params->seqs_build_by($proc_val); | |||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||
| 87 | 'empty-stacks-filled-by=s' => sub { | ||||||||||||||||||||||||||||
| 88 | 0 | ▶ | my ( undef, $val ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | my %empty_stacks_filled_by_map = | ||||||||||||||||||||||||||||
| 91 | 0 | ( map { $_ => 1 } (qw(kings any none)) ); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( !exists( $empty_stacks_filled_by_map{$val} ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||
| 95 | 0 | die "Unknown empty stacks filled by '$val'!"; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | $variant_params->empty_stacks_filled_by($val); | |||||||||||||||||||||||||||
| 99 | }, | ||||||||||||||||||||||||||||
| 100 | 'sequence-move=s' => sub { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my ( undef, $val ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | my %seq_moves = ( map { $_ => 1 } (qw(limited unlimited)) ); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ( !exists( $seq_moves{$val} ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | die "Unknown sequence move '$val'!"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | $variant_params->sequence_move($val); | |||||||||||||||||||||||||||
| 111 | }, | ||||||||||||||||||||||||||||
| 112 | 2 | ▶ | ) or die "Cannot process command line arguments"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2 | my $filename = shift(@$argv); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | ▶ | if ( !defined($filename) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||
| 118 | 0 | $filename = "-"; | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 2 | my $sol_filename = shift(@$argv); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 2 | ▶ | if ( !defined($sol_filename) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | 0 | die "Solution filename not specified."; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 2 | $self->_variant_params($variant_params); | |||||||||||||||||||||||||||
| 129 | 2 | $self->_filename($filename); | |||||||||||||||||||||||||||
| 130 | 2 | $self->_sol_filename($sol_filename); | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | my $s = ''; | |||||||||||||||||||||||||||
| 133 | 2 | $self->_buffer_ref( \$s ); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | return; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub _append | ||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | 299 | ▶ | my ( $self, $text ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_append: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 299 | ${ $self->_buffer_ref } .= $text; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 299 | return; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | sub _get_buffer | ||||||||||||||||||||||||||||
| 148 | { | ||||||||||||||||||||||||||||
| 149 | 5 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_buffer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 5 | return ${ $self->_buffer_ref }; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub _slurp | ||||||||||||||||||||||||||||
| 155 | { | ||||||||||||||||||||||||||||
| 156 | 2 | ▶ | my $filename = shift; | ||||||||||||||||||||||||||
|
Subroutine
_slurp: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 2 | ▶ | open my $in, '<', $filename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | or die "Cannot open '$filename' for slurping - $!"; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | local $/; | |||||||||||||||||||||||||||
| 162 | 2 | my $contents = <$in>; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 2 | close($in); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | return $contents; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub _read_initial_state | ||||||||||||||||||||||||||||
| 170 | { | ||||||||||||||||||||||||||||
| 171 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_read_initial_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 2 | $self->_st( | |||||||||||||||||||||||||||
| 174 | Games::Solitaire::Verify::State::LaxParser->new( | ||||||||||||||||||||||||||||
| 175 | { | ||||||||||||||||||||||||||||
| 176 | string => scalar( _slurp( $self->_filename ) ), | ||||||||||||||||||||||||||||
| 177 | variant => 'custom', | ||||||||||||||||||||||||||||
| 178 | 'variant_params' => $self->_variant_params(), | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | ) | ||||||||||||||||||||||||||||
| 181 | ); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 2 | $self->_append("-=-=-=-=-=-=-=-=-=-=-=-\n\n"); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 2 | $self->_out_running_state; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 2 | return; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | sub _out_running_state | ||||||||||||||||||||||||||||
| 191 | { | ||||||||||||||||||||||||||||
| 192 | 149 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_out_running_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 149 | $self->_append( $self->_st->to_string() . "\n\n====================\n\n" ); | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 149 | return; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | sub _perform_and_output_move | ||||||||||||||||||||||||||||
| 200 | { | ||||||||||||||||||||||||||||
| 201 | 147 | ▶ | my ( $self, $move_s ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_perform_and_output_move: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 147 | $self->_append("$move_s\n\n"); | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 147 | $self->_st->verify_and_perform_move( | |||||||||||||||||||||||||||
| 206 | Games::Solitaire::Verify::Move->new( | ||||||||||||||||||||||||||||
| 207 | { | ||||||||||||||||||||||||||||
| 208 | fcs_string => $move_s, | ||||||||||||||||||||||||||||
| 209 | game => $self->_st->_variant(), | ||||||||||||||||||||||||||||
| 210 | }, | ||||||||||||||||||||||||||||
| 211 | ) | ||||||||||||||||||||||||||||
| 212 | ); | ||||||||||||||||||||||||||||
| 213 | 147 | $self->_out_running_state; | |||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | 147 | return; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | sub _find_col_card | ||||||||||||||||||||||||||||
| 219 | { | ||||||||||||||||||||||||||||
| 220 | 198 | ▶ | my ( $self, $card_s ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find_col_card: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | return firstidx | ||||||||||||||||||||||||||||
| 223 | { | ||||||||||||||||||||||||||||
| 224 | 943 | ▶ | my $col = $self->_st->get_column($_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 225 | 943 | ▶ | ( $col->len == 0 ) ? 0 : $col->top->fast_s eq $card_s | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | 198 | ( 0 .. $self->_st->num_columns - 1 ); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | sub _find_empty_col | ||||||||||||||||||||||||||||
| 231 | { | ||||||||||||||||||||||||||||
| 232 | 11 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find_empty_col: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | return firstidx | ||||||||||||||||||||||||||||
| 235 | { | ||||||||||||||||||||||||||||
| 236 | 42 | ▶ | $self->_st->get_column($_)->len == 0 | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 11 | ( 0 .. $self->_st->num_columns - 1 ); | |||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | sub _find_fc_card | ||||||||||||||||||||||||||||
| 242 | { | ||||||||||||||||||||||||||||
| 243 | 28 | ▶ | my ( $self, $card_s ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find_fc_card: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 244 | my $dest_fc_idx = firstidx | ||||||||||||||||||||||||||||
| 245 | { | ||||||||||||||||||||||||||||
| 246 | 74 | ▶ | my $card = $self->_st->get_freecell($_); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 247 | 74 | ▶ | defined($card) ? ( $card->fast_s eq $card_s ) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | 28 | ( 0 .. $self->_st->num_freecells - 1 ); | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | sub _find_card_src_string | ||||||||||||||||||||||||||||
| 253 | { | ||||||||||||||||||||||||||||
| 254 | 116 | ▶ | my ( $self, $src_card_s ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find_card_src_string: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 116 | my $src_col_idx = $self->_find_col_card($src_card_s); | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # TODO : try to find a freecell card. | ||||||||||||||||||||||||||||
| 259 | 116 | ▶ | if ( $src_col_idx < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | { | ||||||||||||||||||||||||||||
| 261 | 28 | my $src_fc_idx = $self->_find_fc_card($src_card_s); | |||||||||||||||||||||||||||
| 262 | 28 | ▶ | if ( $src_fc_idx < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | { | ||||||||||||||||||||||||||||
| 264 | 0 | die "Cannot find card <$src_card_s>."; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | 28 | return ( "a card", "freecell $src_fc_idx" ); | |||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | else | ||||||||||||||||||||||||||||
| 269 | { | ||||||||||||||||||||||||||||
| 270 | 88 | return ( "1 cards", "stack $src_col_idx" ); | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | sub _perform_move | ||||||||||||||||||||||||||||
| 275 | { | ||||||||||||||||||||||||||||
| 276 | 147 | ▶ | my ( $self, $move_line ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_perform_move: called CC=9 SCAR=23.4
| |||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 147 | ▶ | if ( my ($src_card_s) = $move_line =~ /\A(.[HCDS]) to temp\z/ ) | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 279 | { | ||||||||||||||||||||||||||||
| 280 | 31 | my $src_col_idx = $self->_find_col_card($src_card_s); | |||||||||||||||||||||||||||
| 281 | 31 | ▶ | if ( $src_col_idx < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | { | ||||||||||||||||||||||||||||
| 283 | 0 | die "Cannot find card."; | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | my $dest_fc_idx = firstidx | ||||||||||||||||||||||||||||
| 287 | { | ||||||||||||||||||||||||||||
| 288 | 80 | ▶ | !defined( $self->_st->get_freecell($_) ) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | 31 | ( 0 .. $self->_st->num_freecells - 1 ); | |||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 31 | ▶ | if ( $dest_fc_idx < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | { | ||||||||||||||||||||||||||||
| 294 | 0 | die "No empty freecell."; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | $self->_perform_and_output_move( | ||||||||||||||||||||||||||||
| 298 | 31 | sprintf( | |||||||||||||||||||||||||||
| 299 | "Move a card from stack %d to freecell %d", | ||||||||||||||||||||||||||||
| 300 | $src_col_idx, $dest_fc_idx, | ||||||||||||||||||||||||||||
| 301 | ), | ||||||||||||||||||||||||||||
| 302 | ); | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | elsif ( ($src_card_s) = $move_line =~ /\A(.[HCDS]) out\z/ ) | ||||||||||||||||||||||||||||
| 306 | { | ||||||||||||||||||||||||||||
| 307 | 54 | my @src_s = $self->_find_card_src_string($src_card_s); | |||||||||||||||||||||||||||
| 308 | 54 | $self->_perform_and_output_move( | |||||||||||||||||||||||||||
| 309 | sprintf( "Move a card from %s to the foundations", $src_s[1] ), | ||||||||||||||||||||||||||||
| 310 | ); | ||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | elsif ( ($src_card_s) = $move_line =~ /\A(.[HCDS]) to empty pile\z/ ) | ||||||||||||||||||||||||||||
| 313 | { | ||||||||||||||||||||||||||||
| 314 | 11 | my $dest_col_idx = $self->_find_empty_col; | |||||||||||||||||||||||||||
| 315 | 11 | ▶ | if ( $dest_col_idx < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | { | ||||||||||||||||||||||||||||
| 317 | 0 | die "Cannot find empty col."; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 11 | my @src_s = $self->_find_card_src_string($src_card_s); | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 11 | $self->_perform_and_output_move( | |||||||||||||||||||||||||||
| 322 | sprintf( "Move %s from %s to stack %d", @src_s, $dest_col_idx ), | ||||||||||||||||||||||||||||
| 323 | ); | ||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | elsif ( ( $src_card_s, ( my $dest_card_s ) ) = | ||||||||||||||||||||||||||||
| 326 | $move_line =~ /\A(.[HCDS]) to (.[HCDS])\z/ ) | ||||||||||||||||||||||||||||
| 327 | { | ||||||||||||||||||||||||||||
| 328 | 51 | my $dest_col_idx = $self->_find_col_card($dest_card_s); | |||||||||||||||||||||||||||
| 329 | 51 | ▶ | if ( $dest_col_idx < 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | { | ||||||||||||||||||||||||||||
| 331 | 0 | die "Cannot find card <$dest_card_s>."; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 51 | my @src_s = $self->_find_card_src_string($src_card_s); | |||||||||||||||||||||||||||
| 335 | 51 | $self->_perform_and_output_move( | |||||||||||||||||||||||||||
| 336 | sprintf( "Move %s from %s to stack %d", @src_s, $dest_col_idx ) ); | ||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | else | ||||||||||||||||||||||||||||
| 339 | { | ||||||||||||||||||||||||||||
| 340 | 0 | die "Unrecognised move_line <$move_line>"; | |||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | sub _process_main | ||||||||||||||||||||||||||||
| 345 | { | ||||||||||||||||||||||||||||
| 346 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_process_main: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 1 | $self->_read_initial_state; | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 1 | open my $in_fh, '<', $self->_sol_filename; | |||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 1 | ▶ | while ( my $l = <$in_fh> ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | { | ||||||||||||||||||||||||||||
| 354 | 128 | chomp($l); | |||||||||||||||||||||||||||
| 355 | 128 | $self->_perform_move($l); | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | 1 | close($in_fh); | |||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 1 | $self->_append("This game is solveable.\n"); | |||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | 1 | return; | |||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | sub run | ||||||||||||||||||||||||||||
| 366 | { | ||||||||||||||||||||||||||||
| 367 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 0 | $self->_process_main; | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 0 | print $self->_get_buffer; | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 0 | return; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 2 | 1; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | __END__ | ||||||||||||||||||||||||||||