简体   繁体   English

for循环不会多次循环执行JS代码

[英]for loop does not loop the code multiple times JS

I am making a battleship game.. and a piece of code needs to place random battleships on the field for the enemy. 我正在制作一艘战舰游戏..一段代码需要在敌人的战场上放置随机的战舰。 (5 battleships so i now need to call the function 5 times). (5艘战舰,所以我现在需要调用该函数5次)。 But the first for loop is kind of doing nothing. 但是第一个for循环却什么也不做。 why, and how can i maybe fix this issue? 为什么,我怎么能解决这个问题? Thanks in advance! 提前致谢!

var ships = [
        {
            shipClass: "cruiser",
            shipLength: 3
        },
        {
            shipClass: "battleship",
            shipLength: 4
        },
        {
            shipClass: "submarine",
            shipLength: 3
        },
        {
            shipClass: "destroyer",
            shipLength: 2
        },
        {
            shipClass: "carrier",
            shipLength: 5
        },
    ]
    var currentShipIndex = 0

    function placeEnemyBoat() {
        var currentShipSize = ships[currentShipIndex].shipLength
        var randomInt = Math.floor(Math.random() * numRows * numRows)

        for (var i = 0; i < ships.length; i++){
            if (Math.random() < 0.5) {
                if (isValidEnemyPosition(randomInt, ships[currentShipIndex].shipLength, "vertical")) {
                    for (var idx = randomInt; idx < randomInt + currentShipSize; idx++) {
                        enemySquares[idx].draw(enemyCtx, "ship", ships[currentShipIndex].shipClass)
                    }
                    currentShipIndex += 1
                }
            }else {
                if (isValidEnemyPosition(randomInt, ships[currentShipIndex].shipLength, "horizontal")) {
                    for (var index = randomInt; index < randomInt + (currentShipSize * numRows); index += numRows) {
                        enemySquares[index].draw(enemyCtx, "ship", ships[currentShipIndex].shipClass)
                    }
                    currentShipIndex += 1
                }
            }
        }
    }

I'm assuming you have numRows, draw, enemyCtx, enemySquares, etc. are defined elsewhere in your code. 我假设您在代码中的其他位置定义了numRows,draw,integerCtx,敌人Squares等。 Without more of your code, it's really hard to say. 没有更多的代码,这很难说。

I commented out a line above the for loop and most of the for loop's body to avoid these undeclared variables. 为了避免这些未声明的变量,我在for循环和大多数for循环的主体上方注释了一行。 And I added a simple console.log statement. 我添加了一个简单的console.log语句。

var ships = [
    {
        shipClass: "cruiser",
        shipLength: 3
    },
    {
        shipClass: "battleship",
        shipLength: 4
    },
    {
        shipClass: "submarine",
        shipLength: 3
    },
    {
        shipClass: "destroyer",
        shipLength: 2
    },
    {
        shipClass: "carrier",
        shipLength: 5
    },
]
var currentShipIndex = 0

function placeEnemyBoat() {
    var currentShipSize = ships[currentShipIndex].shipLength
    //var randomInt = Math.floor(Math.random() * numRows * numRows)

    for (var i = 0; i < ships.length; i++){
        console.log(i);
        /*
        if (Math.random() < 0.5) {
            if (isValidEnemyPosition(randomInt, ships[currentShipIndex].shipLength, "vertical")) {
                for (var idx = randomInt; idx < randomInt + currentShipSize; idx++) {
                    enemySquares[idx].draw(enemyCtx, "ship", ships[currentShipIndex].shipClass)
                }
                currentShipIndex += 1
            }
        }else {
            if (isValidEnemyPosition(randomInt, ships[currentShipIndex].shipLength, "horizontal")) {
                for (var index = randomInt; index < randomInt + (currentShipSize * numRows); index += numRows) {
                    enemySquares[index].draw(enemyCtx, "ship", ships[currentShipIndex].shipClass)
                }
                currentShipIndex += 1
            }
        }
        */
    }
}

placeEnemyBoat();

But what you have does seem to be making five iterations through the for loop. 但是您所拥有的似乎确实是通过for循环进行了五次迭代。

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

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