Games-LMSolve
view release on metacpan or search on metacpan
lib/Games/LMSolve/Plank/Base.pm view on Meta::CPAN
my @planks;
my $get_plank = sub {
my $p = shift;
my ( $start_x, $start_y ) =
( $p->{'start'}->{'x'}, $p->{'start'}->{'y'} );
my ( $end_x, $end_y ) = ( $p->{'end'}->{'x'}, $p->{'end'}->{'y'} );
my $check_endpoints = sub {
if ( !$board[$start_y]->[$start_x] )
{
die "Plank cannot be placed at point ($start_x,$start_y)!";
}
if ( !$board[$end_y]->[$end_x] )
{
die "Plank cannot be placed at point ($end_x,$end_y)!";
}
};
lib/Games/LMSolve/Plank/Base.pm view on Meta::CPAN
{
die "$plank_str is out of the boundaries of the board";
}
if ( $start_x == $end_x )
{
if ( $start_y == $end_y )
{
die "$plank_str has zero length!";
}
$check_endpoints->();
if ( $start_y > $end_y )
{
( $start_y, $end_y ) = ( $end_y, $start_y );
}
foreach my $y ( ( $start_y + 1 ) .. ( $end_y - 1 ) )
{
if ( $board[$y]->[$start_x] )
{
die "$plank_str crosses logs!";
}
}
return {
'len' => ( $end_y - $start_y ),
'start' => { 'x' => $start_x, 'y' => $start_y },
'dir' => "S"
};
}
elsif ( $start_y == $end_y )
{
$check_endpoints->();
if ( $start_x > $end_x )
{
( $start_x, $end_x ) = ( $end_x, $start_x );
}
foreach my $x ( ( $start_x + 1 ) .. ( $end_x - 1 ) )
{
if ( $board[$start_y]->[$x] )
{
die "$plank_str crosses logs!";
}
}
return {
'len' => ( $end_x - $start_x ),
'start' => { 'x' => $start_x, 'y' => $start_y },
'dir' => "E"
};
}
elsif ( ( $end_x - $start_x ) == ( $end_y - $start_y ) )
{
$check_endpoints->();
if ( $start_x > $end_x )
{
( $start_x, $end_x ) = ( $end_x, $start_x );
( $start_y, $end_y ) = ( $end_y, $start_y );
}
foreach my $i ( 1 .. ( $end_x - $start_x - 1 ) )
{
if ( $board[ $start_y + $i ]->[ $start_x + $i ] )
{
die "$plank_str crosses logs!";
( run in 0.920 second using v1.01-cache-2.11-cpan-2b1a40005be )