xxxxxxxxxx
1612
//wall order = []
var grid = [];
var gridbackup = [];
var len = 20;
var a = 0;
var ylen = 19;
var b = 1;
var co = [];
var width = 800;
var pathfinder = [];
var pathreset;
var nodefinder = [];
var nodestack = [];
var nodehist = [];
var stack2 = [];
var toggles;
var turn;
var info;
var switches;
var completed;
//var height = 800;
//function resetSketch(){};
function Nodes(){};
var game;
var start;
var paths;
var target;
var targetbackup;
var pathfind;
var findnodes;
var v = width/(len);
var u = width -v/5;
var w = u/(len-2.8);
var toggle = 0;
var end = false;
var moves = 0;
var sides = 6;
var theta = 360/sides;
var nodes = [];
var current = [];
var stack = [];
var hist = [];
var start_array = [];
var scores = [];
var plants = [];
var rocks = [];
var animals = [];
function createGrid(){};
function createPlayers(){};
function home(){};
function setup(){
createCanvas(1100,660);
angleMode(DEGREES);
var ratio = w/2 * sin(theta);
var wx = round(width/ratio/2);
var r2 = w/ratio;
function createGrid(){
for (var j=0;j<ylen;j++){
var a = round(len/3);
for(var i=0;i<len;i++){
var k = pow(-1,j);
var b = w/2*sin(theta/2);
var x = ratio*1.5+(ratio*2-1)*i+ratio/2*k;
var y = (w/2)+j*(w-a-5);
var id = i+j*(len);
grid.push(new Cell(x,y,w,id,j,i));
gridbackup.push(new Cell(x,y,w,id,j,i));
}}
for(var i=0;i<grid.length;i++){
grid[i].init();
//gridbackup[i].init();
}
};
function createPlayers(a){
for (var i=0;i<a;i++){
var start = round(random(0,grid.length-1));
var x = round(random(0,len-1));
var y = round(random(0,ylen-1))
if(y==9&&(x>4&&x<15)){
y=y+1;
//x=x-1
}
current.push(grid[x+y*len]);
stack[i]=[];
hist[i]=[];
start_array.push(grid[x+y*len]);
scores[i]=[];
}
for(var i=0;i<current.length;i++){
var c1 = round(random(0,255));
var c2 = round(random(0,255));
var c3 = round(random(0,255));
var col = color(c1,c2,c3);
current[i].pcol = col;
start_array[i].pcol = col;
}
};
function initialize(a){
createGrid();
createPlayers(a);
};
function home(x,y,w){
for(var i=0;i<w;i++){
var a = grid[x+i+y*len];
a.visited = true;
a.id = 1000;
for(var j=0;j<a.walls.length;j++){
}
}
};
function pathfinders(a){
if(target){
target.pcol = targetbackup;
}
completed = false;
var r = round(random(0,grid.length-1));
for( var i=0;i< a;i++){
pathfinder.push(grid[r]);
stack2[i] = [];
}
var r = round(random(0,grid.length-1));
target = grid[r];
targetbackup = grid[r].pcol;
for( var i=0;i< pathfinder.length;i++){
var c1 = round(random(0,255));
var c2 = round(random(0,255));
var c3 = round(random(0,255));
var col = color(c1,c2,c3);
//pathfinder[i].visited = true;
pathfinder[i].pcol = col;
pathfinder[i].walls = [false,false,false,false,false,false];
}
}
function Nodes(array){
for(var i=0;i<array.length;i++){
// array[i].total = 0;
// array[i].neighbours = [];
array[i].pathNode();
}
for(var i=0;i<array.length;i++){
//array[i].findneighbour();
if(array[i].total!==2){
for(var j=0;j<array[i].total;j++){
nodefinder.push(grid[i]);
nodehist.push([]);
nodestack.push([]);
}
}
}
};
function randomizeMap(){
for(var i=0;i<grid.length-1;i++){
var r = round(random(0,grid.length-1));
grid[r].walls = [false,false,false,false,false,false];
//}
}
};
var dd;
function resetSketch(){
toggles = 0;
turn = 0;
current = [];
grid = [];
hist = [];
stack = [];
start_array = [];
nodefinder = [];
nodestack = [];
nodehist = [];
stack2 = [];
pathfinder = [];
target;
completed = false;
createGrid();
createPlayers(15);
home(5,9,10);
pathfinders(10);
};
function resetPath(){
completed = false;
nodefinder = [];
nodestack = [];
nodehist = [];
stack2 = [];
pathfinder = [];
target ;
grid = gridbackup;
pathfinders(10);
for(var i=0;i<grid.length;i++){
grid[i].visited2 = false;
}
};
function noded(){
nodefinder = [];
nodehist = [];
nodestack = [];
for (var i = 0; i < grid.length; i++) {
if(grid[i].total!==2){
grid[i].node = true;
}
grid[i].pathNode();
//grid[i].findneighbour();
grid[i].info();
if(info.toggle===1){
grid[i].showid();
}
if(switches.toggle===1){
grid[i].draw(sides);
}
}
Nodes(grid);
};
resetSketch();
//randomizeMap();
game = new button({
x:805,
y:10,
width:190,
height:30,
col:color(255,255,255,20),
label:"Map",
onClick: function(){
setups();
}
});
start = new button({
x:805,
y:10+40,
width:190,
height:30,
col:color(255,255,255,20),
label:"Start",
onClick: function(){
turns();
}
});
paths = new button({
x:805,
y:10+40*2,
width:95,
height:30,
col:color(255,255,255,20),
label:"Paths",
onClick: function(){
cellNeighbours();
}
});
reset = new button({
x:805,
y:10+40*8,
width:190,
height:30,
col:color(255,255,255,20),
label:"Reset",
onClick: function(){
resetSketch();
}
});
pathfind = new button({
x:805,
y:10+40*3,
width:160,
height:30,
col:color(255,255,255,20),
label:"Pathfinder",
onClick: function(){
findPath();
}
});
pathreset = new button({
x:805+160,
y:10+40*3,
width:30,
height:30,
col:color(255,255,255,20),
label:"X",
onClick: function(){
resetPath();
}
});
generateNodes = new button({
x:805,
y:10+40*4,
width:190,
height:30,
col:color(255,255,255,20),
label:"Generate Nodes",
onClick: function(){
noded(grid);
}
});
findnodes = new button({
x:805,
y:10+40*5,
width:190,
height:30,
col:color(255,255,255,20),
label:"Findnode",
onClick: function(){
findnode();
}
});
info = new button({
x:805,
y:10+40*6,
width:190,
height:30,
col:color(255,255,255,20),
label:"Info",
onClick: function(){
cellNeighbours();
}
});
switches = new button({
x:805+95,
y:10+40*2,
width:95,
height:30,
col:color(255,255,255,20),
label:"Walls On/Off",
onClick: function(){
setups();
}
});
};
function button(config){
this.x = config.x;
this.y = config.y;
this.w = config.width;
this.h = config.height;
this.col = config.col;
this.col2 = color(0);
this.label = config.label;
this.onClick = config.onClick||function(){};
this.toggle = 0;
this.stroke = 4;
this.draw = function(){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 5;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 4;
this.col2 = color(0)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x,this.y,this.w,30);
fill(255);
text(toggle,10,10);
fill(0);
strokeWeight(1);
text(this.label,this.x+20,this.y+20);
//text(this.toggle,this.x+170,this.y+20)
strokeWeight(0);
};
this.draw2 = function(){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 5;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 4;
this.col2 = color(0)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x,this.y,this.w,30);
fill(255);
text(toggle,10,10);
fill(0);
strokeWeight(1);
text(this.label,this.x+this.w/2-5,this.y+20);
//text(this.toggle,this.x+170,this.y+20)
strokeWeight(0);
};
this.checkmpos = function(){
return mouseX>this.x && mouseX <(this.x + this.w) && mouseY>this.y && mouseY <(this.y + this.h);
};
this.mouseClick = function(){
if(this.checkmpos()&&mouseClicked){
if(this.toggle===0){
this.toggle=1;
this.onClick();
this.col =color(255,255,255,200);
this.stroke = 5;
this.col2 = color(0,0,0,120);
}
else{
this.toggle=0;
this.stroke = 4;
this.col2 = color(0)
this.col =(255,255,255,80);
}
}
};
this.mouseClick2 = function(){
if(this.checkmpos()&&mouseIsPressed){
this.toggle=1;
this.stroke = 5;
}
else if(this.checkmpos()&&mouseReleased&&this.toggle===1||!this.checkmpos()&&this.toggle===1){
this.toggle=0;
this.onClick();
this.stroke = 4;
// this.col2 = color(0)
//this.col =(255,255,255,80);
}
};
};
function index (i,j){
if(i<0||i>len-1||j<0||j>ylen-1){
return undefined;
}
else{
return i+j*len;
}
};
function lShape (x,y,colour,array,object){
for(var k=0;k<sides;k++){
if(array[k]===true){
var thetab = theta * k+a;
var thetac = theta * k+theta+a;
stroke(colour);
strokeWeight(2);
line(x+w/2*sin(thetab),y+w/2*cos(thetab),x+w/2*sin(thetac),y+w/2*cos(thetac));
}else{
//object.total++;
noStroke();
}
}
};
function nodehistory(a,b){
if(a.length>0){
for(var i=0;i<a.length;i++){
if(b===a[i]){
b = false;
}
}
}
return b;
};
function addtonode(a,b){
for(var i=0;i<a.length;i++){
nodefinder.push(b);
nodehist.push([]);
nodestack.push([]);
}
};
function Cell (x,y,wid,id,ypos,xpos,col){
this.x = x;
this.y = y;
this.xpos = xpos;
this.ypos = ypos;
this.id = id;
this.oid = id;
this.nid = id;
this.walls = [];
this.edge = [];
//this.visited = false;
this.fpos = [];
this.col = color(0, 255, 234);
this.pcol = this.col;
this.col2 = color(0, 0, 0);
this.vertex = [];
this.length = 0;
this.t = 0;
this.n1 = -1;
this.n2 = -1;
this.neighbour = {};
this.neighbours = [];
this.total = 0;
this.path = [];
//this.visitedpath = false;
this.init = function(){
var theta = 360/sides;
for (var i=0;i<sides;i++){
var thetab = theta * i;
var thetac = theta * i+theta;
this.walls.push(true);
this.edge.push([this.x+w/2*sin(thetab),this.y+w/2*cos(thetab),this.x+w/2*sin(thetac),this.y+w/2*cos(thetac)]);
this.vertex.push([this.x+w/2*sin(thetac),this.y+w/2*cos(thetac)]);
this.length = dist(this.x+w/2*sin(thetab),this.y+w/2*cos(thetab),this.x+w/2*sin(thetac),this.y+w/2*cos(thetac));
}
//this.pcol = this.col3;
};
this.showid = function(){
if(info.toggle===1){
fill(255);
//text(this.id,this.x-10,this.y);
if(this.node){
text(this.neighbours.length,this.x-10,this.y);
}
text(this.nid,this.x,this.y);
}
strokeWeight(0);
};
this.draw = function(c) {
lShape(this.x,this.y,this.col2,this.walls,grid[this.oid]);
if(this.t===1){
this.visited = true;
}
if(this.visited ===true){
fill(255, 0, 0);
}
var b= this.walls;
if(b[0]&&b[3]^b[1]&&b[4]^b[2]&&b[5]^b[3]&&b[0]^b[4]&&b[1]^b[5]&&b[2]&&this.total>3){
//this.path = true;
}
else{
//this.path = false;
}
for(var i=0;i<this.neighbours.length;i++){
}
};
this.fill = function(){
beginShape();
for(var k=0;k<sides;k++){
var thetab = theta * k;
var thetac = theta * k+theta+a;
fill(this.col);
if (this.visited) {
fill(this.pcol);
}
if(this.visited2){
fill(this.pcol2);
}
noStroke();
vertex(this.x+w/2*sin(thetac),this.y+w/2*cos(thetac));
}
endShape();
};
this.highlight = function(){
beginShape();
for(var k=0;k<sides;k++){
noStroke();
var thetab = theta * k;
var thetac = theta * k+theta;
fill(0, 0, 255, 100);
vertex(this.x+w/2*sin(thetac),this.y+w/2*cos(thetac));
}
endShape();
};
this.checkNeighbors = function(){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var n = neighbours.length;
fill(255, 0, 0);
if(right){
//text(right.id,this.x,this.y);
}
if(topL&&!topL.visited&&topL.id!==1000){
topL.rpos = 3;
neighbours.push(topL);
}
if(topR&&!topR.visited&&topR.id!==1000){
topR.rpos = 2;
neighbours.push(topR);
}
if(right&&!right.visited&&right.id!==1000){
right.rpos = 1;
neighbours.push(right);
}
if(btmR&&!btmR.visited&&btmR.id!==1000){
btmR.rpos = 0;
neighbours.push(btmR);
}
if(btmL&&!btmL.visited&&btmL.id!==1000){
btmL.rpos = 5;
neighbours.push(btmL);
}
if(left&&!left.visited&&left.id!==1000){
left.rpos = 4;
neighbours.push(left);
}
var n = neighbours.length-1;
if(neighbours.length>0){
//text(neighbours.aid,this.x,this.y);
var r = round(random(0,n));
if(neighbours.length>1){
var s = round(random(0,n));
//this.n = n;
}
return neighbours[r];
}
else{
return undefined;
}
};
this.nthPass = function(){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var n = neighbours.length;
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
if(topL && this.id!==topL.id&&topL.id!==1000&&topL.score!==1&&this.score!==1){
topL.rpos = 3;
neighbours.push(topL);
}
if(topR && this.id!==topR.id&&topR.id!==1000&&topR.score!==1&&this.score!==1){
topR.rpos = 2;
neighbours.push(topR);
}
if(right && this.id!==right.id&&right.id!==1000&&right.score!==1&&this.score!==1){
right.rpos = 1;
neighbours.push(right);
}
if(btmR && this.id!==btmR.id&&btmR.id!==1000&&btmR.score!==1&&this.score!==1){
btmR.rpos = 0;
neighbours.push(btmR);
}
if(btmL && this.id!==btmL.id&&btmL.id!==1000&&btmL.score!==1&&this.score!==1){
btmL.rpos = 5;
neighbours.push(btmL);
}
if(left && this.id!==left.id&&left.id!==1000&&left.score!==1&&this.score!==1){
left.rpos = 4;
neighbours.push(left);
}
if(neighbours.length>0){
var r = round(random(0,neighbours.length-1));
this.n1 = neighbours[r].id;
neighbours[r].score = 1;
this.score = 1;
neighbours[r].n2 = this.id;
return neighbours[r];
}
else{
return undefined;
}
};
this.pathNode = function(){
if(paths.toggle===1||generateNodes.toggle===1){
var x = this.xpos;
var y = this.ypos;
var a = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
if(topL&&topL.walls[0]===false&&this.walls[3]===false){
a.push(topL);
this.dir = "nw";
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false){
a.push(topR);
this.dir = "ne";
}
if(right&&right.walls[4]===false&&this.walls[1]===false){
a.push(right);
this.dir = "e";
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false){
a.push(btmR);
this.dir = "se";
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false){
a.push(btmL);
this.dir = "s";
}
if(left&&left.walls[1]===false&&this.walls[4]===false){
a.push(left);
this.dir = "sw";
}
if (a.length > 0) {
this.total = a.length
this.connect(a);
}
}
strokeWeight(0);
};
this.connect = function(a){
if(this.total !==2){
strokeWeight(10);
stroke(0, 0, 0);
if(paths.toggle===1){
point(this.x,this.y);
}
}
else{
this.child = true;
}
for(var i=0;i<a.length;i++){
stroke(0);
strokeWeight(2);
if(paths.toggle===1){
line(this.x,this.y,a[i].x,a[i].y);
}
}
};
this.find = function (b,toggle){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var a = 0;
if(topL&&topL.walls[0]===false&&this.walls[3]===false&&!topL.visited2){
neighbours.push(topL);
a++;
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false&&!topR.visited2){
neighbours.push(topR);
}
if(right&&right.walls[4]===false&&this.walls[1]===false&&!right.visited2){
neighbours.push(right);
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false&&!btmR.visited2){
neighbours.push(btmR);
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false&&!btmL.visited2){
neighbours.push(btmL);
}
if(left&&left.walls[1]===false&&this.walls[4]===false&&!left.visited2){
neighbours.push(left);
}
var n = neighbours.length;
if(neighbours.length>0){
//text(neighbours.length,this.x,this.y);
var r = round(random(0,n));
var targeta = this.forward(neighbours,b);
var targetb = this.nearest(neighbours,b);
if(targeta){
line(this.x,this.y,b.x,b.y);
return targeta;
}
else{
line(this.x,this.y,b.x,b.y);
return targetb;
}
}
};
this.forward = function(a,k){
var b = grid[this.id];
var c;
var d1;
var d2;
for(var i=0;i<a.length;i++){
c = a[i];
d1 = dist(b.x,b.y,k.x,k.y);
d2 = dist(a[i].x,a[i].y,k.x,k.y);
if(d1>d2){
b = c;
}
else{
c = false;
}
}
return c;
};
this.nearest = function(a,k){
var b = a[0];
var c;
for(var i=1;i<a.length;i++){
c = a[i];
var d1 = dist(b.x,b.y,k.x,k.y);
var d2 = dist(c.x,c.y,k.x,k.y);
if(d1>=d2){
b = c;
}
else{
pathfinder.push(b);
stack2.push([]);
}
}
return b;
};
this.findnode = function(history){
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var a = 0;
if(topL&&topL.walls[0]===false&&this.walls[3]===false&&!topL.node){
neighbours.push(topL);
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false&&!topR.node){
neighbours.push(topR);
}
if(right&&right.walls[4]===false&&this.walls[1]===false&&!right.node){
neighbours.push(right);
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false&&!btmR.node){
neighbours.push(btmR);
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false&&!btmL.node){
neighbours.push(btmL);
}
if(left&&left.walls[1]===false&&this.walls[4]===false&&!left.node){
neighbours.push(left);
}
var n = [];
if(neighbours.length>0){
for(var i=0;i<neighbours.length;i++){
var r = nodehistory(history,neighbours[i]);
if(r){
n.push(r)
}
}
}
if(n.length>0){
// for(var i=0;i<n.length;i++){
// // nodefinder.push(grid[0]);
// // nodehist.push([]);
// // nodestack.push([]);
// }
var r = round(random(0,n.length-1));
return n[r];
}
else{
return undefined;
}
};
this.travelpath = function(){
var x = this.xpos;
var y = this.ypos;
var a = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
if(topL&&topL.walls[0]===false&&this.walls[3]===false){
if(topL.node){
a.push(topL);
this.dir = "nw";
}
else{
this.parent = topL;
}}
if(topR&&topR.walls[5]===false&&this.walls[2]===false){
if(topR.node){
a.push(topR);
this.dir = "ne";
}
else{
this.parent = topR;
}}
if(right&&right.walls[4]===false&&this.walls[1]===false){
if(right.node){
a.push(right);
this.dir = "e";
}
else{
this.parent = right;
}}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false){
if(topL.node){
a.push(btmR);
this.dir = "se";
}
else{
this.parent = btmR;
}}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false){
if(btmL.node){
a.push(btmL);
this.dir = "s";
}
else{
this.parent = topL;
}}
if(left&&left.walls[1]===false&&this.walls[4]===false){
if(left.node){
a.push(left);
this.dir = "sw";
}
else{
this.parent = topL;
}}
if (a.length>0) {
var r = round(random(0,a.length-1));
return a[r];
}
else{
return false;
}
};
this.histScore = function(){
//this.score = 0;
var x = this.xpos;
var y = this.ypos;
var neighbours = [];
//due to the offset introduced to make the hexagonal grid a slight adjustment needs to be made for the x values
var k = pow(-1,y);
if(k===1){
x=x+1;
}
var btmL = grid[index(x-1,y+1)];
var btmR = grid[index(x,y+1)];
var topL = grid[index(x-1,y-1)];
if(k===-1){
x=x-1;
}
if(k===1){
x=x-1;
}
var topR = grid[index(x+1,y-1)];
if(k===-1){
x=x+1;
}
var left = grid[index(x-1,y)];
var right = grid[index(x+1,y)];
var a = 0;
if(topL&&topL.walls[0]===false&&this.walls[3]===false&&this.id!==topL.id){
topL.rpos = 3;
neighbours.push(topL);
a ++;
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false&&this.id!==topR.id){
topR.rpos = 2;
neighbours.push(topR);
a ++;
}
if(right&&right.walls[4]===false&&this.walls[1]===false&&this.id!==right.id){
right.rpos = 1;
neighbours.push(right);
a ++;
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false&&this.id!==btmR.id){
btmR.rpos = 0;
neighbours.push(btmR);
a ++;
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false&&this.id!==btmL.id){
btmL.rpos = 5;
neighbours.push(btmL);
a ++ ;
}
if(left&&left.walls[1]===false&&this.walls[4]===false&&this.id!==left.id){
left.rpos = 4;
neighbours.push(left);
a ++;
}
var n = neighbours.length;
if (n > 0) {
fill(255, 0, 0);
this.score = 1;
}else{
}
};
this.hover = function(){
//noLoop();
var x = mouseX;var y = mouseY;
var a = this.edge;
for(var i=0;i<a.length;i++){
var x1 = a[i][0];
var y1 = a[i][1];
var x2 = a[i][2];
var y2 = a[i][3];
var d1 = dist(x,y,x1,y1);
var d2 = dist(x,y,x2,y2);
var d3 = dist(x1,y1,x2,y2);
var d4 = d1+d2;
var d5 = dist(this.x,this.y,x,y);
var dy = y1-y2;
var dx = x1-x2;
var dy2 = y2-y1;
var dx2 = x2-x1;
var dmx = x-x1;
var dmy = y-y1;
var dmx2 = x-x2;
var dmy2 = y-y2;
//fill(255, 0, 0);
var thetaXY1 = atan2(dy,dx);
var thetaXY2= atan2(dy2,dx2);
var thetaMXY2 = atan2(dmy,dmx);
var thetaMXY1 = atan2(dmy2,dmx2);
if((thetaXY1>=0&&thetaMXY1>=thetaXY1&&thetaMXY1>=0&&d1<=this.length&&d2<=this.length)||(thetaXY1<=0&&thetaMXY1<=0&&thetaMXY1>=thetaXY1&&d1<=this.length&&d2<=this.length)||(thetaXY2>=0&&thetaMXY2>=0&&thetaMXY2<=thetaXY2&&d1<=this.length&&d2<=this.length)||(thetaXY2<=0&&thetaXY2<=0&&thetaMXY2<=thetaXY2&&d1<=this.length&&d2<=this.length)||d5<=this.length/1.5){
//text("hello",this.x,this.y);
return true;
}
}
//loop();
};
this.checkp = function(){
if(this.hover()&&mouseOver){
this.col = color(29, 131, 240);
this.col2 = color(212, 8, 8);
}
else{
this.col = color(0, 204, 255);
this.col2 = color(162, 0, 255);
}
};
this.toggle = function(){
if(this.hover()&&mousePressed){
this.t++;
if(this.t===2){this.t=0;}
}
};
this.info = function(){
if(this.hover()&&mousePressed){
fill(255)
stroke(0)
rect(805,400,190,150)
fill(0)
text(this.walls,815,410);
text("Total" +" " + this.total,815,430);
text("Node" +" " + this.node,815,450);
text(this.neighbours.length,815,510);
text(this.oid,815,470);
for(var i=0;i<this.neighbours.length;i++){
text(this.neighbours[i][0].oid,815+30*i,490)
}
}
};
};
function check (a, b) {
return (a.length !== b.length) ? false :
!(a.some(function(row, index) {
return a[index] !== b[index];
}));
};
// this function has been amended from its previous iteration, the neighbor/neighbour function now updates its target cel with a reference position this.rpos which can be called to more easily locate the required walls to remove. Reference position will return a position from 0-n according to the number of sides, for the hexagon it returns 0-5 with 0 being the bottom right line and moving counter clockwise. 0 is paired with 3, 1 with 4, 2 with 5, 3 with 0, 4 with 1, and 5 with 2.
function removeWalls (a, b) {
if (b.rpos === 0) {
a.walls[0] = false;
b.walls[3] = false;
} else if (b.rpos === 1) {
a.walls[1] = false;
b.walls[4] = false;
} else if (b.rpos === 2) {
a.walls[2] = false;
b.walls[5] = false;
} else if (b.rpos === 3) {
a.walls[3] = false;
b.walls[0] = false;
} else if (b.rpos === 4) {
a.walls[4] = false;
b.walls[1] = false;
} else if (b.rpos === 5) {
a.walls[5] = false;
b.walls[2] = false;
}
};
function rebootPlayers(){
current = [];
if(len>20){
for (var i=0;i<current.length;i++){
start_array[i]= current.slice(i,1);
start_array[i].pcol = current[i].pcol;
}
}
else{
for (var i=0;i<start_array.length;i++){
start_array[i]= current.slice(i,1);
start_array[i].pcol = current[i].pcol;
}
}
};
function setups(){
if(game.toggle===1){
if(reset.toggle===1){
start.toggle=0
switches.toggle=0
game.toggle=0;
reset.toggle=0;
//paths.toggle=0;
pathfind.toggle=0;
}
for(var i=0;i<grid.length;i++){
grid[i].fill();
grid[i].hover();
if(switches.toggle===1){
grid[i].draw(sides);
}
grid[i].checkp();
grid[i].histScore();
}
fill(255);
text("PRESS LEFT MOUSE TO START",100,10);
for (var i = 0; i < current.length; i++) {
current[i].visited = true;
}
}
};
// grid.histscore is called before and populates each cell with a score. The function checks if a neighbouring cell contains the same id, and if that neighbouring cell has an open wall with the cell in question. If yes the cell score is updated to 1. A binary figure is the only thing thats required because here were just checking how many open ends each path has. On toggle 2 the program will select a random wall shared with a neighbour with a different id, remember that as a "current" cell moves it imprints its own id onto the new cell. "Note if you wanted yo know the original id this is stored in this.oid". So now some "currents" once returned to their starting position have an open wall, we now need another open wall to guarantee that the maze is completable. To accomplish this we step through the history array. A 2d array which stores all the histories of the "currents" and total the scores in each path. For a path to be open it should have a score of at least 2, meaning its connected to two other paths of different id's. If the score therefore is less than two we return true.
function checkhist(a){
var score = [];
for(var i=0;i<a.length;i++){
if(a[i].score>0){
score.push(a[i]);
//score ++;
}
}
if(score.length <3){
return true;
}else{
return false;
}
};
// This function is used to check if neighbouring cells have the same id. It takes an array and returns the neighbouring cell along with the original cell which produced the neighbour, this information can then be passed into the remove wall function.
function randomNth(a){
var t = [];
for(var i=0;i<a.length;i++){
var next = a[i].nthPass();
if(next){
t.push([a[i],next]);
}
}
if(t.length>0){
var r = round(random(0,t.length-1));
t[r][0].nthPass();
return t[r];
}else{return false;}
};
function turns(){
text(toggle,10,10);
//colorMode(HSB);
if(reset.toggle===1){
findnodes.toggle = 0;
start.toggle=0
switches.toggle=0
game.toggle=0;
generateNodes.toggle = 0;
//pathfind.toggle=0;
}
var tg = 0;
if(start.toggle===1){
for (var i = 0; i < current.length; i++) {
var next = current[i].checkNeighbors();
//current[i].checkNeighbors();
current[i].highlight();
if (next) {
next.id=current[i].id;
next.pcol=current[i].pcol;
next.visited = true;
// STEP 2
stack[i].push(current[i]);
hist[i].push(current[i]);
// STEP 3
removeWalls(current[i], next);
// STEP 4
current[i] = next;
}
else if (stack[i].length > 0) {
current[i] = stack[i].pop();
turn++;
}
}
}
var a = check(current,start_array);
if(a&&turn>0){
toggles=1;
turn=0;
}
if (toggles===1){
var a = check(current,start_array);
fill(255);
//text(a,10,50);
if(a){
for(var i=start_array.length-1;i>-1;i--){
current[i].highlight();
var nth = current[i].nthPass();
if(nth){
current[i].score = 1;
nth.score = 1;
//removeWalls(current[i],nth);
start_array.pop();
toggles++;
}else{
turn ++;
}}}
fill(255);
text(toggles,10,30);
}
//Histscore assigns a score to each cell based on its id. if it has an open wall shared with a neighbour of a different id, then it assigns a score of one. As the history is a two dimentional array checkhist is used to check each checking cell history path. It totals the scores of the history and if the score is less than 2 it returns true. If score equals 0, then that section of the maze is completely self contained and therefore inaccessible. if it is equal to one it means that it only has one open path. If there are to many sections with only one open path they can create sections of the maze which is shared by another with only one open path and again becaome self contained and inaccessible. So we need a score of 2 or more.
if(toggles>5){
var sc = 0;
var temp =[];
for (var i =0;i<hist.length;i++) {
var a = checkhist(hist[i]);
if(a){
temp.push(hist[i]);
}
}
// The checking cell histories with a score of less than 2 are pushed into the temp array. RandomNth is used to pick a random cell with a neighbour of a different id, and removeWalls is used to effect the removal of the wall. Temp is cleared to disable a loop.
for (var i= temp.length-1;i>-1; i--){
var pickCell = randomNth(temp[i]);
//var next = pickCell[0];
//pickCell = pickCell[1].nthPass();
if(pickCell){
removeWalls(pickCell[0],pickCell[1]);
temp[i].pop();
}
else{
temp[i].pop();
}}}
gridbackup = grid;
};
function cellNeighbours (){
if(reset.toggle===1){
start.toggle=0;
switches.toggle=0;
game.toggle=0;
//paths.toggle=0;
pathfind.toggle=0;
}
for (var i = 0; i < grid.length; i++) {
grid[i].pathNode();
// grid[i].findneighbour();
if(grid[i].visited2){
grid[i].draw();
grid[i].fill();
}
grid[i].info();
if(info.toggle===1){
grid[i].showid();
}
if(switches.toggle===1){
grid[i].draw(sides);
}
}
};
function findPath(a){
if(pathfind.toggle===1){
//pathreset.toggle = 0;
//target = a;
target.visited = true;
target.walls = [false,false,false,false,false,false];
target.pcol = color(255,0,0);
target.fill();
for( var i=0;i< pathfinder.length;i++){
pathfinder[i].fill();
//pathfinder[i].walls = [false,false,false,false,false,false];
if((pathfinder[i].y!==target.y||pathfinder[i].x!==target.x)&&completed!==true){
var next = pathfinder[i].find(target);
if(next){
pathfinder[i].highlight();
if(next!==target){
next.pcol2 = color(0,0,255,20);
next.visited2 = true;
next.fill();
next.draw();
}
stack2[i].push(pathfinder[i]);
pathfinder[i] = next;
}
else{
if(stack2[i].length>0){
pathfinder[i] = stack2[i].pop();
}
else{
text("No Path Found",10,10);
}
}
}
else{
fill(255);
text("success",10,10);
completed = true;
}
}
}
};
function findnode (){
if(findnodes.toggle===1){
var x;
for (var i=0;i<nodefinder.length;i++){
text(nodefinder.length,10,50)
var next = nodefinder[i].findnode(nodehist[i]);
nodefinder[i].highlight();
if(next){
nodestack[i].push(nodefinder[i]);
nodehist[i].push(nodefinder[i]);
nodefinder[i] = next;
}
else if(nodestack[i].length>0){
//nodefinder[i] = nodestack[i].pop();
}
}
}
};
//var buttons = [game.draw();,start.draw();,paths.draw();]
function phase_A(){
setups();
turns();
};
function draw(){
background(51);
phase_A();
findPath(grid[380]);
findnode();
cellNeighbours();
game.draw();
start.draw();
paths.draw();
reset.draw();
pathfind.draw();
pathreset.draw2();
generateNodes.draw();
findnodes.draw();
info.draw();
switches.draw();
reset.mouseClick2();
pathreset.mouseClick2();
generateNodes.mouseClick2();
};
function mouseClicked(){
};
function mouseOver(){
};
function mouseIsPressed(){
};
function mouseReleased(){
};
function mousePressed(){
game.mouseClick();
start.mouseClick();
paths.mouseClick();
reset.mouseClick2();
findnodes.mouseClick();
pathfind.mouseClick();
pathreset.mouseClick2();
generateNodes.mouseClick2();
switches.mouseClick();
info.mouseClick();
//toggle++;
for(var i=0;i<grid.length;i++){
//grid[i].toggle();
}
if(toggle===4){
toggle=0;
}
};