| 1 | package Games::Solitaire::Verify::FromOtherSolversBase; | ||||||||||||||||||||||||||||
| 2 | 5 | $Games::Solitaire::Verify::FromOtherSolversBase::VERSION = '0.2601'; | |||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use autodie; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use List::Util qw(first); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Path::Tiny qw/ path /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use parent 'Games::Solitaire::Verify::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use Games::Solitaire::Verify::VariantsMap (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Games::Solitaire::Verify::Solution (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Games::Solitaire::Verify::State::LaxParser (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 5 | ▶ | use Games::Solitaire::Verify::Move (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use Getopt::Long qw(GetOptionsFromArray); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub _init | ||||||||||||||||||||||||||||
| 20 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=21.8
| |||||||||||||||||||||||||||||
| 21 | 4 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | my $argv = $args->{'argv'}; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | my $variant_map = Games::Solitaire::Verify::VariantsMap->new(); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4 | my $variant_params = $variant_map->get_variant_by_id("freecell"); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | GetOptionsFromArray( | ||||||||||||||||||||||||||||
| 30 | $argv, | ||||||||||||||||||||||||||||
| 31 | 4 | ▶ | 'g|game|variant=s' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 32 | 4 | my ( undef, $game ) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | $variant_params = $variant_map->get_variant_by_id($game); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | if ( !defined($variant_params) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | { | ||||||||||||||||||||||||||||
| 38 | 0 | die "Unknown variant '$game'!\n"; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | 'freecells-num=i' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 0 | my ( undef, $n ) = @_; | |||||||||||||||||||||||||||
| 43 | 0 | $variant_params->num_freecells($n); | |||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | 'stacks-num=i' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 0 | my ( undef, $n ) = @_; | |||||||||||||||||||||||||||
| 47 | 0 | $variant_params->num_columns($n); | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | 'decks-num=i' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 50 | 0 | my ( undef, $n ) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 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
| |||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 0 | die "Decks should be 1 or 2."; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | $variant_params->num_decks($n); | |||||||||||||||||||||||||||
| 58 | }, | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | 'sequences-are-built-by=s' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 60 | 0 | my ( undef, $val ) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | my %seqs_build_by = ( | ||||||||||||||||||||||||||||
| 63 | 0 | ( map { $_ => $_ } (qw(alt_color suit rank)) ), | |||||||||||||||||||||||||||
| 64 | "alternate_color" => "alt_color", | ||||||||||||||||||||||||||||
| 65 | ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | my $proc_val = $seqs_build_by{$val}; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( !defined($proc_val) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||
| 71 | 0 | die "Unknown sequences-are-built-by '$val'!"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | $variant_params->seqs_build_by($proc_val); | |||||||||||||||||||||||||||
| 75 | }, | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | 'empty-stacks-filled-by=s' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 77 | 0 | my ( undef, $val ) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | my %empty_stacks_filled_by_map = | ||||||||||||||||||||||||||||
| 80 | 0 | ( map { $_ => 1 } (qw(kings any none)) ); | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( !exists( $empty_stacks_filled_by_map{$val} ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 0 | die "Unknown empty stacks filled by '$val'!"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | $variant_params->empty_stacks_filled_by($val); | |||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | 'sequence-move=s' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 90 | 0 | my ( undef, $val ) = @_; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | my %seq_moves = ( map { $_ => 1 } (qw(limited unlimited)) ); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( !exists( $seq_moves{$val} ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||
| 96 | 0 | die "Unknown sequence move '$val'!"; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | $variant_params->sequence_move($val); | |||||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||||||
| 101 | 4 | ▶ | ) or die "Cannot process command line arguments"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | my $filename = shift(@$argv); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 4 | ▶ | if ( !defined($filename) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||
| 107 | 0 | $filename = "-"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 4 | my $sol_filename = shift(@$argv); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 4 | ▶ | if ( !defined($sol_filename) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | { | ||||||||||||||||||||||||||||
| 114 | 0 | die "Solution filename not specified."; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 4 | $self->_variant_params($variant_params); | |||||||||||||||||||||||||||
| 118 | 4 | $self->_filename($filename); | |||||||||||||||||||||||||||
| 119 | 4 | $self->_sol_filename($sol_filename); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 4 | my $s = ''; | |||||||||||||||||||||||||||
| 122 | 4 | $self->_buffer_ref( \$s ); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 4 | return; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub _append | ||||||||||||||||||||||||||||
| 128 | 643 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_append: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | 643 | my ( $self, $text ) = @_; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 643 | ${ $self->_buffer_ref } .= $text; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 643 | return; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub _get_buffer | ||||||||||||||||||||||||||||
| 137 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_buffer: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 138 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 6 | return ${ $self->_buffer_ref }; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub _read_initial_state | ||||||||||||||||||||||||||||
| 144 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_initial_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 145 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | $self->_st( | |||||||||||||||||||||||||||
| 148 | Games::Solitaire::Verify::State::LaxParser->new( | ||||||||||||||||||||||||||||
| 149 | { | ||||||||||||||||||||||||||||
| 150 | string => path( $self->_filename )->slurp_raw, | ||||||||||||||||||||||||||||
| 151 | variant => 'custom', | ||||||||||||||||||||||||||||
| 152 | 'variant_params' => $self->_variant_params(), | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | ) | ||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 4 | $self->_append("-=-=-=-=-=-=-=-=-=-=-=-\n\n"); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 4 | $self->_out_running_state; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 4 | return; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | sub _out_running_state | ||||||||||||||||||||||||||||
| 165 | 320 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_out_running_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 166 | 320 | my ($self) = @_; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 320 | $self->_append( $self->_st->to_string() . "\n\n====================\n\n" ); | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 320 | return; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub _perform_and_output_move | ||||||||||||||||||||||||||||
| 174 | 316 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_perform_and_output_move: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | 316 | my ( $self, $move_s ) = @_; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 316 | $self->_append("$move_s\n\n"); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 316 | $self->_st->verify_and_perform_move( | |||||||||||||||||||||||||||
| 180 | Games::Solitaire::Verify::Move->new( | ||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||
| 182 | fcs_string => $move_s, | ||||||||||||||||||||||||||||
| 183 | game => $self->_st->_variant(), | ||||||||||||||||||||||||||||
| 184 | }, | ||||||||||||||||||||||||||||
| 185 | ) | ||||||||||||||||||||||||||||
| 186 | ); | ||||||||||||||||||||||||||||
| 187 | 316 | $self->_out_running_state; | |||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 316 | return; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub _find_col_card | ||||||||||||||||||||||||||||
| 193 | 327 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_find_col_card: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 194 | 327 | my ( $self, $card_s ) = @_; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | return first | ||||||||||||||||||||||||||||
| 197 | 1725 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 198 | 1725 | my $col = $self->_st->get_column($_); | |||||||||||||||||||||||||||
| 199 | 1725 | ▶ | ( $col->len == 0 ) ? 0 : $col->top->fast_s eq $card_s | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 327 | } ( 0 .. $self->_st->num_columns - 1 ); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | sub _find_empty_col | ||||||||||||||||||||||||||||
| 204 | 11 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_find_empty_col: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 205 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | return | ||||||||||||||||||||||||||||
| 208 | 42 | ▶ | first { $self->_st->get_column($_)->len == 0 } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 209 | 11 | ( 0 .. $self->_st->num_columns - 1 ); | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | sub _find_fc_card | ||||||||||||||||||||||||||||
| 213 | 85 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_find_fc_card: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | 85 | my ( $self, $card_s ) = @_; | |||||||||||||||||||||||||||
| 215 | return first | ||||||||||||||||||||||||||||
| 216 | 276 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 217 | 276 | my $card = $self->_st->get_freecell($_); | |||||||||||||||||||||||||||
| 218 | 276 | ▶ | defined($card) ? ( $card->fast_s eq $card_s ) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | 85 | } ( 0 .. $self->_st->num_freecells - 1 ); | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | sub _find_card_src_string | ||||||||||||||||||||||||||||
| 223 | 245 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_find_card_src_string: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 224 | 245 | my ( $self, $src_card_s ) = @_; | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 245 | my $src_col_idx = $self->_find_col_card($src_card_s); | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 245 | ▶ | if ( not defined $src_col_idx ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | { | ||||||||||||||||||||||||||||
| 230 | 85 | my $src_fc_idx = $self->_find_fc_card($src_card_s); | |||||||||||||||||||||||||||
| 231 | 85 | ▶ | if ( not defined($src_fc_idx) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | { | ||||||||||||||||||||||||||||
| 233 | 43 | die "Cannot find card <$src_card_s>."; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | 42 | return ( "a card", "freecell $src_fc_idx" ); | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | else | ||||||||||||||||||||||||||||
| 238 | { | ||||||||||||||||||||||||||||
| 239 | 160 | return ( "1 cards", "stack $src_col_idx" ); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | sub run | ||||||||||||||||||||||||||||
| 244 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 245 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 0 | $self->_process_main; | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | print $self->_get_buffer; | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | return; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 5 | 1; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | __END__ | ||||||||||||||||||||||||||||