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.65 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.64 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.63 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.62 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.61 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.60 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.59 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.58 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.57 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.56 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.55 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.54 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.53 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.52 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.51 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.50 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.49 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.48 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.47 tracks/perl5/exercises/minesweeper/Example.pm
trackler-2.2.1.46 tracks/perl5/exercises/minesweeper/Example.pm