Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jerem0597 · Feb 19, 2021 at 10:10 PM · looprandom.range

Why this fuction make the game to pick numbers which is already removed from list through Random.Range?

 void AvailableMoves()
 {
     for (int i = 0; i < buttonList.Length; i++)
     {
         if (buttonList[i].text == "")
         {
             availableList.Add(i);
         }
         else
         {
             availableList.Remove(i);
         }

         availableMin = availableList.Min();
         availableMax = availableList.Max();
         availableRandom = availableList[Random.Range(availableMin, availableMax)];
     }
 }

It's a Tic-Tac-Toe game with AI.

When it's its turn, before it plays, this function will check every button if it's unoccupied then it will be added in a list of available buttons. If not, it will be removed.

But AI can still pick buttons which is already removed.

Why? Thanks.

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

2 Replies

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

Answer by M-Elwy · Feb 19, 2021 at 11:27 PM

If you have only 2 unoccupied buttons (ex : 1 and 7), Min() will evaluate to 1, Max() will evaluate to 7, Random.Range(availableMin, availableMax) will return any number between 1 and 7, it may return 2,3,4,5 or 6 which are in range but are occupied.


The correct way is something like this:

 void AvailableMoves()
     {
         // Resetting the list as .Clear() will clear values but keep the capacity of the list
         availableList = new List<int>();
         for (int i = 0; i < buttonList.Length; i++)
         {
             if (buttonList[i].text == "")
             {
                 // Adding only unoccupied buttons
                 availableList.Add(i);
             }
         }
 
         // Checking if the list was populated
         if (availableList.Count > 0)
         {
             availableMin = 0;
             availableMax = availableList.Count;
             availableRandom = availableList[Random.Range(availableMin, availableMax)];
         }
         else
         {
             // No buttons available.
         }
     }

 

Edit: code correction

Comment
Add comment · Show 4 · 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 Jerem0597 · Feb 20, 2021 at 12:44 AM 0
Share

The problem is that availableMax keeps to increase size of list because there's not Clear() after using availableRandom.

But with your code, it works better but still not perfect because sometimes that makes AI to reuse the button which is already occupied..

avatar image M-Elwy Jerem0597 · Feb 20, 2021 at 01:22 AM 0
Share

the list gets reset first line in the function before we make any step

avatar image Jerem0597 M-Elwy · Feb 20, 2021 at 01:36 AM 0
Share

I think that I found the problem, look at the comment below. I will explain in there.

Also about reseting the list, no it seems not. If you know how, may you explain? Thanks!

Show more comments
avatar image
0

Answer by Jerem0597 · Feb 19, 2021 at 11:58 PM

@unity_k5jZb8tvx_RtNA Thanks for your explanation but I tried with that like what you said:

 private List<int> availableList = new List<int>();
 private int availableMin = 0;
 private int availableRandom;

 void AvailableMoves()
 {
     for (int i = 0; i < buttonList.Length; i++)
     {
         if (buttonList[i].text == "")
         {
             availableList.Add(i);
         }
         if (availableList != null)
         {
             availableRandom = availableList[Random.Range(availableMin, availableList.Count)];
         }
         else
         {

         }
     }

Then the console signals an error:

"ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection."

Comment
Add comment · Show 21 · 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 jackmw94 · Feb 20, 2021 at 12:14 AM 0
Share

Original answerer was spot on with the random range part but checking if the list is null is not the right check to see if it has no elements in it.

Since it's set to an empty list in the first few lines it will never be null at this point. Here you can check whether availableList.Count > 0 to avoid that exception.

P.S. when you call Clear() on a list, the count does get reset to 0 too so if you need that extra millisecond, you can do this instead of creating a new list each time.

Remember to accept their answer if this solves your problem!

avatar image Jerem0597 jackmw94 · Feb 20, 2021 at 12:38 AM 0
Share

So I tried that:

 void AvailableMoves()
 {
     for (int i = 0; i < buttonList.Length; i++)
     {
         if (buttonList[i].text == "")
         {
             availableList.Add(i);
         }
         
         if (availableList.Count > 0)
         {
             availableRandom = availableList[Random.Range(availableMin, availableList.Count)];
             availableList.Clear();
         }
     }

     Debug.Log("List.Count: " + availableList.Count);
 }

That makes AI to choose the largest value [i] of available buttons in every turn.

So I put Debug to check what is happening in availableList.Count. It logs 0 so Random.Range will pick a value between 0 and 0 then I don't know why it keeps to pick the largest like in the bottom right corner then bottom middle, then left, etc. It's strange.

And yes, I will accept his answer if it works but actually it still doesn't.

avatar image M-Elwy Jerem0597 · Feb 20, 2021 at 12:42 AM 0
Share

It returns 0 as you just cleared the list in the previous line.

Show more comments

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

194 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Instantiate Object Inside of Object and Instantiate Parent Object On Looping. 0 Answers

trying to use while that have 2 random range inside to get a any possible numbers that it will create a num 1 Answer

Infinite Looping Crash 2 Answers

My particle emitter stops emitting once the W key is released but doesnt start when it is pressed again 2 Answers

Pre-Determined Path for Helicopter 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