introduce prettierrc formatting
This commit is contained in:
@@ -4,13 +4,13 @@ function Cell(i, j) {
|
||||
this.walls = [true, true, true, true];
|
||||
this.visited = false;
|
||||
|
||||
this.checkNeighbors = function() {
|
||||
this.checkNeighbors = function () {
|
||||
var neighbors = [];
|
||||
|
||||
var top = grid[index(i, j -1)];
|
||||
var right = grid[index(i+1, j)];
|
||||
var bottom = grid[index(i, j+1)];
|
||||
var left = grid[index(i-1, j)];
|
||||
var top = grid[index(i, j - 1)];
|
||||
var right = grid[index(i + 1, j)];
|
||||
var bottom = grid[index(i, j + 1)];
|
||||
var left = grid[index(i - 1, j)];
|
||||
|
||||
if (top && !top.visited) {
|
||||
neighbors.push(top);
|
||||
@@ -31,32 +31,30 @@ function Cell(i, j) {
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
this.highlight = function() {
|
||||
var x = this.i*w;
|
||||
var y = this.j*w;
|
||||
};
|
||||
this.highlight = function () {
|
||||
var x = this.i * w;
|
||||
var y = this.j * w;
|
||||
noStroke();
|
||||
fill(252, 106, 2, 100);
|
||||
rect(x, y, w, w);
|
||||
}
|
||||
};
|
||||
|
||||
this.show = function() {
|
||||
var x = this.i*w;
|
||||
var y = this.j*w;
|
||||
this.show = function () {
|
||||
var x = this.i * w;
|
||||
var y = this.j * w;
|
||||
stroke(0);
|
||||
if (this.walls[0]) {
|
||||
line(x , y , x + w, y);
|
||||
line(x, y, x + w, y);
|
||||
}
|
||||
if (this.walls[1]) {
|
||||
line(x + w, y , x + w, y + w);
|
||||
line(x + w, y, x + w, y + w);
|
||||
}
|
||||
if (this.walls[2]) {
|
||||
line(x + w, y + w, x , y + w);
|
||||
line(x + w, y + w, x, y + w);
|
||||
}
|
||||
if (this.walls[3]) {
|
||||
line(x , y + w, x , y);
|
||||
line(x, y + w, x, y);
|
||||
}
|
||||
|
||||
if (this.visited) {
|
||||
@@ -64,5 +62,5 @@ function Cell(i, j) {
|
||||
fill(242, 255, 28, 125);
|
||||
rect(x, y, w, w);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4,134 +4,129 @@ var grid = [];
|
||||
var current;
|
||||
var stack = [];
|
||||
|
||||
function setup(){
|
||||
createCanvas(800, 800);
|
||||
cols = floor(width/w)
|
||||
rows = floor(height/w)
|
||||
//frameRate(6);
|
||||
function setup() {
|
||||
createCanvas(800, 800);
|
||||
cols = floor(width / w);
|
||||
rows = floor(height / w);
|
||||
//frameRate(6);
|
||||
|
||||
for (var j = 0; j < rows; j++){
|
||||
for (var i = 0; i < cols; i++){
|
||||
var cell = new Cell(i,j);
|
||||
grid.push(cell);
|
||||
}
|
||||
for (var j = 0; j < rows; j++) {
|
||||
for (var i = 0; i < cols; i++) {
|
||||
var cell = new Cell(i, j);
|
||||
grid.push(cell);
|
||||
}
|
||||
current = grid[0];
|
||||
}
|
||||
current = grid[0];
|
||||
}
|
||||
|
||||
function draw(){
|
||||
background(150);
|
||||
for (var i = 0; i < grid.length; i++){
|
||||
grid[i].show();
|
||||
function draw() {
|
||||
background(150);
|
||||
for (var i = 0; i < grid.length; i++) {
|
||||
grid[i].show();
|
||||
}
|
||||
current.visited = true;
|
||||
current.highlight();
|
||||
var next = current.checkNeighbors();
|
||||
if (next) {
|
||||
next.visited = true;
|
||||
stack.push(current);
|
||||
|
||||
}
|
||||
current.visited = true;
|
||||
current.highlight();
|
||||
var next = current.checkNeighbors();
|
||||
if (next) {
|
||||
next.visited = true;
|
||||
stack.push(current);
|
||||
|
||||
removeWalls(current, next);
|
||||
|
||||
current = next;
|
||||
} else if (stack.length > 0){
|
||||
current = stack.pop();
|
||||
}
|
||||
removeWalls(current, next);
|
||||
|
||||
current = next;
|
||||
} else if (stack.length > 0) {
|
||||
current = stack.pop();
|
||||
}
|
||||
}
|
||||
|
||||
function index(i, j) {
|
||||
if (i < 0 || j < 0 || i > cols - 1 || j > rows - 1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
function index(i, j){
|
||||
if(i < 0 || j < 0 || i > cols-1 || j > rows-1){
|
||||
return -1
|
||||
}
|
||||
|
||||
return i + j * cols;
|
||||
return i + j * cols;
|
||||
}
|
||||
|
||||
function cell(i, j){
|
||||
this.i = i;
|
||||
this.j = j;
|
||||
this.walls = [true, true, true, true];
|
||||
this.visited = false;
|
||||
function cell(i, j) {
|
||||
this.i = i;
|
||||
this.j = j;
|
||||
this.walls = [true, true, true, true];
|
||||
this.visited = false;
|
||||
|
||||
this.checkNeighbors = function(){
|
||||
var neighbors = [];
|
||||
|
||||
var top = grid[index(i, j-1)];
|
||||
var right = grid[index(i+1, j)];
|
||||
var bottom = grid[index(i, j + 1)];
|
||||
var left = grid[index(i-1, j)];
|
||||
this.checkNeighbors = function () {
|
||||
var neighbors = [];
|
||||
|
||||
if (top && !top.visited){
|
||||
neighbors.push(top);
|
||||
}
|
||||
if (right && !right.visited){
|
||||
neighbors.push(right);
|
||||
}
|
||||
if (bottom && !bottom.visited){
|
||||
neighbors.push(bottom);
|
||||
}
|
||||
if (left && !left.visited){
|
||||
neighbors.push(left);
|
||||
}
|
||||
if (neighbors.length > 0){
|
||||
var r = floor(random(0, neighbors.length));
|
||||
return neighbor[r];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
this.highlight = function(){
|
||||
var x = this.i*w;
|
||||
var y = this.j*w;
|
||||
noStroke();
|
||||
fill(0, 0, 255, 125);
|
||||
rect(x,y,w,w);
|
||||
}
|
||||
|
||||
}
|
||||
var top = grid[index(i, j - 1)];
|
||||
var right = grid[index(i + 1, j)];
|
||||
var bottom = grid[index(i, j + 1)];
|
||||
var left = grid[index(i - 1, j)];
|
||||
|
||||
this.show = function(){
|
||||
var x = this.i*w;
|
||||
var y = this.j*w;
|
||||
stroke(255);
|
||||
if (this.walls[0]){
|
||||
line(x,y,x+w,y);
|
||||
}
|
||||
if (this.walls[1]){
|
||||
line(x+w,y,x+w,y+w);
|
||||
}
|
||||
if (this.walls[2]){
|
||||
line(x+w,y+w,x,y+w);
|
||||
}
|
||||
if (this.walls[3]){
|
||||
line(x,y+w,x,y);
|
||||
}
|
||||
if (this.visited){
|
||||
fill(255, 0, 255, 125);
|
||||
rect(x,y,w,w);
|
||||
}
|
||||
}
|
||||
if (top && !top.visited) {
|
||||
neighbors.push(top);
|
||||
}
|
||||
if (right && !right.visited) {
|
||||
neighbors.push(right);
|
||||
}
|
||||
if (bottom && !bottom.visited) {
|
||||
neighbors.push(bottom);
|
||||
}
|
||||
if (left && !left.visited) {
|
||||
neighbors.push(left);
|
||||
}
|
||||
if (neighbors.length > 0) {
|
||||
var r = floor(random(0, neighbors.length));
|
||||
return neighbor[r];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
this.highlight = function () {
|
||||
var x = this.i * w;
|
||||
var y = this.j * w;
|
||||
noStroke();
|
||||
fill(0, 0, 255, 125);
|
||||
rect(x, y, w, w);
|
||||
};
|
||||
};
|
||||
|
||||
this.show = function () {
|
||||
var x = this.i * w;
|
||||
var y = this.j * w;
|
||||
stroke(255);
|
||||
if (this.walls[0]) {
|
||||
line(x, y, x + w, y);
|
||||
}
|
||||
if (this.walls[1]) {
|
||||
line(x + w, y, x + w, y + w);
|
||||
}
|
||||
if (this.walls[2]) {
|
||||
line(x + w, y + w, x, y + w);
|
||||
}
|
||||
if (this.walls[3]) {
|
||||
line(x, y + w, x, y);
|
||||
}
|
||||
if (this.visited) {
|
||||
fill(255, 0, 255, 125);
|
||||
rect(x, y, w, w);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function removeWalls(a, b){
|
||||
var x = a.i - b.i;
|
||||
if (x === 1){
|
||||
a.walls[3] = false;
|
||||
b.walls[1] = false;
|
||||
} else if (x === -1){
|
||||
a.walls[1] = false;
|
||||
b.walls[3] = false;
|
||||
}
|
||||
|
||||
var y = a.j - b.j;
|
||||
if (y === 1){
|
||||
a.walls[0] = false;
|
||||
b.walls[2] = false;
|
||||
} else if (y === -1){
|
||||
a.walls[2] = false;
|
||||
b.walls[0] = false;
|
||||
}
|
||||
function removeWalls(a, b) {
|
||||
var x = a.i - b.i;
|
||||
if (x === 1) {
|
||||
a.walls[3] = false;
|
||||
b.walls[1] = false;
|
||||
} else if (x === -1) {
|
||||
a.walls[1] = false;
|
||||
b.walls[3] = false;
|
||||
}
|
||||
|
||||
var y = a.j - b.j;
|
||||
if (y === 1) {
|
||||
a.walls[0] = false;
|
||||
b.walls[2] = false;
|
||||
} else if (y === -1) {
|
||||
a.walls[2] = false;
|
||||
b.walls[0] = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user