generateShip: function(){
//получение направления построения корабля
var direction = Math.floor(Math.random() * 2);
var row, col;
//Ограничения условия начальной позиции корабля, чтобы он не вышел за пределы поля в зависимости от напрвления
if (direction === 1){
row = Math.floor(Math.random()*this.boardSize);
col = Math.floor(Math.random()*(this.boardSize - this.shipLength));
}
else{
col = Math.floor(Math.random()*this.boardSize);
row = Math.floor(Math.random()*(this.boardSize - this.shipLength));
}
//Получение координат корабля
var newShipLocation = [];
var currentShipLocation;
var newShipEnvironment = [];
var currentShipEnvironment;
var coordinatesForEnvironment = [11,-11,9,-9,1,-1,10,-10];
for (var i = 0; i < this.shipLength; i++) {
if (direction === 1) {
currentShipLocation = row + "" + (col + i);
}
else{
currentShipLocation = (row + i) + "" + col;
}
newShipLocation.push(currentShipLocation);
//и его окружения для автообводки при уничтожении и для избежания близкого расположения кораблей
for (var j = 0; j < coordinatesForEnvironment.length; j++) {
currentShipEnvironment = coordinatesForEnvironment[j] + Number(currentShipLocation);
if (currentShipEnvironment > 0 && currentShipEnvironment < 100){
if (String(currentShipEnvironment)[1] == undefined){
currentShipEnvironment = "0" + currentShipEnvironment;
}
if(direction === 1){
if (Math.abs(Number(String(currentShipLocation)[0] - String(currentShipEnvironment)[0])) < 2 &&
Math.abs(Number(String(currentShipLocation)[1] - String(currentShipEnvironment)[1])) < 5){
newShipEnvironment.push(String(currentShipEnvironment));
}
}
else{
if (Math.abs(Number(String(currentShipLocation)[0] - String(currentShipEnvironment)[0])) < 5 &&
Math.abs(Number(String(currentShipLocation)[1] - String(currentShipEnvironment)[1])) < 2){
newShipEnvironment.push(String(currentShipEnvironment));
}
}
}
}
//Удаление дубликатов
newShipEnvironment = getUnique(newShipEnvironment);
}
//удаление позиций корабля из его окружения
var loc, env, index;
for (var k = newShipEnvironment.length - 1; k > 0 ; k--) {
for (var z = 0; z < newShipLocation.length; z++) {
env = newShipEnvironment[k];
loc = newShipLocation[z];
if (env == loc){
index = newShipEnvironment.indexOf(newShipLocation[z]);
newShipEnvironment.splice(index, 1);
}
}
}
return {
Locations: newShipLocation,
Environment: newShipEnvironment
}
},