| 1 | package Games::Solitaire::Verify::App::CmdLine::From_ShirlHartSolver; | ||||||||||||||
| 2 | 2 | $Games::Solitaire::Verify::App::CmdLine::From_ShirlHartSolver::VERSION = '0.2601'; | |||||||||||||
| 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::FromOtherSolversBase'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | 2 | ▶ | use List::Util qw(first); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 2 | __PACKAGE__->mk_acc_ref( | |||||||||||||
| 12 | [ | ||||||||||||||
| 13 | qw( | ||||||||||||||
| 14 | _move_was_performed | ||||||||||||||
| 15 | _input_move_index | ||||||||||||||
| 16 | _st | ||||||||||||||
| 17 | _filename | ||||||||||||||
| 18 | _sol_filename | ||||||||||||||
| 19 | _variant_params | ||||||||||||||
| 20 | _buffer_ref | ||||||||||||||
| 21 | ) | ||||||||||||||
| 22 | ] | ||||||||||||||
| 23 | ); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | 2 | my $COL_RE = qr/[1-8]/; | |||||||||||||
| 26 | 2 | my $FREECELL_RE = qr/[abcd]/; | |||||||||||||
| 27 | |||||||||||||||
| 28 | sub _perform_and_output_move_wrapper | ||||||||||||||
| 29 | 83 | ▶ | { | ||||||||||||
|
Subroutine
_perform_and_output_move_wrapper: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 83 | my $self = shift; | |||||||||||||
| 31 | |||||||||||||||
| 32 | 83 | $self->_perform_and_output_move(shift); | |||||||||||||
| 33 | 83 | return $self->_move_was_performed(1); | |||||||||||||
| 34 | } | ||||||||||||||
| 35 | |||||||||||||||
| 36 | sub _analyze_shirl_hart_move | ||||||||||||||
| 37 | 83 | ▶ | { | ||||||||||||
|
Subroutine
_analyze_shirl_hart_move: called CC=15 SCAR=28.8
| |||||||||||||||
| 38 | 83 | my ( $self, $src_char, $dest_char, $move_line, $src_card, $dest ) = @_; | |||||||||||||
| 39 | 83 | my %fc = ( a => 0, b => 1, c => 2, d => 3 ); | |||||||||||||
| 40 | 43 | ▶ | my $is_invalid_dest_col = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 41 | 43 | my ($dest_col_idx) = @_; | |||||||||||||
| 42 | 43 | my $dcol = $self->_st->get_column($dest_col_idx); | |||||||||||||
| 43 | return ( | ||||||||||||||
| 44 | 43 | ▶ | $dest eq 'e' | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | ? ( $dcol->len() > 0 ) | ||||||||||||||
| 46 | : ( $dest ne $dcol->top->to_string ) | ||||||||||||||
| 47 | ); | ||||||||||||||
| 48 | 83 | }; | |||||||||||||
| 49 | 141 | ▶ | my $try_col = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||
| 50 | 141 | my $c = shift; | |||||||||||||
| 51 | 141 | ▶ | if ( $c =~ /\A$COL_RE\z/ ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | { | ||||||||||||||
| 53 | 118 | return $c - 1; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | 23 | return undef; | |||||||||||||
| 56 | 83 | }; | |||||||||||||
| 57 | 23 | ▶ | my $try_foundation = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||
| 58 | 23 | ▶ | if ( $dest_char eq 'h' ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | { | ||||||||||||||
| 60 | 18 | ▶ | if ( $dest ne 'h' ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | { | ||||||||||||||
| 62 | 0 | die "wrong move foundations - $move_line"; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | 18 | return 1; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | 5 | return; | |||||||||||||
| 67 | 83 | }; | |||||||||||||
| 68 | 83 | ▶ | my $try_fc = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 69 | 83 | my $c = shift; | |||||||||||||
| 70 | 83 | return $fc{$c}; | |||||||||||||
| 71 | 83 | }; | |||||||||||||
| 72 | 83 | ▶ | if ( defined( my $src_col_idx = $try_col->($src_char) ) ) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 73 | { | ||||||||||||||
| 74 | 75 | ▶ | if ( defined( my $dest_fc_idx = $try_fc->($dest_char) ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | { | ||||||||||||||
| 76 | 22 | ▶ | if ( $dest ne 'f' ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | { | ||||||||||||||
| 78 | 0 | die "wrong move to freecell - $move_line"; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | 22 | ▶ | if ( $src_card ne | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | $self->_st->get_column($src_col_idx)->top->to_string ) | ||||||||||||||
| 82 | { | ||||||||||||||
| 83 | 0 | die "wrong move to freecell - $move_line"; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 22 | return $self->_perform_and_output_move_wrapper( | |||||||||||||
| 87 | sprintf( | ||||||||||||||
| 88 | "Move a card from stack %d to freecell %d", | ||||||||||||||
| 89 | $src_col_idx, $dest_fc_idx, | ||||||||||||||
| 90 | ), | ||||||||||||||
| 91 | ); | ||||||||||||||
| 92 | } | ||||||||||||||
| 93 | 53 | ▶ | if ( defined( my $dest_col_idx = $try_col->($dest_char) ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | { | ||||||||||||||
| 95 | 38 | $src_card = substr( $src_card, 0, 2 ); | |||||||||||||
| 96 | 38 | my $col = $self->_st->get_column($src_col_idx); | |||||||||||||
| 97 | 167 | ▶ | my $idx = first { $col->pos($_)->to_string eq $src_card } | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 98 | 38 | ( 0 .. $col->len - 1 ); | |||||||||||||
| 99 | 38 | ▶ | if ( !defined $idx ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | { | ||||||||||||||
| 101 | 0 | die "wrong move stack to stack - $move_line"; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | 38 | ▶ | if ( $is_invalid_dest_col->($dest_col_idx) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | { | ||||||||||||||
| 105 | 0 | die "wrong move stack to stack - $move_line"; | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | |||||||||||||||
| 108 | 38 | return $self->_perform_and_output_move_wrapper( | |||||||||||||
| 109 | sprintf( | ||||||||||||||
| 110 | "Move %d cards from stack %d to stack %d", | ||||||||||||||
| 111 | $col->len() - $idx, | ||||||||||||||
| 112 | $src_col_idx, $dest_col_idx, | ||||||||||||||
| 113 | ), | ||||||||||||||
| 114 | ); | ||||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 15 | ▶ | if ( $try_foundation->() ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | { | ||||||||||||||
| 119 | 15 | ▶ | if ( $src_card ne | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | $self->_st->get_column($src_col_idx)->top->to_string ) | ||||||||||||||
| 121 | { | ||||||||||||||
| 122 | 0 | die "wrong move stack to foundations - $move_line"; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 15 | return $self->_perform_and_output_move_wrapper( | |||||||||||||
| 126 | sprintf( "Move a card from stack %d to the foundations", | ||||||||||||||
| 127 | $src_col_idx ), | ||||||||||||||
| 128 | ); | ||||||||||||||
| 129 | } | ||||||||||||||
| 130 | } | ||||||||||||||
| 131 | elsif ( defined( my $src_fc_idx = $try_fc->($src_char) ) ) | ||||||||||||||
| 132 | { | ||||||||||||||
| 133 | 8 | ▶ | if ( $src_card ne $self->_st->get_freecell($src_fc_idx)->to_string ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 134 | { | ||||||||||||||
| 135 | 0 | die "wrong freecell card - $move_line"; | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | 8 | ▶ | if ( $try_foundation->() ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 138 | { | ||||||||||||||
| 139 | 3 | return $self->_perform_and_output_move_wrapper( | |||||||||||||
| 140 | sprintf( "Move a card from freecell %d to the foundations", | ||||||||||||||
| 141 | $src_fc_idx ), | ||||||||||||||
| 142 | ); | ||||||||||||||
| 143 | } | ||||||||||||||
| 144 | |||||||||||||||
| 145 | 5 | ▶ | if ( defined( my $dest_col_idx = $try_col->($dest_char) ) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | { | ||||||||||||||
| 147 | 5 | ▶ | if ( $is_invalid_dest_col->($dest_col_idx) ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 148 | { | ||||||||||||||
| 149 | 0 | die "wrong move freecell to stack - $move_line"; | |||||||||||||
| 150 | } | ||||||||||||||
| 151 | |||||||||||||||
| 152 | 5 | return $self->_perform_and_output_move_wrapper( | |||||||||||||
| 153 | sprintf( | ||||||||||||||
| 154 | "Move a card from freecell %d to stack %d", | ||||||||||||||
| 155 | $src_fc_idx, $dest_col_idx, | ||||||||||||||
| 156 | ), | ||||||||||||||
| 157 | ); | ||||||||||||||
| 158 | } | ||||||||||||||
| 159 | } | ||||||||||||||
| 160 | 0 | die "wrong move - $move_line"; | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | sub _perform_move | ||||||||||||||
| 164 | 83 | ▶ | { | ||||||||||||
|
Subroutine
_perform_move: called CC=5 SCAR=17.0
| |||||||||||||||
| 165 | 83 | my ( $self, $move_line ) = @_; | |||||||||||||
| 166 | 83 | $self->_move_was_performed(0); | |||||||||||||
| 167 | |||||||||||||||
| 168 | 83 | my @fields = ( split /\|/, $move_line, -1 ); | |||||||||||||
| 169 | 83 | ▶ | if ( @fields != 5 ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 170 | { | ||||||||||||||
| 171 | 0 | die "Wrong $move_line"; | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | 83 | my $idx = shift @fields; | |||||||||||||
| 174 | 83 | ▶ | if ( $idx ne $self->_input_move_index ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 175 | { | ||||||||||||||
| 176 | 0 | die "wrong move index - $move_line"; | |||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 83 | my ( $move_s, $src_card, $dest, $found_moves ) = @fields; | |||||||||||||
| 180 | |||||||||||||||
| 181 | 83 | my @src_dest = ( $move_s =~ /(.)/gms ); | |||||||||||||
| 182 | 83 | ▶ | if ( @src_dest != 2 ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 183 | { | ||||||||||||||
| 184 | 0 | die "bad length in move <<$move_s>>!"; | |||||||||||||
| 185 | } | ||||||||||||||
| 186 | 83 | my ( $src_char, $dest_char ) = @src_dest; | |||||||||||||
| 187 | 83 | $self->_analyze_shirl_hart_move( $src_char, $dest_char, $move_line, | |||||||||||||
| 188 | $src_card, $dest ); | ||||||||||||||
| 189 | 83 | ▶ | die "wrong move - $move_line" if not $self->_move_was_performed; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 190 | 83 | $self->_perform_autoflush_to_foundation_moves( $found_moves, $move_line ); | |||||||||||||
| 191 | |||||||||||||||
| 192 | 83 | $self->_input_move_index( $self->_input_move_index + 1 ); | |||||||||||||
| 193 | 83 | return; | |||||||||||||
| 194 | } | ||||||||||||||
| 195 | |||||||||||||||
| 196 | sub _perform_autoflush_to_foundation_moves | ||||||||||||||
| 197 | 83 | ▶ | { | ||||||||||||
|
Subroutine
_perform_autoflush_to_foundation_moves: called CC=8 SCAR=21.1
| |||||||||||||||
| 198 | 83 | my ( $self, $found_moves, $move_line ) = @_; | |||||||||||||
| 199 | 83 | ▶ | return if ( not length $found_moves ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 200 | 21 | my $map; | |||||||||||||
| 201 | my %suits; | ||||||||||||||
| 202 | 43 | ▶ | $map = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=19.3
| |||||||||||||||
| 203 | 43 | my $s = shift; | |||||||||||||
| 204 | 43 | ▶ | if ( length($s) == 2 ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | { | ||||||||||||||
| 206 | 13 | return $map->("$s-$s"); | |||||||||||||
| 207 | } | ||||||||||||||
| 208 | 30 | ▶ | my ( $start, $end ) = $s =~ /\A(\S\S)-(\S\S)\z/ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 209 | or die "wrong found_moves <<$s>>!"; | ||||||||||||||
| 210 | 30 | my $sc = Games::Solitaire::Verify::Card->new( { string => $start } ); | |||||||||||||
| 211 | 30 | my $ec = Games::Solitaire::Verify::Card->new( { string => $end } ); | |||||||||||||
| 212 | 30 | ▶ | if ( $sc->suit ne $ec->suit ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 213 | { | ||||||||||||||
| 214 | 0 | die "uiui"; | |||||||||||||
| 215 | } | ||||||||||||||
| 216 | 30 | ▶ | if ( exists $suits{ $sc->suit } ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 217 | { | ||||||||||||||
| 218 | 0 | die "duplicate"; | |||||||||||||
| 219 | } | ||||||||||||||
| 220 | 30 | ▶ | if ( $sc->rank > $ec->rank ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 221 | { | ||||||||||||||
| 222 | 0 | die "foo"; | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | 30 | $suits{ $sc->suit } = { start => $sc, end => $ec }; | |||||||||||||
| 225 | 30 | return; | |||||||||||||
| 226 | 21 | }; | |||||||||||||
| 227 | 21 | foreach my $f ( split /;/, $found_moves, -1 ) | |||||||||||||
| 228 | { | ||||||||||||||
| 229 | 30 | $map->($f); | |||||||||||||
| 230 | } | ||||||||||||||
| 231 | 21 | my $count = 1; | |||||||||||||
| 232 | FOUNDATION: | ||||||||||||||
| 233 | 21 | ▶ | while ( $count > 0 ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 234 | { | ||||||||||||||
| 235 | 107 | $count = 0; | |||||||||||||
| 236 | 107 | foreach my $suit ( sort( keys %suits ) ) | |||||||||||||
| 237 | { | ||||||||||||||
| 238 | 129 | my @src_s; | |||||||||||||
| 239 | 129 | my $rec = $suits{$suit}; | |||||||||||||
| 240 | 129 | my $start = $rec->{start}; | |||||||||||||
| 241 | 129 | eval { @src_s = $self->_find_card_src_string( $start->to_string ); }; | |||||||||||||
| 242 | 129 | ▶ | if ( !$@ ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 243 | { | ||||||||||||||
| 244 | 86 | ++$count; | |||||||||||||
| 245 | 86 | my $rank = $start->rank; | |||||||||||||
| 246 | 86 | ▶ | if ( $rank == $rec->{end}->rank ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 247 | { | ||||||||||||||
| 248 | 30 | delete $suits{$suit}; | |||||||||||||
| 249 | } | ||||||||||||||
| 250 | else | ||||||||||||||
| 251 | { | ||||||||||||||
| 252 | 56 | $start->rank( 1 + $rank ); | |||||||||||||
| 253 | } | ||||||||||||||
| 254 | 86 | $self->_perform_and_output_move( | |||||||||||||
| 255 | sprintf( "Move a card from %s to the foundations", | ||||||||||||||
| 256 | $src_s[1] ), | ||||||||||||||
| 257 | ); | ||||||||||||||
| 258 | 86 | next FOUNDATION; | |||||||||||||
| 259 | } | ||||||||||||||
| 260 | } | ||||||||||||||
| 261 | } | ||||||||||||||
| 262 | 21 | ▶ | if (%suits) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 263 | { | ||||||||||||||
| 264 | 0 | die "cannot move to foundations - $move_line"; | |||||||||||||
| 265 | } | ||||||||||||||
| 266 | } | ||||||||||||||
| 267 | |||||||||||||||
| 268 | sub _process_main | ||||||||||||||
| 269 | 2 | ▶ | { | ||||||||||||
|
Subroutine
_process_main: called CC=4 SCAR=13.9
| |||||||||||||||
| 270 | 2 | my $self = shift; | |||||||||||||
| 271 | |||||||||||||||
| 272 | 2 | $self->_read_initial_state; | |||||||||||||
| 273 | 2 | $self->_input_move_index(1); | |||||||||||||
| 274 | |||||||||||||||
| 275 | 2 | open my $in_fh, '<', $self->_sol_filename; | |||||||||||||
| 276 | |||||||||||||||
| 277 | 2 | my $l; | |||||||||||||
| 278 | FIRST_lines: | ||||||||||||||
| 279 | 2 | ▶ | while ( $l = <$in_fh> ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 280 | { | ||||||||||||||
| 281 | 2 | chomp($l); | |||||||||||||
| 282 | 2 | ▶ | last FIRST_lines if ( $l =~ /\|/ ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 283 | } | ||||||||||||||
| 284 | |||||||||||||||
| 285 | 2 | ▶ | while ( $l =~ /\|/ ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 286 | { | ||||||||||||||
| 287 | 83 | $self->_perform_move($l); | |||||||||||||
| 288 | 83 | $l = <$in_fh>; | |||||||||||||
| 289 | 83 | chomp $l; | |||||||||||||
| 290 | } | ||||||||||||||
| 291 | 2 | close($in_fh); | |||||||||||||
| 292 | |||||||||||||||
| 293 | 2 | $self->_append("This game is solveable.\n"); | |||||||||||||
| 294 | |||||||||||||||
| 295 | 2 | return; | |||||||||||||
| 296 | } | ||||||||||||||
| 297 | |||||||||||||||
| 298 | 2 | 1; | |||||||||||||
| 299 | |||||||||||||||
| 300 | __END__ | ||||||||||||||