简体   繁体   English

我的布尔人不工作吗?

[英]My Bools Aren't working?

I am trying to make a "Battleship" like game. 我正在尝试制作类似游戏的“战舰”。 Most of my board is supposed to be "missed" tiles, and only a few are supposed to be tiles with submarines on them for a "hit". 我董事会的大部分应该是“遗漏”的瓷砖,只有少数是上面带有潜水艇的瓷砖以“击中”。 The problem is that whenever I run my program, I cannot tell if it is ignoring my bool or if it isn't understanding what I coded, because everything I click is a "hit". 问题是,每当我运行程序时,我都无法确定它是在忽略我的bool还是在理解代码,因为单击的所有内容都是“命中”。

var cellClick=function(clicked)
{
    var cell=clicked.target;

    if(! cell.isEmpty) return;

    if(cell.isSub=false)
        {

            cell.style.backgroundImage='url("missed.png")';
            cell.style.backgroundSize='constrain';


        }
    else
        {
            cell.style.backgroundImage='url("hit.png")';
            cell.style.backgroundSize='constrain';
            cell.exploded=true;

        }

    cell.isEmpty=false;
    console.log('click');

};

var waterCell=[[],[],[],[],[],[],[],[],[],[]];

for(var row=0;row<10;row++)
    {
        for(var column=0;column<10;column++)
            {
                waterCell[row][column]=document.createElement("div");
                waterCell[row][column].style.width='10%';
                waterCell[row][column].style.height='10%';
                waterCell[row][column].style.position='absolute';
                waterCell[row][column].style.left=(column*10)+'%';
                waterCell[row][column].style.top=(row*10)+'%';
                waterCell[row][column].style.backgroundImage='url("water_cell.jpg")';
                waterCell[row][column].style.backgroundSize='contain';
                gameBoard.appendChild(waterCell[row][column]);

                waterCell[row][column].row=row;
                waterCell[row][column].column=column;
                waterCell[row][column].isEmpty=true;
                waterCell[row][column].isSub=false;
                waterCell[row][column].exploded=false;


            }
    }
//trying to make random subs
for(var i=0;i<5;i++)
    {
        row=Math.round(Math.random()*10);
        column=Math.round(Math.random()*10);
        waterCell[row][column].isSub=true;
    }






gameBoard.addEventListener('click',cellClick,false);

Your code 您的密码

if(cell.isSub=false) {

is assigning false to the property isSub of cell . 正在将false分配给cell isSub属性。 The assignment's result is then tested by if , which is false . 然后,通过if检验分配的结果,该结果为false That's why the condition is never met and the else -branch is processed. 这就是为什么条件,从来没有遇到过和else分枝进行处理。 Obviously, your intention was 显然,您的意图是

if(cell.isSub==false) {

Maybe try swapping the branches of if statement and test for truthy value in cell.isSub instead making the code simpler: 也许尝试交换if语句的分支并在cell.isSub测试真实值,而不是使代码更简单:

if ( cell.isSub ) {
  // it's a hit
} else {
  // it's a miss
}

If you require to keep this order of branches testing for falsy values can be simplified as well by using negation operator: 如果您需要保持分支的这种顺序,那么使用否定运算符也可以简化对伪造值的测试:

if ( !cell.isSub ) {
  // it's a miss
} else {
  // it's a hit
}

One last tip: don't set custom properties of a DOM element as those perform poorly. 最后一个提示:不要设置DOM元素的自定义属性,因为它们的性能不佳。 Even worse, they are perfect soil for memory leakages when putting Javascript objects or functions. 更糟糕的是,它们是放置Javascript对象或函数时内存泄漏的理想土壤。 Thus, keep the two worlds - your Javascript and your HTML/DOM - as separated as possible and train yourself not to put additional data into Javascript-representations of objects managed in context of DOM. 因此,请将两个世界-您的Javascript和HTML / DOM-尽可能分开,并训练自己不要将其他数据放入在DOM上下文中管理的对象的Javascript表示中。 Use a separate description in pure Javascript (eg two-dimensional array) for tracking position of your subs and create a DOM representing that internal data set, only. 使用纯Javascript(例如,二维数组)中的单独描述来跟踪您的潜艇的位置,并创建仅表示该内部数据集的DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM