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 Blitzerine_1 · Aug 12, 2011 at 10:35 AM · arrayfilepackageshuffle

Shuffle Array on button

How to i create a shuffle aray in my scripts that shuffles the arrangement of these 5 cards

 var selStrings : String[] =  ["Card 1", "Card 2", "Card 3", "Card 4", "Card 5"];
 var deckRect : Rect = Rect (0,480,270,380);
 
 private var cardsInHands : CardsInHands;
 
 
 function Start() 
 {
     cardsInHands = GameObject.FindGameObjectWithTag("Hands").GetComponent(CardsInHands);
 }  
 function Update ()    
 {  if (selGridInt > 0)    { 
         switch (selGridInt) 
         {  
             case 1:
                 break; 
             case 2:   
                 break; 
             default:  
                 break;   
         } 
     }    
 }
 function OnGUI () 
 {
 deckRect = GUI.Window (0, deckRect, DoMyDeckWindow, "Deck");
 if (GUI.Button(Rect(boxPos.x, boxPos.y, 140, 40), "Draw"))
         {
             DrawCard();
         }
    }
 function DrawCard()
 {
     var tempCards : String[] = new String[selStrings.length-1];
     for (var i : int; i < selStrings.length; i++)
     {
         if (i == 0)
         {
             print(selStrings[i]);
            cardsInHands.AddCards(selStrings[i]);
             continue;
         }
         tempCards[i-1] = selStrings[i];
     }
     selStrings = new String[tempCards.length];
     for (var j : int; j < tempCards.length; j++)
     {
         selStrings[j] = tempCards[j];
     }   
 }  
 function DoMyDeckWindow (windowID : int) {    
     scrollPosition = GUI.BeginScrollView (Rect (10,20,258,358), scrollPosition, Rect (0, 0, 230, 1200));
     selGridInt = GUI.SelectionGrid (Rect (0, 0, 235, 1200), selGridInt, selStrings, 1);
     GUI.EndScrollView();
 }


Save as Click.js

Create a new game object tag "Hands"

 var cards : Array;
 var handRect : Rect = Rect (0,290,270,190);
 private var cardList : String[];
 private var selGridInt : int = -1;
 private var scrollPosition : Vector2 = Vector2.zero;
 
 function Start(){
     cardList = new String[1];
     cardList[0] = "";
     cards = new Array();
 }
 function AddCards (cardsToAdd : String) {
     cards.Push(cardsToAdd);
     cardList = cards.ToBuiltin(String);
     print(cardList[0]);
 }
 function OnGUI(){
     handRect = GUI.Window (1, handRect, DoMyHandWindow, "Hand");  
 }
 function DoMyHandWindow (windowID : int) {
     scrollPosition = GUI.BeginScrollView (Rect (10,20,258,140), scrollPosition, Rect (0, 0, 230, 500));
     if (cardList != null)
     {
         selGridInt = GUI.SelectionGrid (Rect (0, 50, 235, 400), selGridInt, cardList, 1);
     }
     GUI.EndScrollView();
 }



Comment
Add comment · Show 2
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 Blitzerine_1 · Aug 12, 2011 at 06:02 PM 0
Share

No answers :/

avatar image Rennat · Aug 12, 2011 at 06:11 PM 1
Share

Possible Duplicate

No answers probably because it seems like you want people to write your code for you. Whether that's the case or not, your wording and the way you posted most of your code for a simple question makes it seem that way.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Statement · Aug 12, 2011 at 10:21 PM

With a list:

 // Assume cards is a List.<String> with all your cards.
 var temp = List.<String>(cards);
 cards.Clear();
 while (temp.Count != 0) {
     var index = Random.Range(0, temp.Count);
     var card = temp[index];
     temp.RemoveAt(index);
     cards.Add(card);
 }

Line by line, the algorithm does the following:

  1. Create a copy of the cards, called temp

  2. Clear the contents of cards

  3. While we have any temporary cards:

  4. Get a random card index

  5. Get the card for that index

  6. Remove the card from the temporary cards using the index

  7. Add the card to your set of cards

This will randomize quite nicely since it picks a card at random and places it in the list of cards. Other variants may shuffle the same card around multiple times causing low entropy. The down side is that it will consume more memory, but in your case it is negligable. Its possible to increase the entropy for the solution posted in your comment by increasing the number of iterations, which will add CPU overhead. This is my favorite shuffling algorithm for smaller sets of data since it is simple to follow and shuffles good.

Comment
Add comment · 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
0

Answer by Blankzz · Aug 12, 2011 at 10:33 PM

One of many ways of doing it. Somebody may have to correct my syntax ;)

 public var shuffleCount:int = 10;
 public var arrayOfStrings : String[] = new String["One", "Two", "Three", "Four", "Five"]; 
 
 public function Shuffle(String[] collection) : void
 {
     i1:int;
     i2:int;
     
     for(i:int = 0; i < shuffleCount; i++)
     {
         Random.seed = Random(0,1000);
         i1 = Random.Range(0, collection.length);
         i2 = Random.Range(0, collection.length);
         temp:String = collection[i1];
         collection[i1] = collection[i2];
         collection[i2] = temp;
     }
 }
 
 //call Shuffle when you want to shuffle cards
 Shuffle(arrayOfStrings);
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 Statement · Aug 12, 2011 at 10:50 PM 0
Share

I think you forgot to use shuffleCount? Also this random can lock up giving the same cards over and over again if the seed become the same as after the first iteration, which is possible (0.1% chance of this happening which is fairly high).

avatar image Blankzz · Aug 12, 2011 at 11:45 PM 0
Share

oops meant shuffleCount not collection.length. Probably should have used a while loop too. Good point. Not really acceptable then. Down vote my answer...........................Just kidding ;)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can Vexe.FastSave save dictionaries to files? 1 Answer

File paths on Android 0 Answers

JSON missing arrays 1 Answer

How to instantiate the first 8 gameobjects in an array (UnityScript)? 1 Answer

"Opening file fail"?? 3 Answers


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