编程语言
首页 > 编程语言> > javascript扫雷放置不必要的“1”

javascript扫雷放置不必要的“1”

作者:互联网

我写在JavaScript扫雷艇这是随机的1米跑(我正在试图改善造型),它给了我这个工作的罚款一会儿,然后:

注意右上角的“1”以及下方的2个缺失1和2的三个空格

这是我在广场中添加数字的功能:

function nextToBombCheck(event) {   
    //reset bomb count
bombCount = 0 ;
    //initialize variable for checking nerby boxes
var nextToBox = 0;
    //asign the box's id as a number
var boxNum = parseInt(event.id);

var checkSide = 0;

for ( var i = 9 ; i <= 11 ; i++ ) {
    nextToBox = boxNum + i;
        //check if its a wrap
    if ( ( nextToBox%10 === 0 && boxNum%10 === 9 ) || ( nextToBox%10 === 9 && boxNum%10 === 0 ) ) {
        continue;
        //check boxes below
    } else if ( bomb.indexOf( nextToBox ) >= 0 ) {
        bombCount++;
    }
}

for ( i = -1 ; i <= 1 ; i++ ) {
    nextToBox = boxNum + i;
        //check if its a wrap (above and below wont work anyway)
    if ( ( nextToBox%10 === 0 && boxNum%10 === 9 ) || ( nextToBox%10 === 9 && boxNum%10 === 0 ) ) {
        continue;
        //check boxes alongside
    } else if ( bomb.indexOf( nextToBox ) >= 0 ) {
        bombCount++;
    }
}

for ( i = -11 ; i <= -9 ; i++ ) {
    nextToBox = boxNum + i;
    if ( ( nextToBox%10 === 0 && boxNum%10 === 9 ) || ( nextToBox%10 === 9 && boxNum%10 === 0 ) ) {
        continue;
        //check boxes above
    } else if ( bomb.indexOf( nextToBox ) >= 0 ) {
        bombCount++;
    }
}
        //set class(colors) based on bombCount
    event.className = classList[ bombCount ];
if ( bombCount !== 0 ) {
        //write number of neighboring bombs
    event.innerHTML = bombCount;
}
}

我的程序使用一个表,每个td的id为0-99

heres a link if that helps

解决方法:

不错的游戏.但是您提交了计算最后一个索引的常见错误.你看到你的桌子大小为11×11 = 121吗?但是在你的程序中你使用

var rowAmount = 10;
var columnAmount = 10;

cellAmount = columnAmount * rowAmount;

这是错的. for循环还明确假设有11列:

for ( i = 0 ; i <= rowAmount ; i++ ) {
    gameBox += "<tr>";
    for ( var j = 0 ; j <= columnAmount ; j++ ) {
        var idValue = i * 10 + j;
        gameBox += "<td class = 'box' id = '" + idValue + "' onclick = 'process(this);' ></td>";    }
    gameBox += "</tr>";
}

但是idValue正在使用10列.这意味着您的程序将忽略最后一列.改变你所有的代码,你会没事的.

标签:javascript,minesweeper
来源: https://codeday.me/bug/20190629/1325817.html