Created
March 22, 2011 21:10
-
-
Save moritz/882070 to your computer and use it in GitHub Desktop.
code kata minesweeper http://codingdojo.org/cgi-bin/wiki.pl?KataMinesweeper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
sub transformed_field(@field) { | |
my %transform = ('*' => '*', '.' => 0); | |
my @t = @field.map: { [ .map: { %transform{$_}} ] }; | |
for @t.keys X @t[0].keys -> $x, $y { | |
next unless @t[$x][$y] eq '*'; | |
for ($x - 1 max 0) .. ($x + 1 min @t.end) | |
X ($y - 1 max 0) .. ($y + 1 min @t[0].end) -> $xa, $ya { | |
@t[$xa][$ya]++ if @t[$xa][$ya] ne '*'; | |
} | |
} | |
@t>>.join.join("\n"); | |
} | |
sub MAIN { | |
my $count = 1; | |
loop { | |
my ($rows, $cols) = get.words; | |
last if $rows == 0 && $cols == 0; | |
my @field = ([get.comb] for ^$rows); | |
say '' if $count != 1; | |
say "Field #$count:"; | |
$count++; | |
say transformed_field @field; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment