Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SuperRaed · Jun 21, 2016 at 08:04 AM · randomalgorithmmazerandomizationstack

How to randomize my maze generation algorithm(DFS & stacks)

I'm using DFS algorithm with a stack data structure where the logic so far while (stack is not empty){ pop last added cell mark as visited explore all available children(yet to be visited) in the following order left,right,top, down push to stack } while this does traverse the whole maze it does so in a very organized fashion which is down up right left

my question is how can I make it random?

code:

 while ( nodeList.Count  > 0) {
             
             Node temp = nodeList.Pop();
             temp.visited = true;
             
             if (temp.left!=null && temp.left.visited == false) {                
                 nodeList.Push(temp.left);              
 
             }
             if (temp.right != null && temp.right.visited == false)
             {                
                 nodeList.Push(temp.right);                
             }
             if (temp.up != null && temp.up.visited == false)
             {
                 nodeList.Push(temp.up);
             }
             if (temp.down != null && temp.down.visited == false)
             {
                 nodeList.Push(temp.down);
             }
             yield return new WaitForSeconds(1f);
         }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by MasterLG · Jun 21, 2016 at 09:36 AM

Perhaps consider this in place of all the if statements:

 switch (Random.Range(0, 3)) {
     case 0:
         if (temp.left!=null && temp.left.visited == false)
         {
             nodeList.Push(temp.left);
         }
         break;
     case 1:
         if (temp.right != null && temp.right.visited == false)
         {                
             nodeList.Push(temp.right);                
         }
         break;
     case 2:
         if (temp.up != null && temp.up.visited == false)
         {
             nodeList.Push(temp.up);
         }
         break;
     case 3:
         if (temp.down != null && temp.down.visited == false)
         {
             nodeList.Push(temp.down);
         }
         break;
 }

EDIT: There is a flaw in the above code wherein it will only attempt one direction at a time.

This new code is not the most elegant thing in the world, but it takes all 4 possible directions into consideration, then randomizes the order they're in, and then attempts all of them in the new order.

 int[] direction = new int[] { 0, 1, 2, 3 };
 
 for (int d = 0; d < direction.Length; d++) {
     int r = Random.Range(0, 3);
 
     int swap = direction[d];
     direction[d] = direction[r];
     direction[r] = swap;
 }
 
 foreach (int d in direction) {
     switch (d) {
         case 0:
             if (temp.left!=null && temp.left.visited == false)
             {
                 nodeList.Push(temp.left);
             }
             break;
         case 1:
             if (temp.right != null && temp.right.visited == false)
             {                
                 nodeList.Push(temp.right);                
             }
             break;
         case 2:
             if (temp.up != null && temp.up.visited == false)
             {
                 nodeList.Push(temp.up);
             }
             break;
         case 3:
             if (temp.down != null && temp.down.visited == false)
             {
                 nodeList.Push(temp.down);
             }
             break;
     }
 }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SuperRaed · Jun 21, 2016 at 11:23 AM 0
Share

but what happens in case a chosen neighbor didn't exist?

avatar image MasterLG SuperRaed · Jun 21, 2016 at 12:04 PM 0
Share

Ah, yeah, bad oversight from me. It does only check one direction at a time. I edited my answer with a newer iteration. This new stuff uses a related structure but checks all directions.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

46 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Randomize values without exceeding a value? 3 Answers

Foreach Statement Random Sequence/Order? 3 Answers

An algorithm to automatically instantiate objects 2 Answers

How to randomize a number without duplication? 1 Answer

Random objects in coordinates specific 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges