javascript - Running into "Maximum call stack" errors on a recursive function -


i'm making height map editor. grid of numbers can change location +/- 1. editor makes sure there can difference of 1 between of touching 8 tiles.

i'm doing recursive function. looking @ it's 8 neighbors , adjusting them needed. if adjusted, call function on 8 neighbors.

i uncaught rangeerror: maximum call stack size exceeded errors after messing around awhile , can't see coming from. i'm doing checks make sure don't try access non-exsiting grid locations...

the function this:

var movedown = function (x, y) {     var updated = false;     if (x-1 >= 0 && math.abs(grid[x][y] - grid[x-1][y]) > 1) {         grid[x-1][y] -= 1;         updated = true;     }     if (x-1 < size && math.abs(grid[x][y] - grid[x+1][y]) > 1) {         grid[x+1][y] -= 1;         updated = true;     }     if (y-1 >= 0 && math.abs(grid[x][y] - grid[x][y-1]) > 1) {         grid[x][y-1] -= 1;         updated = true;     }     if (y+1 < size && math.abs(grid[x][y] - grid[x][y+1]) > 1) {         grid[x][y+1] -= 1;         updated = true;     }     if (x-1 >= 0 && y-1 >= 0 && math.abs(grid[x][y] - grid[x-1][y-1]) > 1) {         grid[x-1][y-1] -= 1;         updated = true;     }     if (x-1 >= 0 && y+1 < size && math.abs(grid[x][y] - grid[x-1][y+1]) > 1) {         grid[x-1][y+1] -= 1;         updated = true;     }     if (x+1 < size && y-1 >= 0 && math.abs(grid[x][y] - grid[x+1][y-1]) > 1) {         grid[x+1][y-1] -= 1;         updated = true;     }     if (x+1 < size && y+1 < size && math.abs(grid[x][y] - grid[x+1][y+1]) > 1) {         grid[x+1][y+1] -= 1;         updated = true;     }      if (updated) {         if (x-1 >= 0) { movedown(x-1, y); }         if (x+1 < size) { movedown(x+1, y); }         if (y-1 >= 0) { movedown(x, y-1); }         if (y+1 < size) { movedown(x, y+1); }         if (x-1 >= 0 && y-1 >= 0) { movedown(x-1, y-1); }         if (x-1 >= 0 && y+1 < size) { movedown(x-1, y+1); }         if (x+1 < size && y-1 >= 0) { movedown(x+1, y-1); }         if (x+1 < size && y+1 < size) { movedown(x+1, y+1); }     } } 

i have fiddle here. , looks can inline it, did too.

var size = 15  var grid;  var active = {x: -1, y: -1};    var movedown = function (x, y) {      var updated = false;      if (x-1 >= 0 && math.abs(grid[x][y] - grid[x-1][y]) > 1) {          grid[x-1][y] -= 1;          updated = true;      }      if (x-1 < size && math.abs(grid[x][y] - grid[x+1][y]) > 1) {          grid[x+1][y] -= 1;          updated = true;      }      if (y-1 >= 0 && math.abs(grid[x][y] - grid[x][y-1]) > 1) {          grid[x][y-1] -= 1;          updated = true;      }      if (y+1 < size && math.abs(grid[x][y] - grid[x][y+1]) > 1) {          grid[x][y+1] -= 1;          updated = true;      }      if (x-1 >= 0 && y-1 >= 0 && math.abs(grid[x][y] - grid[x-1][y-1]) > 1) {          grid[x-1][y-1] -= 1;          updated = true;      }      if (x-1 >= 0 && y+1 < size && math.abs(grid[x][y] - grid[x-1][y+1]) > 1) {          grid[x-1][y+1] -= 1;          updated = true;      }      if (x+1 < size && y-1 >= 0 && math.abs(grid[x][y] - grid[x+1][y-1]) > 1) {          grid[x+1][y-1] -= 1;          updated = true;      }      if (x+1 < size && y+1 < size && math.abs(grid[x][y] - grid[x+1][y+1]) > 1) {          grid[x+1][y+1] -= 1;          updated = true;      }            if (updated) {          if (x-1 >= 0) { movedown(x-1, y); }          if (x+1 < size) { movedown(x+1, y); }          if (y-1 >= 0) { movedown(x, y-1); }          if (y+1 < size) { movedown(x, y+1); }          if (x-1 >= 0 && y-1 >= 0) { movedown(x-1, y-1); }          if (x-1 >= 0 && y+1 < size) { movedown(x-1, y+1); }          if (x+1 < size && y-1 >= 0) { movedown(x+1, y-1); }          if (x+1 < size && y+1 < size) { movedown(x+1, y+1); }      }  }        var moveup = function (x, y) {      var updated = false;      if (x-1 >= 0 && math.abs(grid[x][y] - grid[x-1][y]) > 1) {          grid[x-1][y] += 1;          updated = true;      }      if (x-1 < size && math.abs(grid[x][y] - grid[x+1][y]) > 1) {          grid[x+1][y] += 1;          updated = true;      }      if (y-1 >= 0 && math.abs(grid[x][y] - grid[x][y-1]) > 1) {          grid[x][y-1] += 1;          updated = true;      }      if (y+1 < size && math.abs(grid[x][y] - grid[x][y+1]) > 1) {          grid[x][y+1] += 1;          updated = true;      }      if (x-1 >= 0 && y-1 >= 0 && math.abs(grid[x][y] - grid[x-1][y-1]) > 1) {          grid[x-1][y-1] += 1;          updated = true;      }      if (x-1 >= 0 && y+1 < size && math.abs(grid[x][y] - grid[x-1][y+1]) > 1) {          grid[x-1][y+1] += 1;          updated = true;      }      if (x+1 < size && y-1 >= 0 && math.abs(grid[x][y] - grid[x+1][y-1]) > 1) {          grid[x+1][y-1] += 1;          updated = true;      }      if (x+1 < size && y+1 < size && math.abs(grid[x][y] - grid[x+1][y+1]) > 1) {          grid[x+1][y+1] += 1;          updated = true;      }            if (updated) {          if (x-1 >= 0) { moveup(x-1, y); }          if (x+1 < size) { moveup(x+1, y); }          if (y-1 >= 0) { moveup(x, y-1); }          if (y+1 < size) { moveup(x, y+1); }          if (x-1 >= 0 && y-1 >= 0) { moveup(x-1, y-1); }          if (x-1 >= 0 && y+1 < size) { moveup(x-1, y+1); }          if (x+1 < size && y-1 >= 0) { moveup(x+1, y-1); }          if (x+1 < size && y+1 < size) { moveup(x+1, y+1); }      }  }        var init = function () {      $('#board').mouseleave(function () {          active.x = -1;          active.y = -1;      })      .mousemove(function () {          active.x = -1;          active.y = -1;      });      $('#reset').click(function () {          for(var x=0; x<size; x++) {              for(var y=0; y<size; y++) {                  grid[x][y] = 1;              }          }      });      $(window).keydown(function (e) {          if (e.keycode === 119 || e.keycode === 87) {              // w              grid[active.x][active.y] += 1;              moveup(active.x, active.y);          }          if (e.keycode === 115 || e.keycode === 83) {              // s              grid[active.x][active.y] -= 1;              movedown(active.x, active.y);          }      });            grid = [];      for(var x=0; x<size; x++) {          grid[x] = [];          var row = $('<div class="row">');          for(var y=0; y<size; y++) {              grid[x][y] = 1;              var cell = $('<div id="c' + x + '_' + y + '" class="cell">');              cell.data('x', x).data('y', y);              cell.mousemove(function (e) {                  var $this = $(this);                  active.x = $this.data('x');                  active.y = $this.data('y');                  e.stoppropagation();              });              row.append(cell)          }          $('#board').append(row);      }            setinterval(function () {          for(var x=0; x<size; x++) {              for(var y=0; y<size; y++) {                  $('#c' + x + '_' + y).text(grid[x][y]);              }          }          $('#info').text('x: ' + active.x + ' y: ' + active.y);      }, 100);  };  init();
#board {      padding: 20px;      z-index: -1;  }    .row {      height: 25px;  }    .cell {      position: relative;      display: inline-block;      width: 25px;      height: 25px;      border: 1px solid black;      text-align: center;      line-height: 25px;      cursor: default;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="board"></div>  <div id="info"></div>  <p>      click on board once set focus. lets keyboard work. press <strong>s</strong> make tile go down, press <strong>w</strong> make tile go up.  </p>  <button id="reset">reset</button>

uncaught rangeerror: maximum call stack size exceeded error occurs when there no breakpoint recursion function. when call function function or such that. execution goes caller function goes stack , callee function gets memory execution. if there no return statement or condition before recursion, processor stack gets full , throws error message.

in case, calling movedown movedown there 1 condition true , responsible calling movedown again, or same moveup.

just debug breakpoints, wrong.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -