Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Feb 06, 2014 at 04:36 PM by robertbu for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by BlazerGlazer · Feb 06, 2014 at 01:42 PM · card

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?

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 zee_ola05 · Feb 06, 2014 at 02:47 PM 0
Share

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.

avatar image robertbu · Feb 06, 2014 at 04:38 PM 0
Share

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.

1 Reply

  • Sort: 
avatar image
1

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.

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

Follow this Question

Answers Answers and Comments

21 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

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


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