| 1 | package Games::Solitaire::BlackHole::Solver::App::Base; | ||||||||||||||||||||||||||||
| 2 | 6 | $Games::Solitaire::BlackHole::Solver::App::Base::VERSION = '0.4.1'; | |||||||||||||||||||||||||||
| 3 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use Getopt::Long qw/ GetOptions /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Pod::Usage qw/ pod2usage /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Math::Random::MT (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use List::Util 1.34 qw/ any /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | extends('Exporter'); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | has [ | |||||||||||||||||||||||||||
| 12 | '_active_record', '_active_task', | ||||||||||||||||||||||||||||
| 13 | '_board_cards', '_board_lines', | ||||||||||||||||||||||||||||
| 14 | '_board_values', '_init_foundation', | ||||||||||||||||||||||||||||
| 15 | '_init_queue', '_init_tasks_configs', | ||||||||||||||||||||||||||||
| 16 | '_is_good_diff', '_prelude', | ||||||||||||||||||||||||||||
| 17 | '_prelude_iter', '_prelude_string', | ||||||||||||||||||||||||||||
| 18 | '_talon_cards', '_positions', | ||||||||||||||||||||||||||||
| 19 | '_quiet', '_output_handle', | ||||||||||||||||||||||||||||
| 20 | '_output_fn', '_tasks', | ||||||||||||||||||||||||||||
| 21 | '_tasks_by_names', '_task_idx', | ||||||||||||||||||||||||||||
| 22 | ] => ( is => 'rw' ); | ||||||||||||||||||||||||||||
| 23 | 6 | our %EXPORT_TAGS = ( 'all' => [qw($card_re)] ); | |||||||||||||||||||||||||||
| 24 | 6 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 6 | my @ranks = ( "A", 2 .. 9, qw(T J Q K) ); | |||||||||||||||||||||||||||
| 27 | 6 | my %ranks_to_n = ( map { $ranks[$_] => $_ } 0 .. $#ranks ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub _RANK_KING | ||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | 2 | ▶ | return $ranks_to_n{'K'}; | ||||||||||||||||||||||||||
|
Subroutine
_RANK_KING: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 6 | my $card_re_str = '[' . join( "", @ranks ) . '][HSCD]'; | |||||||||||||||||||||||||||
| 35 | 6 | our $card_re = qr{$card_re_str}; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub _get_rank | ||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | 104 | ▶ | shift; | ||||||||||||||||||||||||||
|
Subroutine
_get_rank: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | 104 | return $ranks_to_n{ substr( shift(), 0, 1 ) }; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub _calc_lines | ||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||
| 45 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_calc_lines: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 46 | 2 | my $filename = shift; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | my @lines; | |||||||||||||||||||||||||||
| 49 | 2 | ▶ | if ( $filename eq "-" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 0 | @lines = <STDIN>; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | else | ||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 2 | ▶ | open my $in, "<", $filename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | or die | ||||||||||||||||||||||||||||
| 57 | "Could not open $filename for inputting the board lines - $!"; | ||||||||||||||||||||||||||||
| 58 | 2 | @lines = <$in>; | |||||||||||||||||||||||||||
| 59 | 2 | close($in); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 2 | chomp @lines; | |||||||||||||||||||||||||||
| 62 | 2 | $self->_board_lines( \@lines ); | |||||||||||||||||||||||||||
| 63 | 2 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub _trace_solution | ||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||
| 68 | 2 | ▶ | my ( $self, $final_state ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_trace_solution: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 69 | 2 | my $output_handle = $self->_output_handle; | |||||||||||||||||||||||||||
| 70 | 2 | $output_handle->print("Solved!\n"); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | ▶ | return if $self->_quiet; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 2 | my $state = $final_state; | |||||||||||||||||||||||||||
| 75 | 2 | my ( $prev_state, $col_idx ); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 2 | my @moves; | |||||||||||||||||||||||||||
| 78 | LOOP: | ||||||||||||||||||||||||||||
| 79 | 2 | ▶ | while ( ( $prev_state, $col_idx ) = @{ $self->_positions->{$state} } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | 99 | ▶ | last LOOP if not defined $prev_state; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | push @moves, | ||||||||||||||||||||||||||||
| 83 | ( | ||||||||||||||||||||||||||||
| 84 | 97 | ▶ | ( $col_idx == @{ $self->_board_cards } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | ? "Deal talon " . $self->_talon_cards->[ vec( $prev_state, 1, 8 ) ] | ||||||||||||||||||||||||||||
| 86 | : $self->_board_cards->[$col_idx] | ||||||||||||||||||||||||||||
| 87 | [ vec( $prev_state, 4 + $col_idx, 4 ) - 1 ] | ||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | continue | ||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 97 | $state = $prev_state; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 2 | print {$output_handle} map { "$_\n" } reverse(@moves); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | return; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub _my_exit | ||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 2 | ▶ | my ( $self, $verdict, ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_my_exit: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 102 | 2 | my $output_handle = $self->_output_handle; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | ▶ | if ( !$verdict ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 0 | $output_handle->print("Unsolved!\n"); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | if ( defined( $self->_output_fn ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 2 | close($output_handle); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 2 | exit( !$verdict ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | sub _parse_board | ||||||||||||||||||||||||||||
| 118 | { | ||||||||||||||||||||||||||||
| 119 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse_board: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 120 | 2 | my $lines = $self->_board_lines; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 2 | my $found_line = shift(@$lines); | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 2 | my $init_foundation; | |||||||||||||||||||||||||||
| 125 | 2 | ▶ | if ( my ($card) = $found_line =~ m{\AFoundations: ($card_re)\z} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||
| 127 | 2 | $init_foundation = $self->_get_rank($card); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | else | ||||||||||||||||||||||||||||
| 130 | { | ||||||||||||||||||||||||||||
| 131 | 0 | die "Could not match first foundation line!"; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 2 | $self->_init_foundation($init_foundation); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | $self->_board_cards( [ map { [ split /\s+/, $_ ] } @$lines ] ); | |||||||||||||||||||||||||||
| 136 | $self->_board_values( | ||||||||||||||||||||||||||||
| 137 | [ | ||||||||||||||||||||||||||||
| 138 | map { | ||||||||||||||||||||||||||||
| 139 | 24 | [ map { $self->_get_rank($_) } @$_ ] | |||||||||||||||||||||||||||
| 140 | 2 | } @{ $self->_board_cards } | |||||||||||||||||||||||||||
| 141 | ] | ||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||
| 143 | 2 | return; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub _set_up_initial_position | ||||||||||||||||||||||||||||
| 147 | { | ||||||||||||||||||||||||||||
| 148 | 2 | ▶ | my ( $self, $talon_ptr ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_up_initial_position: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2 | my $init_state = ""; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 2 | vec( $init_state, 0, 8 ) = $self->_init_foundation; | |||||||||||||||||||||||||||
| 153 | 2 | vec( $init_state, 1, 8 ) = $talon_ptr; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 2 | my $board_values = $self->_board_values; | |||||||||||||||||||||||||||
| 156 | 2 | foreach my $col_idx ( keys @$board_values ) | |||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||
| 158 | vec( $init_state, 4 + $col_idx, 4 ) = | ||||||||||||||||||||||||||||
| 159 | 24 | scalar( @{ $board_values->[$col_idx] } ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # The values of $positions is an array reference with the 0th key being the | ||||||||||||||||||||||||||||
| 163 | # previous state, and the 1th key being the column of the move. | ||||||||||||||||||||||||||||
| 164 | 2 | $self->_positions( +{ $init_state => [ undef, undef, 1, 0, ], } ); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | $self->_init_queue( [$init_state] ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 2 | return; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub _shuffle | ||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my ( $self, $gen, $arr ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_shuffle: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 0 | my $i = $#$arr; | |||||||||||||||||||||||||||
| 176 | 0 | ▶ | while ( $i > 0 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | { | ||||||||||||||||||||||||||||
| 178 | 0 | my $j = int( $gen->rand( $i + 1 ) ); | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ( $i != $j ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | { | ||||||||||||||||||||||||||||
| 181 | 0 | @$arr[ $i, $j ] = @$arr[ $j, $i ]; | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | 0 | --$i; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 0 | return; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 6 | my $TASK_NAME_RE = qr/[A-Za-z0-9_]+/; | |||||||||||||||||||||||||||
| 189 | 6 | my $TASK_ALLOC_RE = qr/[0-9]+\@$TASK_NAME_RE/; | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | sub _process_cmd_line | ||||||||||||||||||||||||||||
| 192 | { | ||||||||||||||||||||||||||||
| 193 | 2 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_process_cmd_line: called CC=9 SCAR=28.6
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 2 | my $quiet = ''; | |||||||||||||||||||||||||||
| 196 | 2 | my $output_fn; | |||||||||||||||||||||||||||
| 197 | 2 | my ( $help, $man, $version ); | |||||||||||||||||||||||||||
| 198 | 2 | my @tasks; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | my $push_task = sub { | ||||||||||||||||||||||||||||
| 201 | 2 | ▶ | push @tasks, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | +{ | ||||||||||||||||||||||||||||
| 203 | name => undef(), | ||||||||||||||||||||||||||||
| 204 | seed => 0, | ||||||||||||||||||||||||||||
| 205 | }; | ||||||||||||||||||||||||||||
| 206 | 2 | return; | |||||||||||||||||||||||||||
| 207 | 2 | }; | |||||||||||||||||||||||||||
| 208 | 2 | $push_task->(); | |||||||||||||||||||||||||||
| 209 | GetOptions( | ||||||||||||||||||||||||||||
| 210 | "o|output=s" => \$output_fn, | ||||||||||||||||||||||||||||
| 211 | "quiet!" => \$quiet, | ||||||||||||||||||||||||||||
| 212 | "next-task" => sub { | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | $push_task->(); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 214 | 0 | return; | |||||||||||||||||||||||||||
| 215 | }, | ||||||||||||||||||||||||||||
| 216 | "prelude=s" => sub { | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | my ( undef, $val ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 218 | 0 | ▶ | if ( $val !~ /\A$TASK_ALLOC_RE(?:,$TASK_ALLOC_RE)*\z/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | { | ||||||||||||||||||||||||||||
| 220 | 0 | die "Invalid prelude string '$val' !"; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | 0 | $self->_prelude_string($val); | |||||||||||||||||||||||||||
| 223 | 0 | return; | |||||||||||||||||||||||||||
| 224 | }, | ||||||||||||||||||||||||||||
| 225 | "task-name=s" => sub { | ||||||||||||||||||||||||||||
| 226 | 0 | ▶ | my ( undef, $val ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | if ( $val !~ /\A$TASK_NAME_RE\z/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | { | ||||||||||||||||||||||||||||
| 229 | 0 | die "Invalid task name '$val' - must be alphanumeric!"; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | 0 | $tasks[-1]->{name} = $val; | |||||||||||||||||||||||||||
| 232 | 0 | return; | |||||||||||||||||||||||||||
| 233 | }, | ||||||||||||||||||||||||||||
| 234 | "seed=i" => sub { | ||||||||||||||||||||||||||||
| 235 | 0 | ▶ | my ( undef, $val ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 236 | 0 | $tasks[-1]->{seed} = $val; | |||||||||||||||||||||||||||
| 237 | 0 | return; | |||||||||||||||||||||||||||
| 238 | }, | ||||||||||||||||||||||||||||
| 239 | 'help|h|?' => \$help, | ||||||||||||||||||||||||||||
| 240 | 'man' => \$man, | ||||||||||||||||||||||||||||
| 241 | 'version' => \$version, | ||||||||||||||||||||||||||||
| 242 | 2 | ▶ | %{ $args->{extra_flags} }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | ) or pod2usage(2); | ||||||||||||||||||||||||||||
| 244 | 2 | ▶ | if ( @tasks == 1 ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | { | ||||||||||||||||||||||||||||
| 246 | 2 | $tasks[-1]{name} = 'default'; | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | 2 | ▶ | if ( any { !defined $_->{name} } @tasks ) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 249 | { | ||||||||||||||||||||||||||||
| 250 | 0 | die "You did not specify the task-names for some tasks"; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | 2 | $self->_init_tasks_configs( \@tasks ); | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 2 | ▶ | pod2usage(1) if $help; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 2 | ▶ | pod2usage( -exitstatus => 0, -verbose => 2 ) if $man; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 2 | ▶ | if ($version) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | { | ||||||||||||||||||||||||||||
| 259 | 0 | ||||||||||||||||||||||||||||
| 260 | "black-hole-solve version $Games::Solitaire::BlackHole::Solver::App::Base::VERSION\n"; | ||||||||||||||||||||||||||||
| 261 | 0 | exit(0); | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 2 | $self->_quiet($quiet); | |||||||||||||||||||||||||||
| 265 | 2 | my $output_handle; | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 2 | ▶ | if ( defined($output_fn) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | { | ||||||||||||||||||||||||||||
| 269 | 2 | ▶ | open( $output_handle, ">", $output_fn ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | or die "Could not open '$output_fn' for writing"; | ||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | else | ||||||||||||||||||||||||||||
| 273 | { | ||||||||||||||||||||||||||||
| 274 | 0 | open( $output_handle, ">&STDOUT" ); | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | 2 | $self->_output_fn($output_fn); | |||||||||||||||||||||||||||
| 277 | 2 | $self->_output_handle($output_handle); | |||||||||||||||||||||||||||
| 278 | 2 | $self->_calc_lines( shift(@ARGV) ); | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 2 | return; | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | sub _set_up_tasks | ||||||||||||||||||||||||||||
| 284 | { | ||||||||||||||||||||||||||||
| 285 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_up_tasks: called CC=6 SCAR=21.4
| |||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 2 | my @tasks; | |||||||||||||||||||||||||||
| 288 | my %tasks_by_names; | ||||||||||||||||||||||||||||
| 289 | 2 | foreach my $task_rec ( @{ $self->_init_tasks_configs } ) | |||||||||||||||||||||||||||
| 290 | { | ||||||||||||||||||||||||||||
| 291 | 2 | my $iseed = $task_rec->{seed}; | |||||||||||||||||||||||||||
| 292 | 2 | my $name = $task_rec->{name}; | |||||||||||||||||||||||||||
| 293 | 2 | my $_task_idx = @tasks; | |||||||||||||||||||||||||||
| 294 | my $task_obj = | ||||||||||||||||||||||||||||
| 295 | Games::Solitaire::BlackHole::Solver::App::Base::Task->new( | ||||||||||||||||||||||||||||
| 296 | { | ||||||||||||||||||||||||||||
| 297 | _name => $name, | ||||||||||||||||||||||||||||
| 298 | 2 | ▶ | _queue => [ @{ $self->_init_queue } ], | ||||||||||||||||||||||||||
|
Condition50%1/2
$iseed || 1
MC/DC0%0/1
$iseed || 1
$iseed
Truth table50%1/2
A
A= $iseed
| |||||||||||||||||||||||||||||
| 299 | _seed => $iseed, | ||||||||||||||||||||||||||||
| 300 | _gen => Math::Random::MT->new( $iseed || 1 ), | ||||||||||||||||||||||||||||
| 301 | _remaining_iters => 100, | ||||||||||||||||||||||||||||
| 302 | _task_idx => $_task_idx, | ||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | ); | ||||||||||||||||||||||||||||
| 305 | 2 | push @tasks, $task_obj; | |||||||||||||||||||||||||||
| 306 | 2 | ▶ | if ( exists $tasks_by_names{$name} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | { | ||||||||||||||||||||||||||||
| 308 | 0 | die "Duplicate task-name '$name'!"; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | 2 | $tasks_by_names{$name} = $task_obj; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | 2 | $self->_task_idx(0); | |||||||||||||||||||||||||||
| 313 | 2 | $self->_tasks( \@tasks ); | |||||||||||||||||||||||||||
| 314 | 2 | $self->_tasks_by_names( \%tasks_by_names ); | |||||||||||||||||||||||||||
| 315 | 2 | my @prelude; | |||||||||||||||||||||||||||
| 316 | my $process_item = sub { | ||||||||||||||||||||||||||||
| 317 | 0 | ▶ | my $s = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 318 | 0 | ▶ | if ( my ( $quota, $name ) = $s =~ /\A([0-9]+)\@($TASK_NAME_RE)\z/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 319 | { | ||||||||||||||||||||||||||||
| 320 | 0 | ▶ | if ( not exists $self->_tasks_by_names->{$name} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | { | ||||||||||||||||||||||||||||
| 322 | 0 | die "Unknown task name $name in prelude!"; | |||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | 0 | my $task_obj = $self->_tasks_by_names->{$name}; | |||||||||||||||||||||||||||
| 325 | 0 | return Games::Solitaire::BlackHole::Solver::App::Base::PreludeItem | |||||||||||||||||||||||||||
| 326 | ->new( | ||||||||||||||||||||||||||||
| 327 | { | ||||||||||||||||||||||||||||
| 328 | _quota => $quota, | ||||||||||||||||||||||||||||
| 329 | _task => $task_obj, | ||||||||||||||||||||||||||||
| 330 | _task_idx => $task_obj->_task_idx, | ||||||||||||||||||||||||||||
| 331 | _task_name => $task_obj->_name, | ||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | ); | ||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | else | ||||||||||||||||||||||||||||
| 336 | { | ||||||||||||||||||||||||||||
| 337 | 0 | die "foo"; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | 2 | }; | |||||||||||||||||||||||||||
| 340 | 2 | ▶ | if ( my $_prelude_string = $self->_prelude_string ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | { | ||||||||||||||||||||||||||||
| 342 | push @prelude, | ||||||||||||||||||||||||||||
| 343 | ( | ||||||||||||||||||||||||||||
| 344 | 0 | map { $process_item->($_) } | |||||||||||||||||||||||||||
| 345 | split /,/, $_prelude_string | ||||||||||||||||||||||||||||
| 346 | ); | ||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | 2 | $self->_prelude( \@prelude ); | |||||||||||||||||||||||||||
| 349 | 2 | $self->_prelude_iter(0); | |||||||||||||||||||||||||||
| 350 | 2 | ▶ | if ( @{ $self->_prelude } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | { | ||||||||||||||||||||||||||||
| 352 | 0 | $self->_next_task; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | 2 | return; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | sub _next_task | ||||||||||||||||||||||||||||
| 358 | { | ||||||||||||||||||||||||||||
| 359 | 97 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_next_task: called CC=5 SCAR=20.9
| |||||||||||||||||||||||||||||
| 360 | 97 | ▶ | if ( $self->_prelude_iter < @{ $self->_prelude } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||
| 362 | 0 | my $alloc = $self->_prelude->[ $self->{_prelude_iter}++ ]; | |||||||||||||||||||||||||||
| 363 | 0 | my $task = $alloc->_task; | |||||||||||||||||||||||||||
| 364 | 0 | ▶ | if ( !@{ $task->_queue } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | { | ||||||||||||||||||||||||||||
| 366 | 0 | return $self->_next_task; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | 0 | $task->_remaining_iters( $alloc->_quota ); | |||||||||||||||||||||||||||
| 369 | 0 | $self->_active_task($task); | |||||||||||||||||||||||||||
| 370 | 0 | return 1; | |||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | 97 | my $tasks = $self->_tasks; | |||||||||||||||||||||||||||
| 373 | 97 | ▶ | return if !@$tasks; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | 97 | ▶ | if ( !@{ $tasks->[ $self->_task_idx ]->_queue } ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | { | ||||||||||||||||||||||||||||
| 376 | 0 | splice @$tasks, $self->_task_idx, 1; | |||||||||||||||||||||||||||
| 377 | 0 | return $self->_next_task; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | 97 | my $task = $tasks->[ $self->_task_idx ]; | |||||||||||||||||||||||||||
| 380 | 97 | $self->_task_idx( ( $self->_task_idx + 1 ) % @$tasks ); | |||||||||||||||||||||||||||
| 381 | 97 | $task->_remaining_iters(100); | |||||||||||||||||||||||||||
| 382 | 97 | $self->_active_task($task); | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 97 | return 1; | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | sub _get_next_state | ||||||||||||||||||||||||||||
| 388 | { | ||||||||||||||||||||||||||||
| 389 | 9589 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_next_state: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 9589 | return pop( @{ $self->_active_task->_queue } ); | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | sub _get_next_state_wrapper | ||||||||||||||||||||||||||||
| 395 | { | ||||||||||||||||||||||||||||
| 396 | 9589 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_next_state_wrapper: called CC=3 SCAR=11.6
| |||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 9589 | my $positions = $self->_positions; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 9589 | ▶ | while ( my $state = $self->_get_next_state ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | { | ||||||||||||||||||||||||||||
| 402 | 9589 | my $rec = $positions->{$state}; | |||||||||||||||||||||||||||
| 403 | 9589 | $self->_active_record($rec); | |||||||||||||||||||||||||||
| 404 | 9589 | ▶ | return $state if $rec->[2]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | 0 | return; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | sub _process_pending_items | ||||||||||||||||||||||||||||
| 410 | { | ||||||||||||||||||||||||||||
| 411 | 9587 | ▶ | my ( $self, $_pending, $state ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_process_pending_items: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 9587 | my $rec = $self->_active_record; | |||||||||||||||||||||||||||
| 414 | 9587 | my $task = $self->_active_task; | |||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 9587 | ▶ | if (@$_pending) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | { | ||||||||||||||||||||||||||||
| 418 | 6636 | ▶ | $self->_shuffle( $task->_gen, $_pending ) if $task->_seed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 6636 | push @{ $task->_queue }, map { $_->[0] } @$_pending; | |||||||||||||||||||||||||||
| 420 | 6636 | $rec->[3] += ( scalar grep { !$_->[1] } @$_pending ); | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | else | ||||||||||||||||||||||||||||
| 423 | { | ||||||||||||||||||||||||||||
| 424 | 2951 | my $parent = $state; | |||||||||||||||||||||||||||
| 425 | 2951 | my $parent_rec = $rec; | |||||||||||||||||||||||||||
| 426 | 2951 | my $positions = $self->_positions; | |||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | PARENT: | ||||||||||||||||||||||||||||
| 429 | 2951 | ▶ | while ( ( !$parent_rec->[3] ) or ( ! --$parent_rec->[3] ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$parent_rec->[3] and --$parent_rec->[3]
MC/DC100%2/2
$parent_rec->[3] and --$parent_rec->[3]
$parent_rec->[3]
--$parent_rec->[3]
Truth table100%3/3
A and B
A= $parent_rec->[3]B= --$parent_rec->[3]
| |||||||||||||||||||||||||||||
| 430 | { | ||||||||||||||||||||||||||||
| 431 | 9490 | $parent_rec->[2] = 0; | |||||||||||||||||||||||||||
| 432 | 9490 | $parent = $parent_rec->[0]; | |||||||||||||||||||||||||||
| 433 | 9490 | ▶ | last PARENT if not defined $parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 9490 | $parent_rec = $positions->{$parent}; | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | 9587 | ▶ | if ( not --$task->{_remaining_iters} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | { | ||||||||||||||||||||||||||||
| 439 | 95 | return $self->_next_task; | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | 9492 | return 1; | |||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | sub _find_moves | ||||||||||||||||||||||||||||
| 445 | { | ||||||||||||||||||||||||||||
| 446 | 9589 | ▶ | my ( $self, $_pending, $state, $no_cards ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_find_moves: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 447 | 9589 | my $board_values = $self->_board_values; | |||||||||||||||||||||||||||
| 448 | 9589 | my $fnd = vec( $state, 0, 8 ); | |||||||||||||||||||||||||||
| 449 | 9589 | my $positions = $self->_positions; | |||||||||||||||||||||||||||
| 450 | 9589 | my $_is_good_diff = $self->_is_good_diff; | |||||||||||||||||||||||||||
| 451 | 9589 | foreach my $col_idx ( keys @$board_values ) | |||||||||||||||||||||||||||
| 452 | { | ||||||||||||||||||||||||||||
| 453 | 153483 | my $pos = vec( $state, 4 + $col_idx, 4 ); | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 153483 | ▶ | if ($pos) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | { | ||||||||||||||||||||||||||||
| 457 | 95073 | $$no_cards = 0; | |||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | 95073 | my $card = $board_values->[$col_idx][ $pos - 1 ]; | |||||||||||||||||||||||||||
| 460 | 95073 | ▶ | if ( exists( $_is_good_diff->{ $card - $fnd } ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 461 | { | ||||||||||||||||||||||||||||
| 462 | 13429 | my $next_s = $state; | |||||||||||||||||||||||||||
| 463 | 13429 | vec( $next_s, 0, 8 ) = $card; | |||||||||||||||||||||||||||
| 464 | 13429 | --vec( $next_s, 4 + $col_idx, 4 ); | |||||||||||||||||||||||||||
| 465 | 13429 | my $exists = exists( $positions->{$next_s} ); | |||||||||||||||||||||||||||
| 466 | 13429 | my $to_add = 0; | |||||||||||||||||||||||||||
| 467 | 13429 | ▶ | if ( !$exists ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 468 | { | ||||||||||||||||||||||||||||
| 469 | 9084 | $positions->{$next_s} = [ $state, $col_idx, 1, 0 ]; | |||||||||||||||||||||||||||
| 470 | 9084 | $to_add = 1; | |||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | elsif ( $positions->{$next_s}->[2] ) | ||||||||||||||||||||||||||||
| 473 | { | ||||||||||||||||||||||||||||
| 474 | 0 | $to_add = 1; | |||||||||||||||||||||||||||
| 475 | } | ||||||||||||||||||||||||||||
| 476 | 13429 | ▶ | if ($to_add) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 477 | { | ||||||||||||||||||||||||||||
| 478 | 9084 | push( @$_pending, [ $next_s, $exists ] ); | |||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | 9589 | return; | |||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | sub _set_up_solver | ||||||||||||||||||||||||||||
| 488 | { | ||||||||||||||||||||||||||||
| 489 | 2 | ▶ | my ( $self, $talon_ptr, $diffs ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_up_solver: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||
| 491 | 2 | $self->_parse_board; | |||||||||||||||||||||||||||
| 492 | 2 | $self->_set_up_initial_position($talon_ptr); | |||||||||||||||||||||||||||
| 493 | 2 | $self->_set_up_tasks; | |||||||||||||||||||||||||||
| 494 | 2 | $self->_is_good_diff( +{ map { $_ => 1 } map { $_, -$_ } @$diffs, } ); | |||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | 2 | return; | |||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | package Games::Solitaire::BlackHole::Solver::App::Base::Task; | ||||||||||||||||||||||||||||
| 500 | 6 | $Games::Solitaire::BlackHole::Solver::App::Base::Task::VERSION = '0.4.1'; | |||||||||||||||||||||||||||
| 501 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | 6 | has [ '_queue', '_gen', '_task_idx', '_name', '_remaining_iters', '_seed', ] => | |||||||||||||||||||||||||||
| 504 | ( is => 'rw' ); | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | package Games::Solitaire::BlackHole::Solver::App::Base::PreludeItem; | ||||||||||||||||||||||||||||
| 507 | 6 | $Games::Solitaire::BlackHole::Solver::App::Base::PreludeItem::VERSION = '0.4.1'; | |||||||||||||||||||||||||||
| 508 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | 6 | has [ '_quota', '_task', '_task_idx', '_task_name', ] => ( is => 'rw' ); | |||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 6 | 1; | |||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | __END__ | ||||||||||||||||||||||||||||