SCRIPT16389:SCRIPT16389:未指定的错误。边缘控制台

时间:2018-12-01 01:00:59

标签: javascript jquery html microsoft-edge

我知道发布的其他一些问题与此主题相关,但是他们的回答对我没有帮助。 我试图在js中实现战舰,并且在调试时得到了SCRIPT16389:SCRIPT16389:未指定的错误。在Microsoft Edge控制台中。 到目前为止,这是我的js代码:(我是javascript的初学者,因此如果出现严重错误,敬请原谅)

var board = null;
var boats = null;
var rows = 8;
var columns = 8;
$(document).ready(function(){
  if(typeof(Storage)!=="undefined"){
    boats = JSON.parse(localStorage.getItem("boats"));

    console.log(boats);
    if(boats===null){
      boats = [
       {size:2, letter:'f', name:'fragate'},
       {size:3, letter:'b', name:'buque'},
       {size:3, letter:'s', name:'submarine'},
       {size:4, letter:'d', name:'destructor'},
       {size:5, letter:'p', name:'portplanes'},

     ];
     localStorage.setItem("boats", JSON.stringify(boats));

    }
    rows = parseInt(localStorage.getItem("rows"));
    columns = parseInt(localStorage.getItem("columns"));
    if(isNaN(rows)|| isNaN(columns)){
      rows = 8;
      columns = 8;
      localStorage.setItem("rows", 8);
      localStorage.setItem("columns", 8);
    }
  }else {
    console.log("We don't have localStorage")
  }
});
function createMatrix(row,col){
  var matrix;
  matrix = new Array(row);
  for (var i=0; i<row; i++){
    matriz[i] = new Array(col);
  }
  return matrix;
}
function createFullMatrix(row,col,inside){
  var matrix = new Array(row);
  for(var i=0; i<row; i++){
    matrix[i] = new Array(col);
    for(var j=0; j<col; j++){
      matrix[i][j]=inside;
    }
  }
  return matrix;
}
function matrix2console(matrix){
  var aux;
  for(var i=0; i<matrix.length; i++){
    aux = "";
    for(var j=0; j<matrix[i].length; j++){
      aux+=matrix[i][j]+'\t';
    }
    console.log(aux);
  }
}
function createBoard(){
  var html='<table>';
  for(var i=0; i<rows; i++){
    html+='<tr>';
    for (var j=0; j<columns; j++){
      html+='<td id="cel_'+i+'_'+j+'" class = "water"+ onclick=shoot("celd_'+i+'_'+j+'",'+i+','+j+')></td>';
    }
    html+='</tr>';
  }
  html+='</table>'
  document.getElementById("match").innerHTML=html;
}
function createBoardjQ(){
  $("#match").empty();
  var table = $("<table />");
  for(var i=0; i<rows; i++){
    var row = $("<tr/>");
    for(var j=0; j<columns; j++){
      var celd = $('<td id="celd_'+i+'_'+j+'" onclick=shoot("celd_'+i+'_'+j+'",'+i+','+j+') > </td>');
      celd.addClass("water");
      row.append(celd);
    }
    table.append(row);
  }
  $("#match").append(table);

}
function createMatch(){
  board = createMatrix(rows,columns);
  startMatrix('a',board);
  setBoats(board);
  createBoardjQ();
  matrix2console(board);
}
function shoot(celd, i, j){
  switch (board[i][j]) {
    case 'a':
           board[i][j]= 'A';
           $('#'+celd).removeClass('water');
           $('#'+celd).addClass('miss')

break;
    case 'b':
           board[i][j]= 'B';
           $('#'+celd).removeClass('water');
           $('#'+celd).addClass('boats')

break;
    case 'd':
           board[i][j]= 'd';
           $('#'+celd).removeClass('water');
           $('#'+celd).addClass('boats')
           break;

      case 'f':
             board[i][j]= 'F';
             $('#'+celd).removeClass('water');
             $('#'+celd).addClass('boats')

 break;
 case 'p':
        board[i][j]= 'P';
        $('#'+celd).removeClass('water');
        $('#'+celd).addClass('boats')

break;
case 's':
       board[i][j]= 'S';
       $('#'+celd).removeClass('water');
       $('#'+celd).addClass('boats')

break;
  }
}
function startMatrix(data, matrix){
  for(var i=0; i<matrix.length;){
    for(var j=0; j<matrix[i].length;){
      matrix[i][j]=data;
    }
  }
}
function dice(value){
  var random;
  random = Math.floor(Math.random()*(value));
  return random;
}
function coin(){
  return (dice(2));
}
function setBoats(matrix){
 for (var i=0; i<boats.length; i++){
   var boats = boats[i];

   do{
     var free=true;
     var direction = coin();
     if(!direction){
       var row = dice(matrix.length);
       var col = dice(matrix[row].length-boat.size);
       for(var j=0; j<boat.size;j++){
         if(matrix[row][j]!='a'){
           free = false;
         }
       }
       if(free){
         for(var j=0; j<boat.size; j++){
           matrix[row][j+col]=boat.letter;
         }
       }
     }else{
       var row = dice(matrix.length-boat.size);
       var col = dice(matrix[row].length);
         var row = dice(matrix.length);
         var col = dice(matrix[row].length-boat.size);
         for(var j=0; j<boat.size;j++){
           if(matrix[j+row][col]!='a'){
             free = false;
           }
         }
         if(free){
           for(var j=0; j<boat.size; j++){
             matrix[j+row][col]=boat.letter;
           }
         }
     }
   }while(!free);
 }
  }

1 个答案:

答案 0 :(得分:0)

更正了许多输入错误并检查了逻辑之后,报告的主要问题在startMatrix()中,其中for循环未增加其计数器。这导致了无限循环。

请考虑以下内容进行修复:

function startMatrix(data, matrix) {
  var singleRow, singleCol;
  for (var r = 0; r < matrix.length; r++) {
    for (var c = 0; c < matrix[r].length; c++) {
      matrix[r][c] = data;
    }
  }
  console.log("SM: Matrix Complete", matrix);
}

完整示例:https://jsfiddle.net/Twisty/L1m6jw5q/58/

HTML

<div id="match">
</div>

CSS

#match td {
  width: 20px;
  height: 20px;
}

#match .water {
  background-color: #ccf;
}

#match .miss {
  background-color: #eee;
}

#match .boats {
  background-color: #ccc;
}

JavaScript

var board = null;
var boats = null;
var rows = 8;
var columns = 8;

function createMatrix(row, col) {
  var matrix;
  matrix = new Array(row);
  for (var i = 0; i < row; i++) {
    matrix[i] = new Array(col);
  }
  console.log("CM: New Matrix Created", matrix);
  return matrix;
}

function startMatrix(data, matrix) {
  var singleRow, singleCol;
  for (var r = 0; r < matrix.length; r++) {
    for (var c = 0; c < matrix[r].length; c++) {
      matrix[r][c] = data;
    }
  }
  console.log("SM: Matrix Complete", matrix);
}

function createFullMatrix(row, col, inside) {
  var matrix = new Array(row);
  for (var i = 0; i < row; i++) {
    matrix[i] = new Array(col);
    for (var j = 0; j < col; j++) {
      matrix[i][j] = inside;
    }
  }
  return matrix;
}

function matrix2console(matrix) {
  var aux;
  for (var i = 0; i < matrix.length; i++) {
    aux = "";
    for (var j = 0; j < matrix[i].length; j++) {
      aux += matrix[i][j] + '\t';
    }
    console.log(aux);
  }
}

function createBoardjQ() {
  $("#match").empty();
  var table = $("<table />");
  for (var i = 0; i < rows; i++) {
    var row = $("<tr/>");
    for (var j = 0; j < columns; j++) {
      var celd = $('<td id="celd_' + i + '_' + j + '" data-shoot="celd_' + i + '_' + j + ',' + i + ',' + j + '"></td>');
      celd.addClass("water");
      row.append(celd);
    }
    table.append(row);
  }
  $("#match").append(table);
}

function createMatch() {
  console.log("CMa: Make Board");
  board = createMatrix(rows, columns);
  console.log("CMa: Start Matrix", 'a', board);
  startMatrix('a', board);
  console.log("CMa: Set Boats", board);
  setBoats(board);
  console.log("CMa: Create Board jQ");
  createBoardjQ();
  console.log("CMa: Matrix to Console");
  matrix2console(board);
}

function shoot(celd, i, j) {
  switch (board[i][j]) {
    case 'a':
      board[i][j] = 'A';
      $('#' + celd).removeClass('water');
      $('#' + celd).addClass('miss');
      console.log("Miss");
      break;
    case 'b':
      board[i][j] = 'B';
      $('#' + celd).removeClass('water');
      $('#' + celd).addClass('boats');
      console.log("Hit");
      break;
    case 'd':
      board[i][j] = 'd';
      $('#' + celd).removeClass('water');
      $('#' + celd).addClass('boats');
      console.log("Hit");
      break;
    case 'f':
      board[i][j] = 'F';
      $('#' + celd).removeClass('water');
      $('#' + celd).addClass('boats');
      console.log("Hit");
      break;
    case 'p':
      board[i][j] = 'P';
      $('#' + celd).removeClass('water');
      $('#' + celd).addClass('boats');
      console.log("Hit");
      break;
    case 's':
      board[i][j] = 'S';
      $('#' + celd).removeClass('water');
      $('#' + celd).addClass('boats');
      console.log("Hit");
      break;
  }
}

function dice(value) {
  var random;
  random = Math.floor(Math.random() * (value));
  return random;
}

function coin() {
  return (dice(2));
}

function setBoats(matrix) {
  var i, j, row, col, free;
  for (i = 0; i < boats.length; i++) {
    var boat = boats[i];
    do {
      free = true;
      var direction = coin();
      if (!direction) {
        row = dice(matrix.length);
        col = dice(matrix[row].length - boat.size);
        for (j = 0; j < boat.size; j++) {
          if (matrix[row][j] != 'a') {
            free = false;
          }
        }
        if (free) {
          for (j = 0; j < boat.size; j++) {
            matrix[row][j + col] = boat.letter;
          }
        }
      } else {
        row = dice(matrix.length - boat.size);
        col = dice(matrix[row].length);
        //row = dice(matrix.length);
        //col = dice(matrix[row].length - boat.size);
        for (j = 0; j < boat.size; j++) {
          var k = (j + row) >= 1 ? (j + row) - 1 : 0;
          console.log(j, row, k, boat.size);
          if (k > matrix.length) {
            free = false;
          } else {
            if (matrix[k][col] != 'a') {
              free = false;
            }
          }
        }
        if (free) {
          for (j = 0; j < boat.size; j++) {
            matrix[j + row][col] = boat.letter;
          }
        }
      }
    } while (!free);
  }
}

$(document).ready(function() {
  if (typeof(Storage) !== "undefined") {
    boats = JSON.parse(localStorage.getItem("boats"));
    console.log(boats);

    if (boats === null) {
      boats = [{
          size: 2,
          letter: 'f',
          name: 'fragate'
        },
        {
          size: 3,
          letter: 'b',
          name: 'buque'
        },
        {
          size: 3,
          letter: 's',
          name: 'submarine'
        },
        {
          size: 4,
          letter: 'd',
          name: 'destructor'
        },
        {
          size: 5,
          letter: 'p',
          name: 'portplanes'
        },
      ];
      localStorage.setItem("boats", JSON.stringify(boats));
    }
    rows = parseInt(localStorage.getItem("rows"));
    columns = parseInt(localStorage.getItem("columns"));
    if (isNaN(rows) || isNaN(columns)) {
      rows = 8;
      columns = 8;
      localStorage.setItem("rows", 8);
      localStorage.setItem("columns", 8);
    }
    console.log(rows, columns);
    createMatch();
    $(".water").click(function(e) {
      console.log("Firing Shot", $(this));
      var c = $(this).data("shoot").split(",");
      shoot(c[0], c[1], c[2]);
    });
  } else {
    console.log("We don't have localStorage");
  }
});

一旦解决了无限循环,setBoards()中就会出现另一个逻辑错误。基本上,j + row会比matrix的索引大。您可能需要对其进行审查,甚至可能将其绘制出来,以确保不会将船从板上滑落(这始终是我小时候在战舰上击败我兄弟的最喜欢的方式)。

我还将click回调移动到了jQuery。只是简化了操作,所以我将镜头数据存储在每个单元格的data属性中。易于解析,每次点击都传递给shoot()

希望有帮助。