Я предполагаю, что у вас есть 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.