Chinaunix
标题:
扫雷递归出错
[打印本页]
作者:
atmystate
时间:
2009-04-25 20:06
标题:
扫雷递归出错
#这个函数是扫雷程序的一部分用于揭开隐藏的地雷,已经先行判断了该点不是地雷,
#先根据周围雷的数目>0显示相应的数字,标明已经掘开该处,如果是0个则递归周围八个,
#大多数情况下能够完成揭开任务,即使有大量的连续0区域,但有时0区域很少时也会出现
#This application has requested the Runtime to terminate it in an unusual way.
#Please contact the application's support team for more information.
#的错误信息,是递归出了问题吗?还是perl限制了递归的层数
sub unveil{
my ($m,$n)=@_; #点下的位置
my $count0;
my @shouldfind=();#应该递归的地方
my ($i,$j);
$count0=findcount($m,$n); #周围地雷的数目和
if(!$unveil[$m][$n]){ #此处未被掘开
$findcount++;
$unveil[$m][$n]=1;
if($textp[$m][$n]){$canv->delete($textp[$m][$n]);}
$canv->itemconfigure($button[$m*$row+$n],-fill=>'white');
if($count0){
$canv->create('text',$n*$width+$width/2,$m*$height+$height/2,
-text=>$count0,
-font=>$mw->fontCreate(-size=>$width>$height?$height
width),
-fill=>$colormap[$count0],-tag=>'font');
}
}
if($count0){}
else{
for $i(-1..1){
for $j(-1..1){
if($i+$m>=0 && $j+$n>=0 && $i+$mdelete($textp[$m][$n]);}
$canv->itemconfigure($button[($m+$i)*$row+$n+$j],-fill=>'white');
if($count0){
$canv->create('text',($n+$j)*$width+$width/2,
($m+$i)*$height+$height/2,-text=>$count0,
-font=>$mw->fontCreate(-size=>$width>$height?
$height
width),
-fill=>$colormap[$count0],-tag=>'font');
}
else{
push @shouldfind,[$m+$i,$n+$j];
}
}
}
}
for $i(0..$#shouldfind){
unveil(@{$shouldfind[$i]});#递归继续揭开
}
}
}
作者:
atmystate
时间:
2009-04-28 20:23
问题找到了,出在双重循环中这一块,
$canv->itemconfigure($button[($m+$i)*$row+$n+$j],-fill=>'white');
删除可以完成工作,
即便改成
$canv->itemconfigure($button[1],-fill=>'white');
@button是在Tk::Canvas中绘制的矩形,
仍会出现问题而在递归函数外面可以随意访问,
这就挺纳闷了
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2