Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Hi, I want to get some help for Card randomizing script

Discussion in 'Scripting' started by zecz321, Oct 7, 2014.

  1. zecz321

    zecz321

    Joined:
    Oct 7, 2014
    Posts:
    3
    Hi, I want to make a prototype game of card game+ platformer.
    I need to build a card randomizing script, I will use about 20~30 card in total
    rule will be similar to the 7poker but I will use only 2 card and when character levels up it will be up to 5 cards.

    I am just learning basic of Java scripting so, can anyone please help me to build this script?
     
  2. zecz321

    zecz321

    Joined:
    Oct 7, 2014
    Posts:
    3
    Ah also I am trying to make a 2d style Game :D
     
  3. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
  4. zecz321

    zecz321

    Joined:
    Oct 7, 2014
    Posts:
    3
    Hi, Thanks for the answer:D , I already know that I can use Random.Range but I don't know how to use it with game object or img or gui txture.
     
  5. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    You could put them in an array, then use Random.Range to pick from them.
    Something like this:

    Code (JavaScript):
    1. var cardObjects = new Array();
    2.  
    3. function GetRandomCard(cardList:Array)
    4. {
    5.     return Random.Range(0,cardList.length);
    6. }
    Here you'd assign your cards to the array either through script or in the inspector. So you'd call the GetRandomCard function when you wanted a random card, giving it the array of the card list you want.
    As an example, this would let me store a random card in the newCard variable every time I pressed 'c'. Note that if I get another random card it overwrites the old one, so you'll likely want to do something with the random card before you get another:

    Code (JavaScript):
    1. function Update()
    2. {
    3.     if(Input.GetKeyDown("c"))
    4.     {
    5.         var newCard = GetRandomCard(cardObjects);
    6.     }
    7. }
    Lastly, if you're list of cards you want to fetch from is always going to be the same length you'll want to use a builtin array, such as GameObject[] instead of Array(), using whatever builtin array is appropriate, of course.

    If that isn't an option, you may need to convert your resulting random card before using it. If you were storing objects in the array, for example, something like this:

    Code (JavaScript):
    1. function Update()
    2. {
    3.     if(Input.GetKeyDown("c"))
    4.     {
    5.         var newCardObj = GetRandomCard(cardObjects);
    6.         var newCard : GameObject = newCardObj;
    7.     }
    8. }
    The reason for this is that the array stores it as a generic object, so you won't be able to use it quite the same way until converted.

    Here's the scripting API for arrays, if you're interested in reading up more about them and how to use them:
    http://docs.unity3d.com/ScriptReference/Array.html
     
  6. anonymousunitycreator

    anonymousunitycreator

    Joined:
    Sep 20, 2014
    Posts:
    43
    Why don't you use a public array?
    This is C#, but you should get the general gist. This is also how I do all my card games.
    Note: this is using a full deck, hence the 0,26 randoms.

    Code (CSharp):
    1.  
    2. public GameObject[] CardPrefabsRED; // Drag your cards into the 2 categories
    3. public GameObject[] CardPrefabsBLK;  // This will name then CardPrefabColor[0][1][2][3][etc]
    4. private GameObject CardPrefabSelected;
    5. private int randomInt;
    6.  
    7. //Then randomly select card
    8. randomInt = Random.Range(0,10);
    9.         if(randomInt >= 5)
    10.         {
    11.                 CardPrefabSelected = CardPrefabsBLK[Random.Range(0,26)];
    12.         }
    13.         else if (randomInt < 5)
    14.         {
    15.                 CardPrefabSelected = CardPrefabsRED[Random.Range(0,26)];
    16.         }
    17.  
    18.  
    Or for one card deck

    Code (CSharp):
    1.  
    2. public GameObject[] CardPrefabs; // Drag your cards into the category in inspector
    3. private GameObject CardPrefabSelected;
    4.  
    5. //Then randomly select card
    6. CardPrefabSelected = CardPrefabs[Random.Range(0,52)];
    7.  
     
  7. reentrant

    reentrant

    Joined:
    Oct 5, 2014
    Posts:
    59
    I think you should create prefabs that each have a sprite renderer, and maybe add a script to each that has information about the card, such as the color. C# was a little confusing to me when I started, too, if you need a Javascript example let me know.
     
  8. anonymousunitycreator

    anonymousunitycreator

    Joined:
    Sep 20, 2014
    Posts:
    43
    If you need more information than just red or black I would suggest a script too, though that could be tedious to link to your main script.
    If you only need black and red, then it would be simpler to just tag each card with it's color.
     
  9. Deleted User

    Deleted User

    Guest

    Keep in mind, with a randomize seed there is nothing that tells it too not randomize the same number twice.
    So, if your trying to create a card deck you need to keep that in mind.


    So, the logic of this would be.
    Make a type list of all your cards.

    I would build the type something like this.

    DECK
    Number, Value, AltValue, Suite

    1,11,1,Hearts,ACE
    2,11,1,Spades,ACE
    3,10,0,Spades,KING

    Ok, not going to build the whole list for you get the point..

    Then, to build a list..
    set new array of DECK

    maxcards= 24;

    do
    {
    x = RANDOM SEED
    check = findrandomseedindeck(x)
    if !check
    {
    add card to deck
    }
    } while (i < maxcards);
     
  10. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    I forget what the algorithm is called, but what you do is iterate through each element of an array. For each element, get a random index of any element in the array. Then swap the places of the current element and the one randomly selected.
     
  11. anonymousunitycreator

    anonymousunitycreator

    Joined:
    Sep 20, 2014
    Posts:
    43
    You literally said it cranky, That's a foreach loop :)
     
  12. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
  13. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Fill a list with your cards. random range 0 - list.Count - that's your card. remove that card from the list. rinse and repeat with no duplicates. when the list reaches 0 ( or how low you want it) flush what's left (list.clear() ) and refill the list with the fresh deck.