The ship defined in the ship variable generates within the proper coordinates. The other two ships, battleShip and destroyer, generate far out of the proper grid. Battleship and destroyer coordinates generate outside of the grid, but the ship defined as ship is generating correctly. Much of the code is setting the values for the 3 ships. The x and y coordinates are defined in shipRow and shipCol, along with battleShipRow, BattloeShipCol, and destroyerRow and destroyerCol.
I tried adjusting the shipRow and shipCol values, but that did not solve the problem.
//Create a random location for the ship's x and y position
let shipRow = Math.floor(Math.random() * 9);
let shipCol = Math.floor(Math.random() * 10);
let battleShipRow = Math.floor(Math.random() * 7);
let battleShipCol = Math.floor(Math.random() * 10);
let CELL_HEIGHT = 27;
let CELL_WIDTH = 37;
if(battleShipRow >= shipRow && battleShipRow <= shipRow + 2)
if (battleShipCol === shipCol)
{
battleShipRow = Math.floor(Math.random() * 2);
battleShipCol = Math.floor(Math.random() * 2);
}
let destroyerRow = Math.floor(Math.random() * 2);
let destroyerCol = Math.floor(Math.random() * 2);
//create map
let map =
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
];
map[battleShipRow][battleShipCol] = 1;
map[battleShipRow + 1][battleShipCol] = 1;
map[battleShipRow + 2][battleShipCol] = 1;
map[battleShipRow + 3][battleShipCol] = 1;
map[destroyerRow][destroyerCol] = 2;
map[destroyerRow + 1][destroyerCol] = 2;
map[destroyerRow + 2][destroyerCol] = 2;
map[shipRow][shipCol] = 3;
map[shipRow + 1][shipCol] = 3;
let shipY = shipRow * CELL_HEIGHT + CELL_HEIGHT;
let shipX = shipCol * CELL_WIDTH + CELL_WIDTH;
let battleShipY = battleShipRow * CELL_HEIGHT + CELL_HEIGHT;
let battleShipX = battleShipRow * CELL_HEIGHT + CELL_HEIGHT;
let destroyerY = destroyerRow * CELL_HEIGHT + CELL_HEIGHT;
let destroyerX = destroyerCol * CELL_WIDTH + CELL_WIDTH;
//Then set the style top and style left for each ship using the above calculations.
let vbattleShip = document.querySelector("#battleShip");
vbattleShip.style.top = battleShipY + CELL_HEIGHT + CELL_WIDTH + "px";
vbattleShip.style.left = battleShipX * CELL_WIDTH + CELL_WIDTH + "px";
let vdestroyer = document.querySelector("#destroyer");
vdestroyer.style.top = destroyerY + CELL_HEIGHT + CELL_WIDTH + "px";
vdestroyer.style.left = destroyerX + CELL_WIDTH + CELL_WIDTH + "px";
let vship = document.querySelector("#ship");
vship.style.top = shipY + CELL_HEIGHT + CELL_HEIGHT + "px";
vship.style.left = shipX + CELL_WIDTH + CELL_WIDTH +"px";
//variables for the user's guess
let guessX = 0;
let guessY = 0;
//variable to keep track of guesses
let guesses = 0;
//game state variable
let gameWon = false;
let ship = document.createElement("img");
ship.setAttribute("class", "ship");
let battleShip = document.createElement("img");
battleShip.setAttribute("class", "battleShip");
let destroyer = document.createElement("img");
destroyer.setAttribute("class", "destroyer");
//Game object variable
//set the location of the ship div tag on the screen
battleShip.style.visibility = "visible";
destroyer.style.visibility = "visible";
ship.style.visibility = "visible";
//Input and output variables
let inputX = document.querySelector("#inputX");
let inputY = document.querySelector("#inputY");
let output = document.querySelector("#output");
var button = document.querySelector("#fire");
button.style.cursor = "pointer";
button.addEventListener("click", clickHandler, false);
function clickHandler()
{
validateInput();
}
function validateInput()
{
guessRow = parseInt(inputX.value);
guessCol = parseInt(inputY.value);
if(isNaN(guessX) || isNaN(guessY) )
{
output.innerHTML = "Please enter a number.";
}
else if(guessX > 300 || guessY > 300)
{
output.innerHTML = "Please enter a number less than 300.";
}
else
{
playGame();
}
}
function playGame()
{
guesses++;
if(guessX >= shipX && guessX <= shipX + 13)
{
//Yes, it's within the X range, so now let's
//check the Y range
if(guessY >= shipY && guessY <= shipY + 55)
{
//It's in both the X and Y range, so it's a hit!
gameWon = true;
endGame();
}
else if(guessX >= battleShipX && guessX <= battleShipX + 31)
//Yes, it's within the X range, so now let's
//check the Y range
if(guessY >= battleShipY && guessY <= battleShipY + 209)
{
//It's in both the X and Y range, so it's a hit!
gameWon = true;
endGame();
}
else if(guessX >= destroyerX && guessX <= destroyerX + 20)
//Yes, it's within the X range, so now let's
//check the Y range
if(guessY >= destroyerY && guessY <= destroyerY + 100)
{
//It's in both the X and Y range, so it's a hit!
gameWon = true;
endGame();
}
} else
{
output.innerHTML = "Miss!"
}
}
function endGame()
{
ship.style.visibility = "visible";
battleShip.style.visibility = "visible";
destroyer.style.visibility = "visible";
if(gameWon)
{
output.innerHTML = "You hit my patrol ship! It took you " + guesses + " guesses";
}
}