The current code I’m testing:
Things to note:
The variable City is an int[,] already predefined.
The number 1 signifies a ‘wall’ i don’t want to cross.
The number 0 is an empty space.
I am trying to fill with the number 10.
function FillCity(){
var stack:List.<Vector2> = new List.<Vector2>();
var x:int;
var y:int;
var counter:uint = 1;
//Starting point
stack.Add(new Vector2(0,0));
while(stack.Count != 0){
var point:Vector2 = stack[0];
y = point[1];
//If line is going right
if(point[0]-1<0 || City[point[0]-1,y]==1){
print("Going right ->");
for(x=point[0]; x<City.GetLength(0);x++){
if(City[x,y]==10){
break;
} else if (City[x,y]==1 || x == City.GetLength(0)-1){
if(x == City.GetLength(0)-1){
City[x,y] = 10;
}
//Check up
for(var x1:int = x; x1>-1;x1--){
var y1:int = parseInt(Mathf.Clamp(y+1,0,City.GetLength(1)-1));
if(City[x1,y1]==10){
break;
} else if(City[x1,y1]==0){
stack.Add(new Vector2(x1,y1));
print(point+" placed "+x1+" "+y1+" going right and up.");
break;
}
print("Checking "+x1+" "+y1);
}
print("Done checking up.");
//Check down
for(x1 = x; x1>-1;x1--){
y1 = parseInt(Mathf.Clamp(y-1,0,City.GetLength(1)-1));
if(City[x1,y1]==10){
break;
} else if(City[x1,y1]==0){
stack.Add(new Vector2(x1,y1));
print(point+" placed "+x1+" "+y1+" going right and down.");
break;
}
}
print("Done checking down.");
break;
} else {
City[x,y] = 10;
}
}
}
//If line is going left
if(point[0]+1>City.GetLength(0)-1 || City[point[0]+1,y]==1){
print("Going left <-");
for(x=point[0]; x>-1;x--){
if(City[x,y]==10){
break;
} else if (City[x,y]==1 || x == 0){
if(x == 0){
City[x,y] = 10;
}
//Check up
for(x1 = x1; x<City.GetLength(0);x1++){
y1 = parseInt(Mathf.Clamp(y+1,0,City.GetLength(1)-1));
if(City[x1,y1]==10){
break;
} else if(City[x1,y1]==0){
stack.Add(new Vector2(x1,y1));
print(point+" placed "+x1+" "+y1+" going left and up.");
break;
}
}
print("Done checking up.");
//Check down
for(x1 = x; x1<City.GetLength(0);x1++){
y1 = parseInt(Mathf.Clamp(y-1,0,City.GetLength(1)-1));
if(City[x1,y1]==10){
break;
} else if(City[x1,y1]==0){
stack.Add(new Vector2(x1,y1));
print(point+" placed "+x1+" "+y1+" going left and down.");
break;
}
}
print("Done checking down.");
break;
} else {
City[x,y] = 10;
}
}
}
print("Just did: "+point);
if(counter%2000==0){
print("Stopped");
print(stack.Count+" left.");
return;
}
stack.RemoveAt(0);
counter++;
}
}
Excuse the mass amount of print() lines, I’m trying to find all the problems and it’s easier this way.
Currently my problem is that the fill is stopping prematurely as it fails to find any more lines to fill.
Again, any other approaches are welcomed!