цикл for не повторяет код несколько раз JS - PullRequest
0 голосов
/ 27 сентября 2018

Я делаю игру на линкорах ... и часть кода должна разместить на поле случайные линкоры для врага.(5 линейных кораблей, так что теперь мне нужно вызвать функцию 5 раз).Но первый цикл for ничего не делает.почему и как я могу решить эту проблему?Заранее спасибо!

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
                }
            }
        }
    }

1 Ответ

0 голосов
/ 27 сентября 2018

Я предполагаю, что у вас есть numRows, Draw, врагиCtx, враги и квадраты, которые определены в других местах вашего кода.Без большого количества вашего кода, это действительно трудно сказать.

Я закомментировал строку над циклом for и большую часть тела цикла for, чтобы избежать этих необъявленных переменных.И я добавил простое выражение 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();

Но, похоже, вы делаете пять итераций в цикле for.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...