Пишу морской бой, уже на заключительном тесте вылезает какая-то ошибка, пытаюсь ее фиксить. И мой горе фикс приводит к тому, что вайл в бразузере просто ломается, пишется "Не удалось получить доступ к файлу
Возможно, он был перемещён, изменён или удалён." В консоле пишет "(index):775 crbug/1173575, non-JS module files deprecated."
var view = {
displayMessage: function (dmess) {
var messageArea = document.getElementById('messageArea')
messageArea.innerHTML = dmess;
},
displayHit: function (location) {
var messageHit = document.getElementById(location)
messageHit.setAttribute('class', 'hit')
},
displayMiss: function (location) {
var messageMiss = document.getElementById(location)
messageMiss.setAttribute('class', 'miss')
},
}
var model = {
tablSize: 7,
numShips: 3,
shipLenght: 3,
shipsSunk: 0,
ships: [{ location: ['0', '0', '0'], hits: ['', '', ''] },
{ locations: ['0', '0', '0'], hits: ['', '', ''] },
{ locations: ['0', '0', '0'], hits: ['', '', ''] }],
fire: function (guess) {
for (i = 0; i < this.numShips; i++) {
var ship = this.ships[i]
var index = ship.location.indexOf(guess)
if (index >= 0) { //
ship.hits[index] = 'hits'
view.displayHit(guess)
view.displayMessage('HIT')
if (this.isSunk(ship)) {
this.shipsSunk++;
view.displayMessage('Ты потопил палубу корабля')
}
return true
}
}
view.displayMiss(guess)
view.displayMessage('Ты промазал')
return false
},
isSunk: function (ship) {
for (var i = 0; i < this.shipLenght; i++) {
if (ship.hits[i] !== 'hits') {
return false
}
return true
}
},
generateShipLocations: function () {
var locations;
for (i = 0; i < model.numShips; i++)
do {
location = this.generateShip()
} while (this.collision(location));
this.ships[i].locations = location
},
generateShip: function () {
var position = Math.floor(Math.random() * 2)
var row, col;
if (position === 1) {
row = Math.floor(Math.random() * this.tablSize)
col = Math.floor(Math.random() * this.tablSize - this.shipLenght)
} else {
row = Math.floor(Math.random() * this.tablSize - this.shipLenght)
col = row = Math.floor(Math.random() * this.tablSize)
}
var newLoc = [];
for (var i = 0; i < this.shipLenght; i++) {
if (position === 1) {
newLoc.push(row + '' + (col + i))
} else {
newLoc.push((row + i) + '' + col)
}
}
return newLoc
},
collision: function () {
for (i = 0; i < this.numShips; i++) {
var ship = model.ships[i]
for (j = 0; j < location.length; j++) {
if (ship.location.indexOf(location[j] >= 0)) {
return true
}
}
}
return false
}
};
var controller = {
guesses: 0,
processGuess: function (guess) {
// var locations = this.processGuess(guess)
// if (locations) {
// this.guesses++
// var hit = model.fire(locations)
// if (hit && model.shipsSunk === model.numShips){
// view.displayMessage('')
// }
// }
//
var alf = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
if (guess === null || guess.length !== 2) {
alert('Впиши нормальные координаты')
} else {
var oneChar = guess.charAt(0)
var row = alf.indexOf(oneChar)
var twoChar = guess.charAt(1)
if (isNaN(row) || isNaN(twoChar)) {
alert('Введи нормальные числа')
} else if (row < 0 || row >= model.tablSize || twoChar < 0 || twoChar >= model.tablSize) {
alert('Введи нормальные числа')
} else {
var guess = row + twoChar
model.fire(guess)
}
}
return null;
}
}
function init() {
var fireButton = document.getElementById('shotButton')
fireButton.onclick = handFireButton
model.generateShipLocations()
}
function handFireButton() {
var guessInput = document.getElementById('inputShot')
var guess = guessInput.value
controller.processGuess(guess)
guessInput.value = ' '
}
window.onload = init;