Gunnar said:
If you make it easy to help, I'm sure someone can.
Please post a short but _complete_ program that people can copy and run
and that exhibits the problem you're having!
Sorry I didn't want to overload with code:
Here's the complete code
Regards Daniel
#!/usr/bin/perl
#
#################################################################################
#
# detect_eyes.pl : perl program to find the double eyes in a go game
#
# Filename : detect_eyes.pl
#
# Version : 1.0
#
# Author : D. Jacober, DSAI 2001 8. Semester KI
# Date : 05.07.2005
# Changes :
#
# Input : Text file on the command line with a go board
# Output : Displays double eyes on the go board
#
#################################################################################
use strict;
use warnings;
use Time::localtime;
use IO::Handle;
###use Sort::Fields;
# Variablen deklarieren
my $infile = shift || "example.txt";
my $outfile = shift || "double_eyes.txt";
my %board = ();
my %white = ();
my %black = ();
my %empty = ();
my @open = ();
my @closed = ();
my @current = ();
my @board_number = (1,2,3,4,5);
my @board_alpha = ("A","B","C","D","E");
my $max_num = 5;
my $min_alpha = "A";
my $max_alpha = "F";
#################################################################################
# sub to read input file
#################################################################################
sub has_neighbour {
my ($line,$alpha) = @_;
my $value = $white{$line}{$alpha};
my $next_alpha=chr(ord($alpha)+1);
my $before_alpha=chr(ord($alpha)-1);
my $has_neighbour = 0;
my @new_member = ();
LINE: foreach my $num ($line-1,$line,$line+1) {
next LINE if ( ($num < 1) || ($num > $max_num) );
ALPHA: foreach my $char ($before_alpha,$alpha,$next_alpha) {
next ALPHA if ( ($num == $line) && ($alpha eq $char) );
next ALPHA if ( ($char lt $min_alpha) || ($char gt $max_alpha) );
if ( exists($white{$num}{$char}) && $value eq $white{$num}{$char}) {
foreach my $in_open (@open) {
next LINE if ("$num $char" eq $in_open);
}
foreach my $in_current (@current) {
next LINE if ("$num $char" eq $in_current);
}
print "inserting $num $char in open\n";
$new_member[$has_neighbour++] = "$num $char";
}
}
}
push(@new_member, @open);
@open = @new_member;
return $has_neighbour;
}
sub read_board {
my @alpha_coordinate;
open(INFILE,$infile) or die " cannot open $infile ";
while(<INFILE>) {
s/^\s//g;
chomp;
if (s/^\///) {
s/^\s//g;
@alpha_coordinate = split(/ /);
} elsif (s/^(\d+)//) {
my $line_no = $1;
s/^\s//g;
my @line = split / /;
my $n = 0;
foreach my $field (@line) {
#full board with all figures
$board{$line_no}{$alpha_coordinate[$n]} = $field;
#separate hashes for each color type (black, white and empty)
if ($field =~ /0/) {
$white{$line_no}{$alpha_coordinate[$n]} = $field;
} elsif ($field =~ /X/) {
$black{$line_no}{$alpha_coordinate[$n]} = $field;
} else {
$empty{$line_no}{$alpha_coordinate[$n]} = $field;
}
$n++;
}
} else { die "wrong input line \n"; }
}
close(INFILE);
}
sub detect_eye {
my $ref_color = @_;
foreach my $line (@board_number) {
VALUE: foreach my $alpha (@board_alpha) {
print "$line,$alpha : $ref_color->{$line}{$alpha} \n";
if (exists($white{$line}{$alpha}) ) {
if (has_neighbour($line,$alpha) > 1) {unshift(@current, "$line
$alpha");}
while ( $#open != -1 ) {
print "@open \n";
print "@current \n";
my ($num, $char) = split(/ /,shift(@open));
if (has_neighbour($num,$char) ) {unshift(@current, "$num
$char");}
}
exit;
}
}
}
}
sub main {
read_board();
detect_eye(\%white);
}
main();