Sha256: c7da645855899b323ce4809d8fdca99cb173358e4df19e6613f269915e6bb22d

Contents?: true

Size: 1.28 KB

Versions: 323

Compression:

Stored size: 1.28 KB

Contents

package Example;

use strict;
use warnings;

sub count_adjacent_bombs {
    my $board = shift;

    my @lines = split "\n", $board;

    # All input lines have same length
    my $length = length( $lines[0] );
    die "ArgumentError" if grep { length != $length } @lines;

    # The board consists of legal characters
    my @board = grep { /[-+| *]/ } map { split // } @lines;
    die "ArgumentError" if @board < @lines * $length;

    # The borders are sane
    /^\+\-+\+$/ or die "ArgumentError" for @lines[0, -1];
    /^\|.+\|$/  or die "ArgumentError" for @lines[1 .. -2];

    my $translate = sub { $_[0] * $length + $_[1] };

    my $count_bombs = sub {
        my($row, $col) = @_;

        $row -= 2;
        return grep { $board[$_] eq "*"              }
               map  { $translate->( @$_ )            } 
               map  { $row++; map { [$row, $_] } @$_ }
                      ( [$col-1, $col, $col+1] ) x 3;
    };

    foreach my $row ( 1 .. @lines-1 ) {
	foreach my $col ( 1 .. $length-1 ) {
	    my $idx = $translate->($row, $col);
	    next unless $board[$idx] eq " ";
	    $board[$idx] = $count_bombs->($row, $col) || " ";
	}
    }

    return( join "\n",
            map { join '', @board[$_ .. $_+$length-1] }
            map { $_ * $length } 0 .. @lines-1
	  ) . "\n";
}

1;

Version data entries

323 entries across 323 versions & 1 rubygems

Version Path
trackler-2.2.1.85 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.84 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.83 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.82 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.81 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.80 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.79 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.78 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.77 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.76 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.75 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.74 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.73 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.72 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.71 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.70 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.69 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.68 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.67 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.66 tracks/perl5/exercises/minesweeper/Example.pm