forked from boonth/minesweeper-kata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minesweeper.pl
executable file
·61 lines (57 loc) · 1.48 KB
/
minesweeper.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl -w
use strict;
my $c = 1;
while(<STDIN>){
#regex matches only pos. int
die "Wrong input: expect two ints got $_" unless /(\d+)\s+(\d+)\s*/;
my $lines=$1;
my $rows=$2;
if ($lines == 0 && $rows == 0){
exit 0;
}
die "Wrong input: expect two pos ints or 0 0 got $_" unless ($lines > 0 && $rows > 0);
die "Wrong input: expect two pos ints smaller 100 got $_" unless ($lines < 101 && $rows < 101);
my @field;
my @output;
for (my $k=0;$k<$lines;$k++){
$_=<STDIN>;
die "Wrong input: needed * . or space got $_" unless /[*. ]*/;
my @field2=split(/\s+/);
die "Wrong input: expected $rows fields, got ",$#field2+1," fields in $_" unless ($#field2+1 == $rows);
$field[$k]=\@field2;
for (my $l=0;$l<$rows;$l++){
$output[$k+1][$l+1]=0;
}
}
for (my $k=0;$k<$lines;$k++){
for (my $l=0;$l<$rows;$l++){
if ($field[$k][$l] eq "*"){
#loop over nbs, self doesn't matter, overwritten below
for (my $i=0;$i<3;$i++){
for (my $j=0;$j<3;$j++){
$output[$k+$i][$l+$j]++;
}
}
} elsif ($field[$k][$l] ne ".") {
die "Wrong input: expected * or ., got $field[$k][$l]";
}
}
}
#Override mines
for (my $k=0;$k<$lines;$k++){
for (my $l=0;$l<$rows;$l++){
if ($field[$k][$l] eq "*"){
$output[$k+1][$l+1]="*";
}
}
}
#output
print "Field #",$c++,":\n";
for (my $k=0;$k<$lines;$k++){
for (my $l=0;$l<$rows;$l++){
print "$output[$k+1][$l+1] ";
}
print "\n";
}
print "\n";
}