Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript扫雷器洪水填充算法无法正常工作_Javascript_Algorithm_Minesweeper - Fatal编程技术网

javascript扫雷器洪水填充算法无法正常工作

javascript扫雷器洪水填充算法无法正常工作,javascript,algorithm,minesweeper,Javascript,Algorithm,Minesweeper,我想写我的第一个简单的扫雷游戏。为了显示空字段,我编写了一个简单的泛洪填充算法,但它并没有正常工作。下面是一段代码: function reveal(a,b){ var fieldId = getFieldId(a,b); /* cells are stored in array fields[]. Each cell is an object {x: x,y: y, hasBomb: boolean, hasBeenDiscovered: boolean}. Function getFi

我想写我的第一个简单的扫雷游戏。为了显示空字段,我编写了一个简单的泛洪填充算法,但它并没有正常工作。下面是一段代码:

function reveal(a,b){
var fieldId = getFieldId(a,b);  

/*
cells are stored in array fields[]. Each cell is an object {x: x,y: y, hasBomb: boolean,
hasBeenDiscovered: boolean}. Function getFieldId returns array key for (x,y) cell.
*/

if(a < 0 || a > boardWidth-1){return}
if(b < 0 || b > boardHeight-1){return}

if(fields[fieldId].hasBeenDiscovered == true){return}

if(getNeighbourNumber(a,b) > 0){
    document.getElementById(a+'x'+b).innerHTML = getNeighbourNumber(a,b);
    document.getElementById(a+'x'+b).style.backgroundColor = 'white';
    document.getElementById(a+'x'+b).setAttribute('discovered',1);
    fields[fieldId].hasBeenDiscovered = true;
    return
}else if(getNeighbourNumber(a,b) == 0){
    document.getElementById(a+'x'+b).innerHTML = ' ';
    document.getElementById(a+'x'+b).style.backgroundColor = 'white';
    document.getElementById(a+'x'+b).setAttribute('discovered',1);  
    fields[fieldId].hasBeenDiscovered = true;


}

    reveal(a,b);
    console.log('0 ' + '0');
    reveal(a+1,b);
    console.log('+1' + ' ' + '0');
    reveal(a-1,b);
    console.log('-1 ' + '0');
    reveal(a,b+1);
    console.log('0 ' + '+1');
    reveal(a,b-1);
    console.log('0 ' + '-1');
    reveal(a-1,b-1);
    console.log('-1 ' + '-1');
    reveal(a-1,b+1);
    console.log('-1 ' + '+1');
    reveal(a+1,b+1);
    console.log('+1 ' + '+1');
    reveal(a+1,b-1);
    console.log('+1 ' + '-1');
    console.log('------------');

}
功能显示(a、b){
var fieldId=getFieldId(a,b);
/*
单元格存储在数组字段[]中。每个单元格都是一个对象{x:x,y:y,hasBomb:boolean,
hasBeenDiscovered:boolean}。函数getFieldId返回(x,y)单元格的数组键。
*/
如果(a<0 | | a>boardWidth-1){return}
如果(b<0 | | b>boardHeight-1){return}
if(fields[fieldId].hasBeenDiscovered==true){return}
如果(GetNeighborNumber(a,b)>0){
document.getElementById(a+'x'+b).innerHTML=getNeighborNumber(a,b);
document.getElementById(a+'x'+b).style.backgroundColor='white';
document.getElementById(a+'x'+b).setAttribute('discovered',1);
字段[fieldId]。hasBeenDiscovered=true;
返回
}else if(getNeighborNumber(a,b)==0){
getElementById(a+'x'+b).innerHTML='';
document.getElementById(a+'x'+b).style.backgroundColor='white';
document.getElementById(a+'x'+b).setAttribute('discovered',1);
字段[fieldId]。hasBeenDiscovered=true;
}
(a,b);
console.log('0'+'0');
揭示(a+1,b);
console.log('+1'+'+'0');
揭露(a-1,b);
log('-1'+'0');
分隔缝(a、b+1);
console.log('0'+'+1');
揭露(a,b-1);
console.log('0'+'-1');
分隔缝(a-1,b-1);
log('-1'+'-1');
分隔缝(a-1,b+1);
log('-1'+'+1');
分隔缝(a+1,b+1);
log('+1'+'+1');
分隔缝(a+1,b-1);
log('+1'+'-1');
console.log('-------------');
}
当发现北部、西北部和西部邻居有炸弹的空牢房时,即使其他邻居(南部、东南部、西部、东部、东北部)都是空的,洪水也只会显示出这些牢房。我是一个初学者,我不明白为什么这个代码不能完全工作。任何帮助都将不胜感激:)


编辑:控制台日志只是为了调试而放进去的。

我很高兴地说,您编写了一个很棒的代码,它非常容易阅读和理解代码

您只需要检查RevelField函数中的一个小问题,这里需要记住的一件事是,当您从属性中获取值时,它将始终以字符串形式提供给您,因此您需要将字符串解析为以十进制为基数的数字

function revealField(){
    var x = this.getAttribute('x');
    var y = this.getAttribute('y');
    x = parseInt(x, 10);
    y = parseInt(y, 10);

    var fieldId = getFieldId(x,y);

    if(fields[fieldId].hasBomb == true){
        document.getElementById(x+'x'+y).innerHTML = 'B';
        this.style.backgroundColor = 'brown';
        this.setAttribute('hasBomb', 1);
        removeEvents();
        alert('Bomba! Przegrales!');
    }else{
        this.style.backgroundColor = 'white';
        this.setAttribute('discovered',1);
        reveal(x,y);
        if(validateVictory() == true){
            removeEvents();
            alert('Brawo! Odkryles wszystkie bomby!');          
        }   
    }
}

这是你的最新版本

你会创建JSFIDLE吗?我的意思是当用户点击一个单元格时,发生了什么,应该发生什么?例如,您可以获取静态数据而不是炸弹,并可以解释我的问题,或者创建4x4或5x5矩阵,并尝试解释我从您的问题中了解到的是,假设此图像是一个字段->。。。。然后,如果我点击5,如果所有9个项目没有炸弹,那么它应该发生。。。目前只有1,2和4在开放,对吗?几乎是的。如果用户在您的cellgrid上单击了5。如果所有周围的单元在其附近有0枚炸弹,则所有单元周围的单元都会被显示,算法工作正常。当1、2、4号单元格附近有一个或多个炸弹时,问题就开始了(所以他们不应该完全暴露出来,而是应该暴露一个数字),算法只适用于这些单元格(所以1、2、4号单元格会出现),而不适用于3、6、7、8、9号单元格——即使他们附近没有炸弹,算法也应该暴露出来并进一步探索。对不起,我的英语很差,但这不是我的母语。谢谢你的好话。现在程序运行得非常好,谢谢!不过,我想澄清一下,我是否完全理解脚本以前为什么不工作——它只对选定单元格的左上角、左上角和上角的单元格工作,因为递归函数REVEL(a,b)中的这些单元格没有“+”运算符,只有“-”?对于参数为“+”的运算符函数,取两个字符串的和(因此启示(12+1,13)确实是启示(121,13),而不是启示(13,13))?而“-”运算符的情况并非如此?是的,每当你尝试添加+1时,它都是串联的,而不是添加。。。。