3
我正在尝试在一个项目中重新创建扫雷工具,我已经尽可能地放置了地雷和数字。我似乎无法找出扩展点击瓦片并清除邻居的递归算法。有两个数组,_map保存背景+地雷&数字,_clickable地图保存位于背景顶部的瓦片。所以基本上,我要求的是帮助递归。如果这不够清楚,我会用必要的信息更新问题。扫雷清算算法
private function onClick(e:MouseEvent):void
{
trace("on Click");
var symbol = e.currentTarget;
var tempTileMask:TileMask = symbol;
var tempTile:Tile = _map[tempTileMask.yCoord][tempTileMask.xCoord]
if (tempTile.hasMine)
{
for (var i:int = _gridSize - 1; i >= 0; i--)
for (var j:int = _gridSize - 1; j >= 0; j--)
{
var temp:TileMask = _clickableMap[i][j];
removeChild(temp);
}
//explosion();
//gameOver();
}
if (tempTile.hasNumber)
{
removeChild(tempTileMask);
tempTile.cleared = true;
}
else
{
clearTiles(tempTile.xCoord, tempTile.yCoord);
}
}
这是我修改,以清除地雷
function clearTiles(x:int, y:int)
{
// Get an object that contains the tile information for the location
// we are checking
if(_map[y][x] != null)
{
var tempTile:Tile = _map[y][x];
var tempTileMask:TileMask = _clickableMap[y][x];
// Check if the location we are checking is out of the bounds of the
// playable area
trace(tempTile);
if (tempTile.outOfBounds(x, y))
{
return;
}
// If the tile has already been revealed then there is nothing to do
trace("before clearing");
if (tempTile.cleared)
{
trace("clearing tile");
return;
}
// If the tile hasn't been revealed and it's an empty square we
// reveal the location then call this function again for each
// surrounding block
trace("before check for surrounding tiles");
if (tempTile.hasNumber != true && tempTile.hasMine != true)
{
trace("check for surrounding tiles");
// Remove the mask hiding the tiles property
removeChild(tempTileMask);
// Set the tile as cleared
tempTile.cleared = true;
if(_map[tempTile.yCoord][tempTile.xCoord - 1] != null)
{
var tile1:Tile =_map[tempTile.yCoord][tempTile.xCoord - 1]
if(!tile1.cleared)
clearTiles(tempTile.xCoord - 1 , tempTile.yCoord); //Check tile to the left
}
if(_map[tempTile.yCoord][tempTile.xCoord + 1] != null)
{
var tile2:Tile =_map[tempTile.yCoord][tempTile.xCoord + 1]
if(!tile2.cleared)
clearTiles(tempTile.xCoord + 1 , tempTile.yCoord); //Check tile to the left
}
if(_map[tempTile.yCoord - 1][tempTile.xCoord] != null)
{
var tile3:Tile =_map[tempTile.yCoord - 1][tempTile.xCoord]
if(!tile3.cleared)
clearTiles(tempTile.xCoord, tempTile.yCoord - 1); //Check tile to the left
}
if(_map[tempTile.yCoord + 1][tempTile.xCoord] != null)
{
var tile4:Tile =_map[tempTile.yCoord + 1][tempTile.xCoord]
if(!tile4.cleared)
clearTiles(tempTile.xCoord, tempTile.yCoord + 1); //Check tile to the left
}
if(_map[tempTile.yCoord - 1][tempTile.xCoord - 1] != null)
{
var tile5:Tile =_map[tempTile.yCoord - 1][tempTile.xCoord - 1]
if(!tile5.cleared)
clearTiles(tempTile.xCoord - 1, tempTile.yCoord - 1); //Check tile to the left
}
if(_map[tempTile.yCoord + 1][tempTile.xCoord + 1] != null)
{
var tile6:Tile =_map[tempTile.yCoord + 1][tempTile.xCoord + 1]
if(!tile6.cleared)
clearTiles(tempTile.xCoord + 1, tempTile.yCoord + 1); //Check tile to the left
}
if(_map[tempTile.yCoord - 1][tempTile.xCoord + 1] != null)
{
var tile7:Tile =_map[tempTile.yCoord - 1][tempTile.xCoord + 1]
if(!tile7.cleared)
clearTiles(tempTile.xCoord + 1, tempTile.yCoord - 1); //Check tile to the left
}
if(_map[tempTile.yCoord + 1][tempTile.xCoord - 1] != null)
{
var tile8:Tile =_map[tempTile.yCoord + 1][tempTile.xCoord - 1]
if(!tile8.cleared)
clearTiles(tempTile.xCoord - 1, tempTile.yCoord + 1); //Check tile to the left
}
}
}
else
return;
}
你是什么意思由tempTile.outOfBounds()?这是我需要编写的函数,还是闪存的一部分,我不知道检查数组的边界? – user3523222
我只是添加了一个编辑来帮助澄清。我有很多崩溃的经验,因为我引用了超出数组边界的数组元素。所以在处理数组时,我现在要做的第一件事就是确保我没有引用不存在的东西。我给你的代码绝对不能复制粘贴,但希望你看到我是如何构建递归函数的,你可以传递你认为合适的任何参数。 –
我与您的代码存在问题,它将清除一些相邻的地块,既没有地雷也没有数字,但不是所有相邻的地块 – user3523222