xxxxxxxxxx
2379
//wall order = []
var grid = [];
var total_players = 1;
var bank = false;
var xoffset = 0;
var yoffset = 0;
var ypadding = 45.5;
var padding;
var mousedown = false;
var menuxy = [805,50];
var xmap = 0;
var ymap = 0;
var offset = 0;
var offsetx;
var xx = 0;
var yy = 0;
var gridbackup = [];
var len = 5;
var a = 0;
//function updateLength(){};
var ylen = 2;
var b = 1;
var co = [];
//function resetSketch(){};
var width = 800;
var pathfinder = [];
var pathreset;
var nodefinder = [];
var nodebackup = [];
var nodestack = [];
var nodehist = [];
var stack2 = [];
var buttons = [];
var sliders = [];
var slidersminimize;
var slidersdrag;
function Nodes(){};
var toggles;
var turn;
var info;
var switches;
var completed;
var game;
var start;
var paths;
var menu;
var menudrag;
var target;
var targetbackup;
var pathfind;
var findnodes;
var load;
var minimize;
var sliderPlayers;
var slidersMenu;
var length;
var height;
var v = width/(len);
var u = width -v/5;
var w = u/(len-+offset);
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 start_backup = [];
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+xoffset;
var y = (w/2)+j*(w-a-ypadding)+yoffset;
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].visitedp = false;
array[i].node=undefined;
}
for(var i=0;i<array.length;i++){
if(array[i].total!==2){
nodefinder.push(grid[i]);
nodebackup.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];
//}
}
};
function resetSketch(){
// xx=0;
// yy=0;
xmap = 0;
ymap = 0;
toggles = 0;
turn = 0;
current = [];
grid = [];
hist = [];
stack = [];
start_array = [];
nodefinder = [];
nodebackup = [];
nodestack = [];
nodehist = [];
stack2 = [];
pathfinder = [];
target;
completed = false;
createGrid();
createPlayers(total_players);
if(bank){
home(5,9,10);
}
pathfinders(total_players);
};
function resetPath(){
completed = false;
stack2 = [];
pathfinder = [];
target ;
grid = gridbackup;
pathfinders(10);
for(var i=0;i<grid.length;i++){
grid[i].visited2 = false;
}
};
function noded(){
nodefinder = [];
nodebackup = []
nodehist = [];
nodestack = [];
Nodes(grid);
};
resetSketch();
function amend(){
// total_players = sliderPlayers.value;
if(sliderPlayers.value&&sliderPlayers.toggle===1){
total_players = sliderPlayers.value;
}
if(height.value&&height.toggle===1){
ylen = height.value;
if(ylen>6&&len>20){
bank=true;
}
}
if(length.value&&length.toggle===1){
len = length.value;
if(ylen>6&&len>20){
bank=true;
}
v = width/(len);
u = width -v/5;
w = u/(len-offset);
ratio = w/2 * sin(theta);
wx = round(width/ratio/2);
r2 = w/ratio;
}
if(offsetx.value&&offsetx.toggle===1){
offset = offsetx.value;
v = width/(len);
u = width -v/5;
w = u/(len-offset);
ratio = w/2 * sin(theta);
wx = round(width/ratio/2);
r2 = w/ratio;
}
if(padding.value&&padding.toggle===1){
if(ylen>6&&len>20){
bank=true;
}
ypadding = padding.value;
}
//grid = [];
resetSketch();
}
textSize(12);
//randomizeMap();
menu = new button({
x:menuxy[0]+xx,
y:menuxy[1]+yy-10,
width:190+12,
height:320,
col:color(255,255,255,20),
label:"",
onClick: function(){
setups();
}
});
game = new button({
x:menuxy[0]+xx,
y:menuxy[1]+yy,
width:190,
height:30,
col:color(255,255,255,20),
label:"Map",
onClick: function(){
setups();
}
});
start = new button({
x:menuxy[0]+xx,
y:menuxy[1]+40+yy,
width:190,
height:30,
col:color(255,255,255,20),
label:"Start",
onClick: function(){
turns();
}
});
paths = new button({
x:menuxy[0]+xx,
y:menuxy[1]+40*2+yy,
width:95,
height:30,
col:color(255,255,255,20),
label:"Paths",
onClick: function(){
cellNeighbours();
}
});
reset = new button({
x:menuxy[0]+xx,
y:menuxy[1]+40*7+yy,
width:190,
height:30,
col:color(255,255,255,20),
label:"Reset",
onClick: function(){
resetSketch();
}
});
pathfind = new button({
x:menuxy[0]+xx,
y:menuxy[1]+40*3+yy,
width:160,
height:30,
col:color(255,255,255,20),
label:"Pathfinder",
onClick: function(){
findPath();
}
});
pathreset = new button({
x:menuxy[0]+160+xx,
y:menuxy[1]+40*3+yy,
width:30,
height:30,
col:color(255,255,255,20),
label:"X",
onClick: function(){
resetPath();
}
});
generateNodes = new button({
x:menuxy[0]+xx,
y:menuxy[1]+40*4+yy,
width:190,
height:30,
col:color(255,255,255,20),
label:"Generate Nodes",
onClick: function(){
noded(grid);
}
});
findnodes = new button({
x:menuxy[0]+xx,
y:menuxy[1]+40*5+yy,
width:190,
height:30,
col:color(255,255,255,20),
label:"Findnodes",
onClick: function(){
//findnode();
}
});
info = new button({
x:menuxy[0]+xx,
y:menuxy[1]+40*6+yy,
width:190,
height:30,
col:color(255,255,255,20),
label:"Info",
onClick: function(){
cellNeighbours();
}
});
switches = new button({
x:menuxy[0]+95+xx,
y:menuxy[1]+40*2+yy,
width:95,
height:30,
col:color(255,255,255,20),
label:"Walls On/Off",
onClick: function(){
//setups();
}
});
menudrag = new button({
x:menuxy[0]+xx,
y:menuxy[1]+yy-10,
width:190,
height:10,
col:color(255,255,255,20),
label:"",
onClick: function(){
//setups();
},
id : 1
});
load = new button({
x:menuxy[0]+xx+4,
y:menuxy[1]+40*8+yy,
width:190,
height:30,
col:color(255,255,255,20),
label:"Load",
onClick: function(){
setups();
}
});
minimize = new button({
x:menuxy[0]+xx+190+2,
y:menuxy[1]+yy,
width:10,
height:10,
col:color(255,255,255,20),
label:"-",
onClick: function(){
hideMenu();
},
id: 1
});
var y = 560
var x = 90
var sw =6;
sliderPlayers = new button({
x:x,
y:y,
width:190,
height:sw,
col:color(255,255,255,20),
label:"No of Players",
value:1,
onClick: function(){
amend();
}
});
length = new button({
x:x,
y:y+14*1,
width:190,
height:sw,
col:color(255,255,255,20),
label:"XLen",
onClick: function(){
amend();
}
});
slidersMenu = new button({
x:x,
y:y-10,
width:190,
height:sw,
col:color(255,255,255,20),
label:"",
onClick: function(){
//setups();
}
});
height = new button({
x:x,
y:y+14*3,
width:190,
height:sw,
col:color(255,255,255,20),
label:"Ylen",
onClick: function(){
amend();
}
});
offsetx = new button({
x:x,
y:y+14*4,
width:190,
height:sw,
col:color(255,255,255,20),
label:"X Offset",
onClick: function(){
amend();
}
});
slidersminimize = new button({
x:x,
y:y+14*4,
width:190,
height:sw,
col:color(255,255,255,20),
label:"-",
onClick: function(){
amend();
}
});
slidersdrag = new button({
x:x,
y:y+14*4,
width:190,
height:sw,
col:color(255,255,255,20),
label:"-",
onClick: function(){
amend();
}
});
padding = new button({
x:x,
y:y+14*5,
width:190,
height:sw,
col:color(255,255,255,20),
label:"Y Padding",
onClick: function(){
amend();
}
});
buttons = [menu,game,start,paths,reset,pathfind,pathreset,generateNodes,findnodes,info,switches,load,sliderPlayers,minimize,menudrag,slidersMenu,slidersdrag,slidersminimize];
sliders = [sliderPlayers,length,height,offsetx,padding];
for(var i=0;i<sliders.length;i++){
if(sliders[i].toggle ===1){
resetSketch(total_players);
}}
};
function button(config){
this.x = config.x;
this.y = config.y;
this.w = config.width;
this.h = config.height;
this.col = config.col;
this.id = config.id;
this.col2 = color(0);
this.label = config.label;
this.onClick = config.onClick||function(){};
//this.value = config.value;
this.toggle = 0;
this.toggle2 = 0;
this.bw = 6;
this.bh = 6;
this.bxo = 0;
this.bxo1 = this.x;
this.stroke = 4;
this.textSize = 12;
this.visible = true;
this.this = this;
this.slide = function(){
if(this.buttonPos&&mouseIsPressed){
this.value = abs(round(map(mouseX,this.x,this.x+this.w,0,40)));
}
}
this.draw = function(){
if(this.y+this.h<menu.h+menu.y+4+10&&this.y+4>menu.y){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 0;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 0;
this.col2 = color(0)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x+xx,this.y+yy,this.w,this.h);
fill(255);
text(toggle,10,10);
fill(0);
strokeWeight(1);
textSize(this.textSize);
text(this.label,this.x+20+xx,this.y+20+yy);
//text(this.toggle,this.x+170,this.y+20)
strokeWeight(0);
}
};
this.draw2 = function(){
if(this.y+this.h<menu.h+menu.y+4&&this.y>menu.y-10){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 0;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 0;
this.col2 = color(0)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x+xx,this.y+yy,this.w,30);
fill(255);
text(toggle,10,10);
fill(0);
strokeWeight(1);
textSize(this.textSize);
text(this.label,this.x+this.w/2-5+xx,this.y+20+yy);
//text(this.toggle,this.x+170,this.y+20)
strokeWeight(0);
}
};
this.draw3 = function(){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 0;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 0;
this.col2 = color(0)
this.col =(255,255,255,80);
}
fill(this.col);
stroke(this.col2);
strokeWeight(this.stroke);
rect(this.x+xx,this.y+yy,this.w,this.h);
fill(255);
text(toggle,10,10);
fill(0);
strokeWeight(2);
textSize(this.textSize);
text(this.label,this.x+this.w/2-5+xx+2,this.y+20+yy-10);
//text(this.toggle,this.x+170,this.y+20)
strokeWeight(0);
};
this.draw4 = function(){
if(this.toggle===1){
this.col =color(255,255,255,200);
this.stroke = 0;
this.col2 = color(0,0,0,120);
}
if(this.toggle===0){
this.stroke = 0;
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,this.h);
//this.bxo1 = this.x;
fill(255)
text(this.label,this.x-80,this.y+5)
if(this.toggle===1){
rect(this.bxo,this.y-1,this.bw+2,this.bh+2);
this.bxo1 = this.bxo;
}
else{
rect(this.bxo1,this.y-1,this.bw,this.bh);
}
fill(255);
//if(this.toggle===1){
var k = round(map(this.bxo1,this.x,this.x+this.w,0,40));
text(k,this.x+this.w,this.y);
//}
fill(0);
strokeWeight(2);
strokeWeight(0);
};
this.checkmpos = function(){
if(this.id===1&&!switches.checkmpos()){
return mouseX>this.x+xx && mouseX <(this.x + this.w+xx) && mouseY>this.y+yy && mouseY <(this.y + this.h+yy);
}else{
if(this.y+this.h<menu.h+menu.y+4&&this.y+4>menu.y){
return mouseX>this.x+xx && mouseX <(this.x + this.w+xx) && mouseY>this.y+yy && mouseY <(this.y + this.h+yy);
}}
};
this.hover = function(){
if(this.checkmpos2()){
this.textSize += 0.5;
this.col2 = color(255);
this.col = color(255);
}
else{
this.textSize = 12;
}
if(this.textSize>=14){
this.textSize = 14;
}
return true;
};
this.checkmpos2 = function(){
return mouseX>this.x+xx && mouseX <(this.x + this.w+xx) && mouseY>this.y+yy && mouseY <(this.y + this.h+yy);
};
this.mouseClick = function(){
if(this.y+this.h<menu.h+menu.y+4+10&&this.y+4>menu.y){
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.y+this.h<menu.h+menu.y+4+10&&this.y+4>menu.y){
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.mouseClick3 = function(){
if(this.y+this.h<menu.h+menu.y+4+10&&this.y+4>menu.y){
if(this.checkmpos()&&mouseIsPressed&&!mousedown){
this.toggle=1;
}
else if(mouseReleased){
this.toggle=0;
}
}
};
this.mouseClick4 = function(){
if(this.checkmpos()&&mouseClicked){
if(this.toggle===0){
this.toggle = 1;
}
else{
this.toggle=0;
menu.toggle2=0;
}}
};
this.mouseClick5 = function(){
if(this.checkmpos2()&&mouseIsPressed){
this.toggle=1;
this.stroke = 5;
}
else if(this.checkmpos2()&&mouseReleased&&this.toggle===1||!this.checkmpos2()&&this.toggle===1){
this.toggle=0;
this.stroke = 4;
}
};
this.buttonPos = function(){
var a;
for(var i=0;i<sliders.length;i++){
if(sliders[i].toggle===1&&sliders[i]!==this){
a = 1;
}
}
if(mouseX>this.x&&mouseX<this.x+this.w&&mouseY>this.y-2&&mouseY<this.y+this.h+2&&a!==1){
return true;
}
}
this.buttonClick = function(){
if(this.buttonPos()&&mouseIsPressed){
this.toggle=1;
mousedown = true;
}
if(this.toggle ===1&&mouseDragged&&mouseIsPressed){
//mousedown = false;
this.onClick();
if(this.toggle===1&&mouseX<this.x+this.w&&mouseX>this.x){
this.bxo = mouseX
}
this.bxo = mouseX;
if(mouseX> this.x+this.w){
this.bxo = this.x+this.w;
}
if(mouseX< this.x){
this.bxo = this.x;
}}
//resetSketch()
};
if(this.toggle===0){
mousedown = false;
}
};
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{
noStroke();
}
}
};
function nodehistory(a,b){
var c;
if(a.length>0){
c=b;
for(var i=0;i<a.length;i++){
if(c!==a[i]){
}
else{
b = false;
}
}}
else{
return b;
}
return b;
};
function hideMenu(){
var t = 3;
var height = menu.h;
if(minimize.toggle===1){
t++;
menu.h+=-30;
if(menu.h<=0){
menu.h = 0;
menu.toggle2=1;
}
}
if(minimize.toggle===0){
t++;
menu.h+=30;
if(menu.h>=320){
menu.h = 320;
menu.toggle2=0;
}
}
noStroke();
fill(51)
//rect(menuxy[0],menuxy[1],menuxy[0]+190,menuxy[1]+400);
};
function Cell (x,y,wid,id,ypos,xpos,col){
this.x = x+xmap;
this.y = y+ymap;
this.z = 0;
this.xpos = xpos;
this.ypos = ypos;
this.id = id;
this.oid = id;
this.nid = id;
this.walls = [];
this.edge = [];
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.node = false;
this.parent = [];
// for(var i=0 ;i<2;i++){
// this.parent[i] = [];
//
// }
// var x = 10;
// var x = this.id.toString();
// this.array = {}
// this.array[x] = false;
//this.array[this.var]
//this.visitedp = 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.showid = function(){
if(info.toggle===1){
fill(255);
text(this.score2,this.x,this.y);
}
strokeWeight(0);
};
this.draw = function(c) {
lShape(this.x,this.y,this.col,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;
}
strokeWeight(0);
};
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&&paths.toggle===1){
strokeWeight(10);
stroke(0, 0, 0);
point(this.x,this.y);
}
for(var i=0;i<a.length;i++){
stroke(0);
strokeWeight(2);
if(this.total !==2&&paths.toggle===1||generateNodes.toggle===1){
this.neighbours[i] = [a[i]];
}
if(this.visitedp===true&&findnodes.toggle===1){
//stroke(255);
}
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);
var x = topR.id;
this[x].id = 0;
}
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;
for(var i=0;i<a.length;i++){
c = a[i];
var d1 = dist(b.x,b.y,k.x,k.y);
var 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,backup,steps){
// 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 = [];
// if(topL&&topL.walls[0]===false&&this.walls[3]===false){
// var next = nodehistory(history,topL);
// if(topL.node&&next){
// topL.neighbours.push([backup,steps]);
// history.push(next);
// return;
// }
// else if(next){
// neighbours.push(next);
// history.push(next);
// }}
// if(topR&&topR.walls[5]===false&&this.walls[2]===false){
// var next = nodehistory(history,topR);
// if(topR.node&&next){
// topR.neighbours.push([backup,steps]);
// return;
// }
// else{
// if(next){
// neighbours.push(next);
// history.push(next);
// }}}
// if(right&&right.walls[4]===false&&this.walls[1]===false){
// var next = nodehistory(history,right);
// if(right.node&&next){
// right.neighbours.push([backup,steps]);
// history.push(next);
// return;
// }
// else{
// if(next){
// neighbours.push(next);
// history.push(next);
// }}}
// if(btmR&&btmR.walls[3]===false&&this.walls[0]===false){
// var next = nodehistory(history,btmR);
// if(btmR.node&&next){
// next.visitedp = true;
// btmR.neighbours.push([backup,steps]);
// history.push(next);
// return;
// }
// else{
// if(next){
// neighbours.push(next);
// history.push(next);
// }}}
// if(btmL&&btmL.walls[2]===false&&this.walls[5]===false){
// var next = nodehistory(history,btmL);
// if(btmL.node&&next){
// next.visitedp = true;
// btmL.neighbours.push([backup,steps]);
// history.push(next);
// return;
// }
// else{
// if(next){
// neighbours.push(next);
// history.push(next);
// }}}
// if(left&&left.walls[1]===false&&this.walls[4]===false){
// var next = nodehistory(history,left);
// if(left.node&&next){
// next.visitedp = true;
// left.neighbours.push([backup,steps]);
// history.push(next);
// return;
// }else{
// if(next){
// neighbours.push(next);
// history.push(next);
// }}}
// var a = [];
//
//
// if(neighbours.length>0){
// var r = round(random(0,neighbours.length-1))
// return neighbours[r];
// if(neighbours.length>1){
// for(var i=0;i<neighbours.length;i++){
// if(neighbours[i].node){
// neighbours[i].neighbours.push([backup,steps]);
// //this.neighbours.push([]);
// }
// nodefinder.push(backup);
// nodebackup.push(backup);
// nodehist.push(history);
// nodestack.push([]);
// }
// }
// }
// else{
// return undefined;
// }
// };
this.findnode = function(){
if(findnodes.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){
if(topL.node&&!this.node&&this.parent[0]!==topL){
if(this.parent[0]){
this.parent[1] = [topL]
}
else{
this.parent[0] = [topL];
this.step = 1;
}
}
}
if(topR&&topR.walls[5]===false&&this.walls[2]===false){
if(topR.node&&!this.node&&this.parent[0]!==topR){
if(this.parent[0]){
this.parent[1] = [topR]
}
else{
this.parent[0] = [topR];
this.step = 1;
}
}
}
if(right&&right.walls[4]===false&&this.walls[1]===false){
if(right.node&&!this.node&&this.parent[0]!==right){
if(this.parent[0]){
this.parent[1] = [right]
}
else{
this.parent[0] = [right];
this.step = 1;
}
}
}
if(btmR&&btmR.walls[3]===false&&this.walls[0]===false){
if(btmR.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==btmR){
this.parent[1] = [btmR];
}
else{
this.parent[0] = [btmR];
this.step = 1;
}
}
}
if(btmL&&btmL.walls[2]===false&&this.walls[5]===false){
if(btmL.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==btmL){
this.parent[1] = [btmL];
}
else{
this.parent[0] = [btmL];
this.step = 1;
}
}
}
if(left&&left.walls[1]===false&&this.walls[4]===false){
if(left.node&&!this.node){
if(this.parent[0]&&this.parent[0]!==left){
this.parent[1] = [left];
}
else{
this.parent[0] = [left];
this.step = 1;
}
}
}
if(this.parent[0]&&this.parent[1]&&this.parent[0][0]===this.parent[1][0]||!this.parent[1]){
this.parent[1] = 0
}
if(!this.node&&!this.neighbours[0][0].node&&!this.parent[0]&&this.neighbours[0][0].parent[0]){
//for(var i=0;i<2;i++){
this.parent[0] = this.neighbours[0][0].parent[0];
//this.parent[1] = []
//this.parent[1][0] = [];
}
if(!this.node&&!this.neighbours[1][0].node&&!this.parent[0]&&this.neighbours[1][0].parent[0]){
//for(var i=0;i<2;i++){
this.parent[0] = this.neighbours[1][0].parent[0];
this.step = this.neighbours[1][0].parent[0].step+1
//this.parent[1] = [];
//this.parent[1][0] = [];
}
if(!this.node&&!this.neighbours[0][0].node&&this.parent[0]&&!this.parent[1]&&this.neighbours[0][0].parent[0]&&this.neighbours[0][0].parent[0]!==this.parent[0]){
//for(var i=0;i<2;i++){
//this.parent[1] = []
this.parent[1] = this.neighbours[0][0].parent[0];
this.step = this.neighbours[0][0].parent[0].step+1
//this.parent[1] = []
}
if(!this.node&&this.neighbours[1][0].node&&this.parent[0]&&this.neighbours[1][0].parent[1]!==undefined&&this.neighbours[1][0].parent[0]!==this.parent[0]){
//for(var i=0;i<2;i++){
//this.parent[1] = []
this.parent[1] = this.neighbours[1][0].parent[0];
//this.step = this.neighbours[1][0].parent[0].step+1
//this.parent[1] = []
}
}
strokeWeight(0);
};
// this.findnode = function(){
//
// if(findnodes.toggle===1){
//
// for(var i=0;i<this.neighbours.length-1;i++){
// //for(var j=0;j<this.neighbours[i].length;j++){
//
// if(!this.node&&!this.neighbours[i][0].node){
// //this.parent =[this.neighbours[i][0]]
// fill(255)
// text(this.parent[i][0].oid,this.x+20*i,this.y);
// this.steps = 1;
// //}
// }
// }
//
//
// }
//
// strokeWeight(0);
//
// };
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(){
var x = mouseX-xmap;var y = mouseY-ymap;
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;
}
}
};
this.ppos = function(){
};
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()&&mouseClicked){
this.t++;
if(this.t===2){this.t=0;}
}
};
this.info = function(){
if(xx<0){
xmap = -xmap;
}
if(ymap<0){
ymap = -ymap;
}
var a = 815-xmap;
var b = 410-ymap;
if((this.hover()&&mousePressed&&(game.toggle===1||paths.toggle===1||start.toggle)||this.toggle===1)&&!mousedown){
fill(255)
stroke(0)
rect(a,b-10,190,210)
fill(0)
text(this.walls,a,b);
text("Total" +" " + this.total,a,b+20);
text("Node" +" " + this.node,a,b+20*2);
text("Node" +" " + this.nodeNode,a+90,b+20*2);
text("Id" +" " +this.oid,a,b+20*3);
text("Cluster " +" " +this.id,a,b+20*4);
text("Neighbours" +" " +this.neighbours.length,a,b+20*5);
for(var i=0;i<this.neighbours.length;i++){
if(this.neighbours.node&&!this.node){
//this.parent = this.neighbours[i];
}
text(this.neighbours[i][0].oid,a+30*i,b+20*6)
}
text("Toggle " +" " +this.t,a,b+20*7);
//if(this.visited){
text("Visited:" + " " + this.visited,a,b+20*8);
//}
text("Visitedp:" +" " + this.visitedp,a,b+20*9);
if(this.parent[0]){
for(var i=0;i<2;i++){
stroke(51);
strokeWeight(0)
if(this.parent[i][0]!==undefined){
text("Parent:" +" " +this.parent[i][0].oid+" "+ this.step,a+70*i,b+20*10);
}
}
}
// if(!this.node&&!this.neighbours.node&&this.neighbours.parent[0]){
// //this.parent[0][0] = this.neighbours[0][0].parent[1][0]
// }
}
};
};
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(){
for(var i=0;i<grid.length;i++){
grid[i].hover();
grid[i].pathNode();
if(grid[i].total!==2&&grid[i].neighbours.length>0){
grid[i].node = true;
//grid[i].visitedp = true;
}
}
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 = [];
var score2 = [];
for(var i=0;i<a.length;i++){
if(a[i].score>0){
score.push(a[i]);
}
if(score2>0){
score2.push(a[i]);
}
}
if(score.length<3||score2.length<2){
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();
t[r][0].score2 = 1;
t[r][1].score2 = 1;
return t[r];
}else{return false;}
};
function turns(){
text(toggle,10,10);
if(reset.toggle===1){
findnodes.toggle = 0;
start.toggle=0
switches.toggle=0
game.toggle=0;
generateNodes.toggle = 0;
//pathfind.toggle=0;
}
if(start.toggle===1){
for (var i = 0; i < current.length; i++) {
var next = 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;
}
//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===1){
var temp =[];
for (var i =hist.length-1;i>-1;i--) {
var a = checkhist(hist[i]);
if(a){
temp.push(hist[i]);
//hist[i].pop();
}
}
// 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]);
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].info();
grid[i].findnode();
if(info.toggle===1){
grid[i].showid();
}
if(switches.toggle===1){
grid[i].draw(sides);
}
}
};
function findPath(a){
if(pathfind.toggle===1){
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 findnode2 (){
if(findnodes.toggle===1){
for(var i=0;i<nodehist.length;i++){
fill(255);
if(nodehist[i][0]){
text(nodehist[i].length,10+30*i,50);
}
}
var t = 0;
for (var i=0;i<nodefinder.length;i++){
//while(nodebackup[i].neighbours.length!==nodebackup[i].total){
text(nodefinder.length,10,50)
var next = nodefinder[i].findnode(nodehist[i],nodebackup[i],t);
nodefinder[i].highlight();
if(next){
t++;
next.visitedp = true;
nodestack[i].push(nodefinder[i]);
nodehist[i].push(nodefinder[i]);
nodefinder[i] = next;
}
else if(nodestack[i].length>0){
t=0;
//nodebackup[i].neighbours.push([]);
nodefinder[i] = nodestack[i].pop();
}}
}
};
function trace (gparent,parent){
var array = null;
var gchild = null;
for(var i=0;i<parent.neighbours.length;i++){
if(gparent!==parent.neighbours[i][0]){
array = true;
gchild = parent.neighbours[i][0];
}}
if(array){
fill(255)
//text(gparent.oid,gchild.x,gchild.y);
return gchild;
}
};
function updateNodes (parent,child,steps){
var array = [];
var path = null;
//parent.nodeNode = true;
for(var i=0;i<child.neighbours.length;i++){
if(parent===child.neighbours[i][0]){
path = child.neighbours[i][0];
child.neighbours[i].push([path,steps])
}}
}
// function findnode(){
// if(findnodes.toggle===1){
// var steps;
// for(var i=0;i<nodefinder.length;i++){
// var a ;
// for(var j=0;j<nodefinder[i].neighbours.length;j++){
// var parent = nodefinder[i];
// nodefinder[i].visitedp = true;
// var child = parent.neighbours[j][0];
// var next = trace(nodefinder[i],child);
// nodefinder[i].highlight();
// if(next){
// child.visitedp = true;
//
// if(!next.node&&next.neighbours.length===2){
// steps++;
// // nodehist.push(next);
// nodestack[i].push(nodefinder[i]);
// nodefinder[i] = next;
// //child = nodefinder[i].neighbours;
// }
// else{
// fill(255);
// text("Node",next.x,next.y);
// if(next.node&&next.neighbours.length!==2){
// next.nodeNode = true;
// }
// updateNodes(parent,child,steps) ;
// }
// }
// else if(nodestack[i].length >0){
// fill(255);
// text("no more paths",parent.x,parent.y);
// //nodefinder[i] = nodestack[i].pop();
// //nodefinder[i] = nodefinder[i][0]
// }}
// }
// }
// };
function checkToggles(){
for(var i=0;i<sliders.length;i++){
if(sliders[i].toggle===1)
return true;
}
}
function draw(){
push();
translate(xmap,ymap)
background(51);
setups();
turns();
findPath();
//findnode();
cellNeighbours();
pop();
minimize.draw3();
hideMenu();
game.draw();
start.draw();
paths.draw();
reset.draw();
pathfind.draw();
pathreset.draw2();
generateNodes.draw();
findnodes.draw();
info.draw();
switches.draw();
if(!mousedown){
reset.mouseClick2();
pathreset.mouseClick2();
generateNodes.mouseClick2();
menudrag.mouseClick2();
}
menu.mouseClick3();
//sliderPlayers.draw4();
menudrag.draw3();
//sliderPlayers.buttonClick();
if(!mousedown){
menudrag.mouseClick5();
}
//length.draw4();
for(var i=0;i<sliders.length;i++){
sliders[i].draw4();
//if(!mousedown){
sliders[i].slide();
sliders[i].buttonClick();
//}
}
for(var i=0;i<buttons.length;i++){
if(!mousedown){
buttons[i].hover();
}
}
};
function mouseClicked(){
if(game.toggle===1||paths.toggle===1){
for(var i=0;i<grid.length;i++){
grid[i].toggle();
}}
};
function mouseOver(){
};
function mouseIsPressed(){
};
function mouseReleased(){
mousedown = false;
for(var i=0;i<sliders.length;i++){
sliders[i].toggle = 0;
}
};
function mouseDragged(){
if(mousedown&&(menudrag.toggle!==1||slidersdrag.toggle!==1)&&!checkToggles()){
xmap += mouseX-pmouseX;
ymap += mouseY-pmouseY;
}
if(menudrag.toggle===1){
mousedown = true;
xx = mouseX-menuxy[0]-190/2;
yy = mouseY-menuxy[1];
}
if(slidersdrag.toggle===1){
mousedown = true;
xx = mouseX-menuxy[0]-190/2;
yy = mouseY-menuxy[1];
}
};
function mousePressed(){
if(!menu.checkmpos()){
mousedown = true;
}
game.mouseClick();
start.mouseClick();
paths.mouseClick();
findnodes.mouseClick();
pathfind.mouseClick();
switches.mouseClick();
info.mouseClick();
minimize.mouseClick4();
slidersminimize.mouseClick4();
//menudrag.mouseClick5();
for(var i=0;i<sliders.length;i++){
//sliders[i].buttonClick();
}
if(!mousedown){
reset.mouseClick2();
pathreset.mouseClick2();
generateNodes.mouseClick2();
}
};