The code provided is working fine, but I would like to see if there is room for improvement for the code provided:
function Ship (size, direction) {
this.coveredFields = [];
this.place = function (sizeY, sizeX) { // sizeX & sizeY: size of fields in both dimensions
// pick randomly within our limits
var locationX;
var locationY;
if (direction) {
locationX = Math.floor(Math.random() * (sizeX - 1 - size));
locationY = Math.floor(Math.random() * (sizeY - 1));
} else {
locationX = Math.floor(Math.random() * (sizeX - 1));
locationY = Math.floor(Math.random() * (sizeY - 1 - size));
}
// setting locations
for (var i = 0 ; i < size ; i++) {
if (direction) {
this.coveredFields.push(locationY * 10 + locationX + i)
} else {
this.coveredFields.push((locationY + i) * 10 + locationX)
}
}
}
}
var ship1 = new Ship(3, true);
ship1.place(6,6);
ship1.coveredFields;
var ship2 = new Ship(3, false);
ship2.place(6,6);
ship2.coveredFields;
var ship3 = new Ship(3, true);
ship3.place(6,6);
ship3.coveredFields;
var guess;
var hits = 0;
var guesses = 0;
var isSunk = false;
var shipLength = 7;
var countSunkShip = 0;
var shipsArray = [];
shipsArray.push(ship1.coveredFields, ship2.coveredFields, ship3.coveredFields);
console.log(shipsArray);
/*function collision(direction)
{
for (var i = 0; i < shipsArray.length; i++)
{
for (var j = 0; j < direction.length; j++)
{
if (shipsArray[i].direction.indexOf(direction[j]) != -1)
{
return true;
}
}
}
return false;
}*/
var createTable = function(cellsInARow, Rows){
var table = document.createElement('table');
for (var i = 0; i <= Rows; i++){
var tr = document.createElement('tr');
for(var j=0; j<=cellsInARow; j++){
var td = document.createElement('td');
td.style.width = "50px";
td.style.height = "50px";
td.id = "td"+i+j;
var text1 = document.createTextNode('');
td.appendChild(text1);
tr.appendChild(td);
}
table.appendChild(tr);
}
document.body.appendChild(table);
}
//call function to create grid(7,7)
createTable(6,6);
//game's logic
// add event listeners to all the td's
var tags = document.getElementsByTagName("td");
console.log(tags);
for(var i=0; i<tags.length; i++){
//console.log(tags[i].id.replace('td',''));
matchTrue = 0;
tags[i].addEventListener("click", function clicked(d){
guesses++;
// get the id of the cliked item in a var
var locationClicked = parseInt(this.id.replace('td',''));
// var which indicates if the match has been made
var matchTrue = 0;
// looping through all locations, from 1-9
for(var i=0; i<shipsArray.length && matchTrue==0; i++){
for(var j=0; j<shipsArray.length && matchTrue==0; j++){
//check if locations correspond to cliked items' ids
if(locationClicked == shipsArray[i][j]){
matchTrue = 1;
hits++;
if (hits == 9) {
isSunk = true;
var div = document.createElement("div");
div.style.color = "#3dcabb;";
div.id = "congrats-message";
var stats = "You took " + guesses + " guesses to find all the objects, " +
"which means your total accuracy was " + (3/guesses);
div.innerHTML = "Congratulations, the game is over! You have overcome the challenge!"+ "<br/>"+ stats;
document.body.appendChild(div);
// assigning the final values to the final vars
document.getElementById("hitArea").innerHTML = "Hit accuracy: "+(3/guesses);
document.getElementById("guessArea").innerHTML = "Guesses: "+(guesses);
}
if(hits%3==0){
countSunkShip++;
document.getElementById("shipArea").innerHTML = "Ships sunk: "+ countSunkShip;
}
}
else
{
matchTrue = 0;
}// END if which checks locations == clicked items
}
}
//check if the match was made
if(matchTrue == 1){
//insert ship image here
var img = document.createElement('img');
img.src = "ship3.png";
img.style.backgroundPosition = "center center";
this.appendChild(img);
}
else{
//insert miss image here
var img = document.createElement('img');
img.src = "arrow.png";
this.appendChild(img);
}
var node = d.target.parentNode.querySelector("td");
console.log(node);
d.target.removeEventListener("click", clicked, false);
});
}
//resetBoard button
function reloadPage(){
window.location.reload();
}
var resetButton = document.getElementById('resetBtn');
console.log(resetButton);
resetButton.onclick = reloadPage;