I'm making a randomized board game where the path is generated dynamically. The data structure I'm using is a 2D Array, where "start" is defined and randomly finds the next available path (Up, Right, Down, Left). It uses a weighted random system where it has a "tendency" to go straight, following the last direction. After the "finish" is blocked off, or completes a certain number of walks, it stops generating.
I would use the "Maze Generating Algorithm" but I think it's not applicable in my case because my purpose isn't to make a maze, but traversal path.
My question is, how to "branch off" into new tracks and generate a new pathway, and have spacing between blocks?
For example, my runs are as follows:
I would also like for it to branch off into new paths at random. What kind of data structures is possible? I feel like 2D array's capabilities are not so great in generating this. Any help would be great.
This code is used for finding Next Path. Tuple is a class that contains Row and Col for positioning.
private void nextPath(){
// up, right, down, left
boolean availableNext [] = new boolean[] { false, false, false, false};
//up
if(isEmpty(new Tuple(this.lastPath.getRow() - 1, this.lastPath.getCol()))){
availableNext[0] = true;
//System.out.println("This ran");
}
//right
if(isEmpty(new Tuple(this.lastPath.getRow(), this.lastPath.getCol() + 1))){
availableNext[1] = true;
}
//down
if(isEmpty(new Tuple(this.lastPath.getRow() + 1, this.lastPath.getCol()))){
availableNext[2] = true;
}
//left
if(isEmpty(new Tuple(this.lastPath.getRow(), this.lastPath.getCol() - 1))){
availableNext[3] = true;
}
int numTrue = 0;
for(int i=0; i<availableNext.length; i++){
if(availableNext[i] == true){
numTrue ++;
}
//System.out.println(availableNext[i]);
}//end numTrueTest
if(numTrue == 0 || order == this.terminateNumber){
building = false;
this.im[lastPath.getRow()][lastPath.getCol()] = "Finish";
} else {
boolean chosenC = false;
while(!chosenC) {
int tempWeightedArr [] = new int[100];
int counter = 0;
for(int i=0; i<this.weightedDir.length; i++){
for(int j=0; j<this.weightedDir[i]; j++){
tempWeightedArr[counter] = i;
counter++;
}
}//end for
//int rnd = new Random().nextInt(availableNext.length);
int rnda = new Random().nextInt(tempWeightedArr.length);
int rnd = tempWeightedArr[rnda];
if(availableNext[rnd]){
chosenC = true;
//rnd = 0,1,2,3
if(rnd == 0){
this.im[this.lastPath.getRow() - 1][this.lastPath.getCol()] = Integer.toString(order);
this.lastPath = new Tuple(this.lastPath.getRow()-1, this.lastPath.getCol());
}
if(rnd == 1){
this.im[this.lastPath.getRow()][this.lastPath.getCol()+1] = Integer.toString(order);
this.lastPath = new Tuple(this.lastPath.getRow(), this.lastPath.getCol()+1);
}
if(rnd == 2){
this.im[this.lastPath.getRow() + 1][this.lastPath.getCol()] = Integer.toString(order);
this.lastPath = new Tuple(this.lastPath.getRow()+1, this.lastPath.getCol());
}
if(rnd == 3){
this.im[this.lastPath.getRow()][this.lastPath.getCol()-1] = Integer.toString(order);
this.lastPath = new Tuple(this.lastPath.getRow(), this.lastPath.getCol()-1);
}
for(int i=0; i<4; i++){
this.weightedDir[i] = 15;
}
this.weightedDir[rnd] = 55;
this.lastDir = rnd;
order++;
}//end if
}//end while
}//end else
}
Aucun commentaire:
Enregistrer un commentaire