Games-Go-Sgf2Dg
view release on metacpan or search on metacpan
lib/Games/Go/Sgf2Dg.pm view on Meta::CPAN
my @hoshi;
push(@hoshi, Coords2SGF($a,$c), Coords2SGF($c,$a)) if ($num >= 2);
push(@hoshi, Coords2SGF($c,$c)) if ($num >= 3);
push(@hoshi, Coords2SGF($a,$a)) if ($num >= 4);
push(@hoshi, Coords2SGF($b,$b)) if (($num == 5) or
($num == 7) or
($num == 9));
push(@hoshi, Coords2SGF($a,$b), Coords2SGF($c,$b)) if ($num >= 6);
push(@hoshi, Coords2SGF($b,$a), Coords2SGF($b,$c)) if ($num >= 8);
if (($num > 9) or ($num < 2)) {
print(STDERR "Handicap is $num - I can only handle 2 through 9.\n");
}
return \@hoshi;
}
sub CheckForDeadGroups {
my ($x, $y) = @_;
my $color = $diagram->game_stone(Coords2SGF($x, $y));
return unless(defined($color));
my $otherColor = ($color eq 'black') ? 'white' : 'black';
CheckIfDead($x + 1, $y, $otherColor); # first check the four neighboring stones of the other color
CheckIfDead($x - 1, $y, $otherColor);
CheckIfDead($x, $y + 1, $otherColor);
CheckIfDead($x, $y - 1, $otherColor);
CheckIfDead($x, $y, $color); # and finally we need to check the stone just placed
}
sub CheckIfDead {
my ($x, $y, $color) = @_;
my $stone = $diagram->game_stone(Coords2SGF($x, $y));
return unless(defined($stone) and # no stone/group here to check
($stone eq $color)); # color doesn't match
unless (HasLibs($x, $y, $color, {}, 0)) {
RemoveGroup($x, $y, $color); # no liberties? - it's dead!
}
}
sub HasLibs {
my ($x, $y, $color, $been_here, $depth) = @_;
if ($depth > 1000) {
die("Oops, recursion > 1000 while checking for liberties at move $moveNum coords ($x,$y)\n" .
"This isn't supposed to be possible! Aborting...\n");
}
if (($x < 1) or ($x > ($option{boardSizeX})) or ($y < 1) or ($y > ($option{boardSizeY}))) {
return(0); # oops! off the board.
}
return 0 if (exists($been_here->{"$x,$y"})); # we've been here before
$been_here->{"$x,$y"} = 1; # mark that we've been here
my $thisStone = $diagram->game_stone(Coords2SGF($x, $y));
return 1 unless(defined($thisStone)); # empty, the group has liberties
return(0) if ($thisStone ne $color); # this is an opponents stone - no liberties here!
# this is a connected stone of the same color
$depth++;
if (HasLibs($x + 1, $y, $color, $been_here, $depth) or
HasLibs($x - 1, $y, $color, $been_here, $depth) or
HasLibs($x, $y + 1, $color, $been_here, $depth) or
HasLibs($x, $y - 1, $color, $been_here, $depth)) {
return(1); # yes! we're alive!
}
return(0); # uh-oh! no liberties yet...
}
sub RemoveGroup {
my ($x, $y, $color) = @_;
my $thisStone = $diagram->game_stone(Coords2SGF($x, $y));
if (defined($thisStone) and
($thisStone eq $color)) {
$diagram->capture(Coords2SGF($x, $y));
RemoveGroup($x + 1, $y, $color); # remove any connected stones of the same color
RemoveGroup($x - 1, $y, $color);
RemoveGroup($x, $y + 1, $color);
RemoveGroup($x, $y - 1, $color);
}
}
sub finishDiagram {
my ($d, $cause) = @_;
$d = $diagram unless(defined($d));
return unless ($d->actions_done); # no new actions pending? just use current diagram
if (exists($d->user->{mainId})) {
printVerbose("Finish Diagram ", $d->user->{mainId}, " at move $moveNum due to $cause\n");
} else {
printVerbose("Finish Variation at move $moveNum due to $cause\n");
}
my $prevDiagram = $d;
$diagram = $d->next; # start a fresh diagram
$diagram->user({id => $diagramId++}); # init user hash
if (exists($prevDiagram->user->{mainId})) {
my $mainId = $prevDiagram->user->{mainId} + 1;
$diagram->user->{mainId} = $mainId;
printIndent("Parsing Diagram $mainId at move $moveNum\n");
} else {
printIndent("Parsing Variation continuation at move $moveNum\n");
}
$prevDiagram->user->{next} = $diagram; # link from previous to new diagram
if ($option{repeatLast} and
defined($lastMove2[0])) {
$diagram->renumber($lastMove2[0], $lastMove2[1], undef, $lastMove2[2]);
}
}
sub CompareVariation {
my @aa = split(/\./, $a);
my @bb = split(/\./, $b);
my ($ii, $max, $return);
#print "CompareVariation($a, $b)\n";
if (@aa > @bb) {
$max = @aa;
} else {
$max = @bb;
}
for ($ii = 0; $ii < $max; $ii++) {
$aa[$ii] = -1 unless(defined($aa[$ii]));
$bb[$ii] = -1 unless(defined($bb[$ii]));
( run in 2.747 seconds using v1.01-cache-2.11-cpan-14f38c9f855 )