| 1 | package Games::LMSolve::Numbers; | ||||||||||
| 2 | 3 | $Games::LMSolve::Numbers::VERSION = '0.14.0'; | |||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | ▶ | use Games::LMSolve::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use vars qw(@ISA); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 3 | @ISA = qw(Games::LMSolve::Base); | |||||||||
| 11 | |||||||||||
| 12 | 3 | my %cell_dirs = ( | |||||||||
| 13 | 'N' => [ 0, -1 ], | ||||||||||
| 14 | 'S' => [ 0, 1 ], | ||||||||||
| 15 | 'E' => [ 1, 0 ], | ||||||||||
| 16 | 'W' => [ -1, 0 ], | ||||||||||
| 17 | ); | ||||||||||
| 18 | |||||||||||
| 19 | sub input_board | ||||||||||
| 20 | { | ||||||||||
| 21 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
input_board: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 22 | |||||||||||
| 23 | 0 | my $filename = shift; | |||||||||
| 24 | |||||||||||
| 25 | 0 | my $spec = { | |||||||||
| 26 | 'dims' => { 'type' => "xy(integer)", 'required' => 1 }, | ||||||||||
| 27 | 'start' => { 'type' => "xy(integer)", 'required' => 1 }, | ||||||||||
| 28 | 'layout' => { 'type' => "layout", 'required' => 1 }, | ||||||||||
| 29 | }; | ||||||||||
| 30 | |||||||||||
| 31 | 0 | my $input_obj = Games::LMSolve::Input->new(); | |||||||||
| 32 | 0 | my $input_fields = $input_obj->input_board( $filename, $spec ); | |||||||||
| 33 | my ( $width, $height ) = | ||||||||||
| 34 | 0 | @{ $input_fields->{'dims'}->{'value'} }{ 'x', 'y' }; | |||||||||
| 35 | my ( $start_x, $start_y ) = | ||||||||||
| 36 | 0 | @{ $input_fields->{'start'}->{'value'} }{ 'x', 'y' }; | |||||||||
| 37 | 0 | my (@board); | |||||||||
| 38 | |||||||||||
| 39 | my $line; | ||||||||||
| 40 | 0 | my $line_number = 0; | |||||||||
| 41 | 0 | my $lines_ref = $input_fields->{'layout'}->{'value'}; | |||||||||
| 42 | |||||||||||
| 43 | my $read_line = sub { | ||||||||||
| 44 | 0 | ▶ | if ( scalar(@$lines_ref) == $line_number ) | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||
| 45 | { | ||||||||||
| 46 | 0 | return 0; | |||||||||
| 47 | } | ||||||||||
| 48 | 0 | $line = $lines_ref->[$line_number]; | |||||||||
| 49 | 0 | $line_number++; | |||||||||
| 50 | 0 | return 1; | |||||||||
| 51 | 0 | }; | |||||||||
| 52 | |||||||||||
| 53 | my $gen_exception = sub { | ||||||||||
| 54 | 0 | ▶ | my $text = shift; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 55 | die "$text on $filename at line " | ||||||||||
| 56 | 0 | . ( $input_fields->{'layout'}->{'line_num'} + $line_number + 1 ) | |||||||||
| 57 | . "!\n"; | ||||||||||
| 58 | 0 | }; | |||||||||
| 59 | |||||||||||
| 60 | 0 | my $y = 0; | |||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | INPUT_LOOP: while ( $read_line->() ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | { | ||||||||||
| 64 | 0 | ▶ | if ( length($line) != $width ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | { | ||||||||||
| 66 | 0 | $gen_exception->("Incorrect number of cells"); | |||||||||
| 67 | } | ||||||||||
| 68 | 0 | ▶ | if ( $line =~ /([^\d\*])/ ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | { | ||||||||||
| 70 | 0 | $gen_exception->("Unknown cell type $1"); | |||||||||
| 71 | } | ||||||||||
| 72 | 0 | push @board, [ split( //, $line ) ]; | |||||||||
| 73 | 0 | $y++; | |||||||||
| 74 | 0 | ▶ | if ( $y == $height ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | { | ||||||||||
| 76 | 0 | last; | |||||||||
| 77 | } | ||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 0 | ▶ | if ( $y != $height ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | { | ||||||||||
| 82 | 0 | $gen_exception->("Input terminated prematurely after reading $y lines"); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | if ( !defined($start_x) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | { | ||||||||||
| 87 | 0 | $gen_exception->("The starting position was not defined anywhere"); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | $self->{'height'} = $height; | |||||||||
| 91 | 0 | $self->{'width'} = $width; | |||||||||
| 92 | 0 | $self->{'board'} = \@board; | |||||||||
| 93 | |||||||||||
| 94 | 0 | return [ $start_x, $start_y ]; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | # A function that accepts the expanded state (as an array ref) | ||||||||||
| 98 | # and returns an atom that represents it. | ||||||||||
| 99 | sub pack_state | ||||||||||
| 100 | { | ||||||||||
| 101 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
pack_state: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 102 | 0 | my $state_vector = shift; | |||||||||
| 103 | 0 | return pack( "cc", @{$state_vector} ); | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | # A function that accepts an atom that represents a state | ||||||||||
| 107 | # and returns an array ref that represents it. | ||||||||||
| 108 | sub unpack_state | ||||||||||
| 109 | { | ||||||||||
| 110 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
unpack_state: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 111 | 0 | my $state = shift; | |||||||||
| 112 | 0 | return [ unpack( "cc", $state ) ]; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | # Accept an atom that represents a state and output a | ||||||||||
| 116 | # user-readable string that describes it. | ||||||||||
| 117 | sub display_state | ||||||||||
| 118 | { | ||||||||||
| 119 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
display_state: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 120 | 0 | my $state = shift; | |||||||||
| 121 | 0 | my ( $x, $y ) = @{ $self->unpack_state($state) }; | |||||||||
| 122 | 0 | return sprintf( "X = %i ; Y = %i", $x + 1, $y + 1 ); | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | sub check_if_final_state | ||||||||||
| 126 | { | ||||||||||
| 127 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
check_if_final_state: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 128 | |||||||||||
| 129 | 0 | my $coords = shift; | |||||||||
| 130 | 0 | return $self->{'board'}->[ $coords->[1] ][ $coords->[0] ] eq "*"; | |||||||||
| 131 | } | ||||||||||
| 132 | |||||||||||
| 133 | # This function enumerates the moves accessible to the state. | ||||||||||
| 134 | # If it returns a move, it still does not mean that it is a valid | ||||||||||
| 135 | # one. I.e: it is possible that it is illegal to perform it. | ||||||||||
| 136 | sub enumerate_moves | ||||||||||
| 137 | { | ||||||||||
| 138 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
enumerate_moves: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||
| 139 | |||||||||||
| 140 | 0 | my $coords = shift; | |||||||||
| 141 | |||||||||||
| 142 | 0 | my $x = $coords->[0]; | |||||||||
| 143 | 0 | my $y = $coords->[1]; | |||||||||
| 144 | |||||||||||
| 145 | 0 | my $step = $self->{'board'}->[$y][$x]; | |||||||||
| 146 | |||||||||||
| 147 | 0 | my @moves; | |||||||||
| 148 | |||||||||||
| 149 | 0 | ▶ | if ( $x + $step < $self->{'width'} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | { | ||||||||||
| 151 | 0 | push @moves, "E"; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | # The ranges are [0 .. ($width-1)] and [0 .. ($height-1)] | ||||||||||
| 155 | 0 | ▶ | if ( $x - $step >= 0 ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | { | ||||||||||
| 157 | 0 | push @moves, "W"; | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | 0 | ▶ | if ( $y + $step < $self->{'height'} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | { | ||||||||||
| 162 | 0 | push @moves, "S"; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | 0 | ▶ | if ( $y - $step >= 0 ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | { | ||||||||||
| 167 | 0 | push @moves, "N"; | |||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | 0 | return @moves; | |||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | # This function accepts a state and a move. It tries to perform the | ||||||||||
| 174 | # move on the state. If it is succesful, it returns the new state. | ||||||||||
| 175 | # | ||||||||||
| 176 | # Else, it returns undef to indicate that the move is not possible. | ||||||||||
| 177 | sub perform_move | ||||||||||
| 178 | { | ||||||||||
| 179 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
perform_move: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 180 | |||||||||||
| 181 | 0 | my $coords = shift; | |||||||||
| 182 | 0 | my $m = shift; | |||||||||
| 183 | |||||||||||
| 184 | 0 | my $step = $self->{'board'}->[ $coords->[1] ][ $coords->[0] ]; | |||||||||
| 185 | |||||||||||
| 186 | 0 | my $offsets = [ map { $_ * $step } @{ $cell_dirs{$m} } ]; | |||||||||
| 187 | 0 | my @new_coords = @$coords; | |||||||||
| 188 | 0 | $new_coords[0] += $offsets->[0]; | |||||||||
| 189 | 0 | $new_coords[1] += $offsets->[1]; | |||||||||
| 190 | |||||||||||
| 191 | 0 | return [@new_coords]; | |||||||||
| 192 | } | ||||||||||
| 193 | |||||||||||
| 194 | 3 | 1; | |||||||||
| 195 | |||||||||||
| 196 | __END__ | ||||||||||