Advertisement
Guest User

cell.js

a guest
Aug 25th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Cell(i, j) {
  2.   this.i = i;
  3.   this.j = j;
  4.  
  5.   this.f = 0;
  6.   this.g = 0;
  7.   this.h = 0;
  8.  
  9.   this.walls = [true, true, true, true]; // top, right, bottom, left
  10.   this.visited = false;
  11.  
  12.   this.previous = undefined;
  13.  
  14.   this.checkNeighbors = function() {
  15.     var neighbors = [];
  16.  
  17.     var top = grid[index(i, j - 1)];
  18.     var right = grid[index(i + 1, j)];
  19.     var bot = grid[index(i, j + 1)];
  20.     var left = grid[index(i - 1, j)];
  21.  
  22.     if (top && !top.visited) {
  23.       neighbors.push(top);
  24.     }
  25.     if (right && !right.visited) {
  26.       neighbors.push(right);
  27.     }
  28.     if (bot && !bot.visited) {
  29.       neighbors.push(bot);
  30.     }
  31.     if (left && !left.visited) {
  32.       neighbors.push(left);
  33.     }
  34.  
  35.     if (neighbors.length > 0) {
  36.       var r = floor(random(0, neighbors.length));
  37.       return neighbors[r];
  38.     } else {
  39.       return undefined;
  40.     }
  41.   }
  42.  
  43.   this.getNeighbors = function() {
  44.     var neighbors = [];
  45.  
  46.     var top = grid[index(i, j - 1)];
  47.     var right = grid[index(i + 1, j)];
  48.     var bot = grid[index(i, j + 1)];
  49.     var left = grid[index(i - 1, j)];
  50.  
  51.     if (top && !top.visited && walls[0]) {
  52.       neighbors.push(top);
  53.     }
  54.     if (right && !right.visited && walls[1]) {
  55.       neighbors.push(right);
  56.     }
  57.     if (bot && !bot.visited && walls[2]) {
  58.       neighbors.push(bot);
  59.     }
  60.     if (left && !left.visited && walls[3]) {
  61.       neighbors.push(left);
  62.     }
  63.  
  64.     return neighbors;
  65.   }
  66.  
  67.   this.show = function() {
  68.     var x = this.i * w;
  69.     var y = this.j * w;
  70.     stroke(255);
  71.  
  72.     if (this.walls[0]) {
  73.       line(x, y, x + w, y);
  74.     }
  75.     if (this.walls[1]) {
  76.       line(x + w, y, x + w, y + w);
  77.     }
  78.     if (this.walls[2]) {
  79.       line(x + w, y + w, x, y + w);
  80.     }
  81.     if (this.walls[3]) {
  82.       line(x, y + w, x, y);
  83.     }
  84.     if (this.visited) {
  85.       noStroke();
  86.       fill(255, 0, 255, 100);
  87.       rect(x, y, w, w);
  88.     }
  89.   }
  90.  
  91.   this.highlight = function() {
  92.     var x = this.i * w;
  93.     var y = this.j * w;
  94.     noStroke();
  95.     fill(0, 0, 255, 100);
  96.     rect(x, y, w, w);
  97.   }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement