免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1781 | 回复: 1
打印 上一主题 下一主题

扫雷递归出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 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?$heightwidth),
               -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?
                        $heightwidth),
                        -fill=>$colormap[$count0],-tag=>'font');
                  }
                  else{
                    push @shouldfind,[$m+$i,$n+$j];
                  }
              }
          }
      }
      for $i(0..$#shouldfind){
          unveil(@{$shouldfind[$i]});#递归继续揭开
      }
  }   
}

论坛徽章:
0
2 [报告]
发表于 2009-04-28 20:23 |只看该作者
问题找到了,出在双重循环中这一块,
$canv->itemconfigure($button[($m+$i)*$row+$n+$j],-fill=>'white');
删除可以完成工作,
即便改成
$canv->itemconfigure($button[1],-fill=>'white');
@button是在Tk::Canvas中绘制的矩形,
仍会出现问题而在递归函数外面可以随意访问,
这就挺纳闷了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP