- Home /
Question is off-topic or not relevant
Random Card Deal
ublic class KartKarma : MonoBehaviour { public GameObject[] Deste; public static GameObject KupaAs, Kupa2, Kupa3, Kupa4, Kupa5, Kupa6, Kupa7, Kupa8, Kupa9, Kupa10, KupaVale, KupaKiz, KupaPapaz; public static GameObject KaroAs, Karo2, Karo3, Karo4, Karo5, Karo6, Karo7, Karo8, Karo9, Karo10, Karo11, Karo12, Karo13; public static GameObject SinekAs, Sinek2, Sinek3, Sinek4, Sinek5, Sinek6, Sinek7, Sinek8, Sinek9, Sinek10, Sinek11, Sinek12, Sinek13; public static GameObject MacaAs, Maca2, Maca3, Maca4, Maca5, Maca6, Maca7, Maca8, Maca9, Maca10, Maca11, Maca12, Maca13; public GameObject[] Kupa = {KupaAs, Kupa2, Kupa3, Kupa4, Kupa5, Kupa6, Kupa7, Kupa8, Kupa9, Kupa10, KupaVale, KupaKiz, KupaPapaz}; public GameObject[] Karo = {KaroAs, Karo2, Karo3, Karo4, Karo5, Karo6, Karo7, Karo8, Karo9, Karo10, Karo11, Karo12, Karo13}; public GameObject[] Sinek = {SinekAs, Sinek2, Sinek3, Sinek4, Sinek5, Sinek6, Sinek7, Sinek8, Sinek9, Sinek10, Sinek11, Sinek12, Sinek13}; public GameObject[] Maca = {MacaAs, Maca2, Maca3, Maca4, Maca5, Maca6, Maca7, Maca8, Maca9, Maca10, Maca11, Maca12, Maca13}; public int[] nodes; List deste = new List();
// Use this for initialization
void Start () {
int i = 0;
while(i < 26){
int a = Random.Range(1,3);
if (a == 1) {
int b = Random.Range(0,13);
if(Maca != null){
while(Maca[b] == null){
b = Random.Range(0,13);
}
deste.Add(Maca[b]);
Maca[b] = null;
}
else {
//while(a == 1)
a = Random.Range(2,3);
}
}
if (a == 2) {
int b = Random.Range(0,13);
if(Sinek != null){
while(Sinek[b] == null)
b = Random.Range(0,13);
deste.Add (Sinek[b]);
Sinek[b] = null;
}
else {
//while(a == 2)
a = Random.Range(1,2);
}
}
/* if (a == 2) {
int b = Random.Range(0,13);
if(Karo != null){
while(Karo[b] == null)
b = Random.Range(0,13);
deste.Add (Karo[b]);
Karo[b] = null;
}
else {
while(a == 2)
a = Random.Range(2,3);
}
}*/
/*if (a == 4) {
int b = Random.Range(0,13);
if(Kupa != null){
while(Kupa[b] == null){
b = Random.Range(0,13);
}
deste.Add(Kupa[b]);
Kupa[b] = null;
}
else a = Random.Range(2,3);
}*/
i++;
}
int j = 0;
while (j < deste.Count) {
Debug.Log (deste[j]);
j++;
}
}
void Update () {
}
}
Guys here is the code. Hello everyone I got a problem selecting random numbers to shuffke a deck of cards. To shuffle randomly we tried to pick a random card in every loop, but it seems like if we ran out of one kind, we cannot pick a different kind randomly. It goes into infinite loop. Any suggestions?
Just to clarify, is your question: "How to shuffle a list?" Please edit your post I can't read the code in the first part.
This is not a Unity question. There are numerous examples of decks of cards posted on the internet... Some in C#. They include dealing and shuffling code. @Fabkins approach is a common way to solve this problem.
Answer by Fabkins · Feb 06, 2014 at 02:57 PM
What you need to do is to shuffle the deck, then start picking cards from the top.
To do that all you need to do is go through each card position in the deck and randomly move it to a different position.
In pseudo code it goes something like:
for( var i:int =0 ; i < totalNumberOfCards ; i++)
{
var currentCard=deck[i];
var randomPos=randomnumber(totalNumberOfCards);
// Swap the two cards
deck[i]= deck[randomPos];
deck[randomPos]=currentCard;
}
Now your deck is shuffled and you just need to get pick cards from the top.
Follow this Question
Related Questions
Does the Unity Webplayer Plugin run on a laptop without a graphics adapter expansion card? 1 Answer
how to make script when two cards hit and action? like solitaire.. 0 Answers
Open just 2 card at a time 0 Answers
Microsoft C# Card Game Starter Kit 0 Answers
CardboardVR on Samsung Note4, wired phenomenon, 2 cubes in 2 eyes 0 Answers