- Home /
Drunkard Walk Algorithm C# Question
Hi dear Unity's community! I've been away for a lot, but I spent that time trying and studying by myself the mysterious world of coding, lurking here and there, reading your kind answers to questions already asked that were useful to fulfill my needs!
I'm now here once more to subject to you my last achievement in this field, trusting in your unselfish dedication to these subjects and hoping to hear helpful advices. Before I continue, I think I should justify my decision to talk about this by opening a question... well, I wanted to make known the method I used to help other amateurs in their own quests for knowledge, but I also wanted to make clear that what follows is by no means finished or " correct". Thus I preferred unityAnswers to the Forums.
So, I wanted to use Procedural Generation of Contents. I learned about Drunkard Walk which is basically what I thought to do by myself, except that somebody else already thought about it. What follows is my own personal attempt to make such an algorithm. So far it doesn't stop by itself and is unable to " think" too much... it " just" build up a map of squares. Squares, yes, because I've attached this script to a plane with a box collider in my editor and I've got another script attached to a GameMaster that instantiate the first square from which to start walking.
Here it is, in all his unholy ignorance!
e's ROUGH Drunkard Square Generator
using UnityEngine; using System.Collections; using System.Collections.Generic;//List Enabled
public class DrunkardSquareGenerator : MonoBehaviour {
// This class will handle the algorithms for my Drunkard Walk Procedural Generated Content
/*
* I originally thought to store all the functions here, but then I tried to separate them in other scripts.
* I'm going to put them back here once more... I kinda miss some basics in software architecture.
* Problem is, I thought to use a List to handle all this stuff, instead of a sequence of IF statements, BUT
* I wasn't capable of doing it because I lack the competence.
*
* There will be 3 different algorithms and all of them will be capable of avoid overlap via a specific method.
* Maybe...
*/
private int chosenAlgo; // it's a random int to know which algorithm must be picked.
//Algorithms' Stuff
private List <Vector3> PosList = new List<Vector3>(); // here we will store the coordinates to choose from
private int iPosList; // index for PosList.
private int sPosList; // sieze of PosList.
private Transform myTransform; // reference to this transform.
private Vector3 posChosen; // reference to the position chosen
private int shouldI; // int to decide if it should try to instantiate or skip
private Vector3 north;//items of PosLists
private Vector3 south;
private Vector3 east;
private Vector3 west;
// I'll set up some things in here
void Awake () {
//set the random int to choose
chosenAlgo = 1; // FOR TEST SAKE
//chosenAlgo = Random.Range(1,3);
Debug.Log ("Chosen Algo is:" + chosenAlgo);
//get reference
myTransform = gameObject.transform;
north = new Vector3(myTransform.position.x+10.0f, 0, myTransform.position.z);
south = new Vector3(myTransform.position.x-10.0f, 0, myTransform.position.z);
east = new Vector3(myTransform.position.x, 0, myTransform.position.z+10.0f);
west = new Vector3(myTransform.position.x, 0, myTransform.position.z-10.0f);
//populate list
PosList.Add(north);
PosList.Add(south);
PosList.Add(east);
PosList.Add(west);
sPosList = PosList.Count;
}
// Use this for initialization
void Start () {
//When the square is spawned, it will decide what to do.
//I know this thing down here sucks but hey! at least it's working!
if(chosenAlgo == 1){
CdrunkardWalk();
}
if(chosenAlgo == 2){
SdrunkardWalk();
}
if(chosenAlgo == 3){
BdrunkardWalk();
}
}
// Update is called once per frame
void Update () {
}
//Classic Drunkard Walk
void CdrunkardWalk(){
//Debug.Log ("You got to Classic"); just to be sure...
//Now we have to build the algorithm... SO, for every item in List
for(int i = 0; i<sPosList; i++){
//Ask yourself if you should try to spawn
shouldI = Random.Range(0,2);//this will return 0 to 1.9999... and since it's an int it works fine
//If you said yes...
if(shouldI == 0){
//Debug.Log("Tried to spawn for:" +PosList[i]);
//...ask yourself if you CAN spawn
if(Physics.CheckSphere(PosList[i], 5.0f) == false){
GameObject clone;
clone = Instantiate(this.gameObject,PosList[i],myTransform.localRotation) as GameObject;
}
}
//If you said no...
if(shouldI == 1){
//Debug.Log("Not spawned for:" +PosList[i]);
}
}
}
//Straight Drunkard
void SdrunkardWalk(){
//Debug.Log ("You got to Straight");
}
//Backtrack Drunkard
void BdrunkardWalk(){
//Debug.Log ("You got to Backtrack");
}
}
Now, as you can see this code is reeeaaally rough... all those if statements should vanish. I was trying to but I got stuck. If you have any suggestions ( like, patient full explanations) I'd really appreciate :D
The other 2 methods that I left in blank are simple variations to the algorithm, I still have to build them. One will go back to the starting square and try to walk again, if it can... just to fix those runs in which at the very beginning one or more directions are discarded. The other will try to store in a variable the previous position chosen ( ex: north), and tweak the Positions' List so that it has more items equal to that one ( meaning it becomes a shuffle bag... i believe that's its name).
I already know I'll have problems with the other two methods but that's the fun of it, or is it not? Again, suggestions of links and/or ideas are appreciated.
Last, but not least, I'll have to make it stop before everything crashes. I was thinking about a variable that adds +1 every time something is spawned, and another one to store the total amount of squares allowed.
I hope this code can help others understand how to build these kind of algorithms from scratches, feel free to text me and thanks in advance for your attention! - e
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
By Code generated Mesh (problems with visibility) 4 Answers
Procedural terrain generation ? 0 Answers