Advertisement
TheTintin

CreateNeighborTab

Nov 2nd, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Crée un tableau global qui associe une cellule à ses voisins (non nul et non obstacle)
  2. if (getTurn() == 1) {
  3.     var x, y, newCell;
  4.     var neighbors = [];
  5.     var isNeighbor = function (aCell) {
  6.         if (aCell == null) {
  7.             return false;
  8.         }
  9.         if (isObstacle(aCell)) {
  10.             return false;
  11.         }
  12.         return true;
  13.     };
  14.    
  15.     for (var cell = 0; cell < 613; cell++) {
  16.         if (!isObstacle(cell)) {
  17.             neighbors = [];
  18.             x = getCellX(cell);
  19.             y = getCellY(cell);
  20.        
  21.             newCell = getCellFromXY(x - 1, y);
  22.             if (isNeighbor(newCell)) push(neighbors, newCell);
  23.             newCell = getCellFromXY(x + 1, y);
  24.             if (isNeighbor(newCell)) push(neighbors, newCell);
  25.             newCell = getCellFromXY(x, y - 1);
  26.             if (isNeighbor(newCell)) push(neighbors, newCell);
  27.             newCell = getCellFromXY(x, y + 1);
  28.             if (isNeighbor(newCell)) push(neighbors, newCell);
  29.        
  30.             neighborsCells[cell] = neighbors;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement