- Home /
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();
}
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.
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:
Create a copy of the cards, called temp
Clear the contents of cards
While we have any temporary cards:
Get a random card index
Get the card for that index
Remove the card from the temporary cards using the index
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.
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);
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).
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
![](https://koobas.hobune.stream/wayback/20220612175245im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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