c# - I want to shuffle 5 cards in an array -
i have 5 cards in gabeobject[] cards. want shuffle , save these cards gameobject[] cardshuffle. wrong code. shuffles same cards. how can make not shuffle same cards again? thank you.
using unityengine; using system.collections; public class main : monobehaviour { public gameobject[] cards; public gameobject[] cardshuffle; int i=0; int j = 0; int save=0; int[] kayit = new int[10]; int kayit2; public gameobject[] player1; public gameobject[] player2; public gameobject[] player3; // use initialization void start () { kayit[0] = 20; while(i<5) { save = random.range(0, 5); (int = 0; < 5; a++ ) { if(kayit[a] == save) { kayit2 = save; } } if (kayit2 != save) { cardshuffle[i] = cards[save]; } else i--; kayit[i] = save; i++; } while (cards[j] != null) { player1[j] = cards[j]; j++; debug.log(j); } } // update called once per frame void update () { } }
okay works plastic sturgeon.
using unityengine; using system.collections; public class main : monobehaviour { public gameobject[] cards; public gameobject[] tmp; public gameobject[] player1; public gameobject[] player2; public gameobject[] player3; // use initialization void start () { (int t = 0; t < cards.length; t++) { tmp[0] = cards[t]; int r = random.range(t, cards.length); cards[t] = cards[r]; cards[r] = tmp[0]; } for(int i=0; i<cards.length;i++) { player1[i] = cards[i]; } } // update called once per frame void update () { } }
Comments
Post a Comment