Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Deck of Cards

Discussion in 'Scripting' started by themind15, Jul 9, 2010.

  1. themind15

    themind15

    Joined:
    Apr 22, 2010
    Posts:
    16
    I am working on setting up a deck of cards in code and have been using some Java code has a little help hand in setting it up. The only problem I having is translating some of the code to Javascript to work in Unity.

    This is what I have been using.

    JAVA CODE
    Code (csharp):
    1. private Card[] deck;   // An array of 52 Cards, representing the deck.
    2.     private int cardsUsed; // How many cards have been dealt from the deck.
    3.    
    4.     public Deck() {
    5.            // Create an unshuffled deck of cards.
    6.        deck = new Card[52];
    7.        int cardCt = 0; // How many cards have been created so far.
    8.        for ( int suit = 0; suit <= 3; suit++ ) {
    9.           for ( int value = 1; value <= 13; value++ ) {
    10.              deck[cardCt] = new Card(value,suit);
    11.              cardCt++;
    12.           }
    13.        }
    14.        cardsUsed = 0;
    15.     }
    16.  
    The problem I am having is the creation of the Array and using it the same way as above, adding the value # and the suit # to the array index indicated.

    Any tips or suggestions for getting this to work?

    Thanks.
     
  2. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    I don't understand what's the aim of the script. Shuffle ? Picking a card ? What does the array contains ? Names (Strings) of cards ?
     
  3. themind15

    themind15

    Joined:
    Apr 22, 2010
    Posts:
    16
    The ultimate aim is to set up the Deck of cards for the game to use. There will be a shuffle function and a dealing function later, but right now I am trying to get the Deck set up.

    The array is supposed to hold 2 int values, 1 to signify the suit and 1 for the card value. 0-3 for the suits, and then 1-13 for the value (2,3,4,...Jack, Queen, etc...)

    There is another script I have the handles the card info, so at the start before shuffling the first card should have the values (0,1) which the computer will identify as the game plays as the Ace of Spades.

    My problem right now lies in the set up of the array for the deck of cards. Arrays are still a little confusing for me in Unity Javascript because the specific nature of declaring arrays and getting what you need to do to get info into the array. In the Java

    So I am looking for help on how I should declare the array and I how I should add the values into the Array in the way I am looking for above.

    Or if you know of another way to do this less complicated let me know.

    Thanks.
     
  4. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Code (csharp):
    1. function Start () {
    2.     InitiateDeck ();
    3.     ShuffleDeck (2);
    4. }
    5.  
    6. var deck : Array = new Array ();
    7.  
    8. function InitiateDeck () {
    9.     deck.Clear ();
    10.     for (var valueCard : float = 1; valueCard <= 13; valueCard++) {
    11.         for (var suitCard : float = 1; suitCard <= 4; suitCard++) {
    12.             deck.Push (Vector2 (valueCard, suitCard));
    13.         }
    14.     }
    15.     Debug.Log ("Deck initiated. Current size :"+deck.length+". Current content:"+deck);
    16. }
    17.  
    18. function ShuffleDeck (times : int) {
    19.     // Repeat the process as many times as specified.
    20.     for (var i : int = 0; i < times; i++) {
    21.    
    22.         // Setting the first temporary deck, put all existing card of deck in deck 1.
    23.         var tempDeck1 : Array = new Array ();
    24.         for (var j : int = 0; j < deck.length; j++) {
    25.             tempDeck1.Push (deck[j]);
    26.         }
    27.        
    28.         // Shuffle by using random number and transfers in deck 2.
    29.         var tempDeck2 : Array = new Array ();
    30.        
    31.         var indexCard : int;
    32.        
    33.         while (0 < tempDeck1.length) {
    34.            
    35.             // Get a random card between minimum (inclusive) and maximum (exclusive).
    36.             var minimum : int = 0;
    37.             var maximum : int = tempDeck1.length;
    38.             indexCard = Random.Range (minimum, maximum);
    39.            
    40.             // Add the card in the deck 2.
    41.             tempDeck2.Push (tempDeck1 [indexCard]);
    42.            
    43.             // Remove the card at deck 1.
    44.             tempDeck1.RemoveAt (indexCard);
    45.         }
    46.        
    47.         // Shuffle done, but needs to set the new deck in the current deck.
    48.         deck.Clear ();
    49.         for (var k : int = 0; k < tempDeck2.length; k++) {
    50.             deck.Push (tempDeck2[k]);
    51.         }
    52.         Debug.Log ("Deck shuffled. Current size :"+deck.length+". Current content:"+deck);
    53.     }
    54. }
    In the script above :

    - I put all the content of main deck in temp deck one.
    - I pick a random card in temp deck one, put it in temp deck two before removing it from temp deck one.
    - When temp deck one is empty, put the content of temp deck two in main deck.

    I could have used only one temp deck :

    - Picking one random card in the main deck, putting it in the temp deck and removing it from the main deck.
    - When main deck is empty, put the content of temp deck in the main deck.

    But I wanted to modify main deck in the last moment, to avoid surprises.

    The script seems to work. I am using Vector2 because well, they contain two values (value, suit).
     
  5. themind15

    themind15

    Joined:
    Apr 22, 2010
    Posts:
    16
    Thank you for your help. What you did with InitiateDeck() is what I was having trouble doing. Like I said, the Array stuff is still a little tricky for me.

    And the Shuffle function is pretty much what I was going to do. Nice to see to that I had the right idea.

    Thanks again.
     
  6. DylanGlow

    DylanGlow

    Joined:
    May 22, 2013
    Posts:
    2
    I realize this is quite an old post, but this is the first thing that pops up when I Googled "unity deck of cards." Therefore, I find that it may be helpful to post a C# version of this code to help those not working with Java. I've added simple input functions to read the console easier.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. /// <summary>
    7. /// Deck handler.
    8. /// This operation initializes a standard deck of cards based
    9. /// off of Vector2's. (First input is suit, second is value.)
    10. /// This both instantiates a deck and shuffles the deck.
    11. /// </summary>
    12. public class DeckHandler : MonoBehaviour{
    13.    
    14.     public int timesToShuffle;
    15.     private List<Vector2> mainDeck = new List<Vector2>();
    16.     private bool ableToShuffle = false;
    17.    
    18.     // Is used to initialize
    19.     void Start(){
    20.         InitializeDeck();
    21.     }
    22.    
    23.     // Is called once every frame
    24.     void Update(){
    25.         if(Input.GetKeyDown(KeyCode.Space)){
    26.             ShuffleDeck(timesToShuffle);
    27.         }
    28.         if(Input.GetKeyDown(KeyCode.D)){
    29.             DisplayMainDeck();
    30.         }
    31.     }
    32.    
    33.     // Initializes the mainDeck
    34.     void InitializeDeck(){
    35.         mainDeck.Clear();
    36.         for(int deckValue = 1; deckValue <= 13; deckValue++){
    37.             for(int deckSuit = 1; deckSuit <= 4; deckSuit++){
    38.                 mainDeck.Add(new Vector2(deckSuit, deckValue));
    39.             }
    40.         }
    41.         Debug.Log("Deck Initialized. Size: " + mainDeck.Count);
    42.         ableToShuffle = true;
    43.     }
    44.    
    45.     // Shuffles the deck based on the public variable "timesToShuffle"
    46.     void ShuffleDeck(int times){
    47.        
    48.         // Makes it so the player can't press 'Space' to shuffle until shuffling is complete
    49.         ableToShuffle = false;
    50.        
    51.         for(int i = 0; i < times; i++){
    52.             Debug.Log("Shuffling " + (i+1) + " time(s).");
    53.        
    54.             // Initializes a temporary deck to shuffle into
    55.             List<Vector2> tempDeck1 = new List<Vector2>();
    56.             for(int j = 0; j < mainDeck.Count; j++){
    57.                 tempDeck1.Add(mainDeck[j]);
    58.             }
    59.            
    60.             // Initializes an indexCard and a second temporary deck
    61.             List<Vector2> tempDeck2 = new List<Vector2>();
    62.             int indexCard;
    63.            
    64.             // Adds indexCard to tempDeck2 in a random order
    65.             while(0 < tempDeck1.Count){
    66.                 int minimum = 0;
    67.                 int maximum = tempDeck1.Count;
    68.                 indexCard = Random.Range(minimum, maximum);
    69.                
    70.                 tempDeck2.Add(tempDeck1[indexCard]);
    71.                 tempDeck1.RemoveAt(indexCard);
    72.             }
    73.            
    74.             // Adds cards back into mainDeck and prints to console
    75.             mainDeck.Clear();
    76.             for(int k = 0; k < tempDeck2.Count; k++){
    77.                 mainDeck.Add(tempDeck2[k]);
    78.             }
    79.             tempDeck2.Clear();
    80.             Debug.Log("Done shuffling " + (i+1) + ".");
    81.         }
    82.         ableToShuffle = true;
    83.     }
    84.    
    85.     // Displays the contents of mainDeck
    86.     void DisplayMainDeck(){
    87.         Debug.Log("Deck size: " + mainDeck.Count);
    88.         foreach(Vector2 value in mainDeck){
    89.             Debug.Log(value);
    90.         }
    91.     }
    92. }
    93.  
    Cheers,
    ~Dylan
     
    Chastky likes this.
  7. sahira

    sahira

    Joined:
    Jan 29, 2015
    Posts:
    3
    Hello, I'm totally new to Unity4 and totally lost. I'm trying to create a deck of cards but I don't have a clue how to do this, I've seen a lot of posts with code that seems to be perfect but I don't know how to attach images to the code and to ''see'' it in the scene. It might seem a stupid question but I seem to have missed a step somewhere. I created planes and panels and put an image inside it but I need a list of cards and it needs to look like a deck. If you would be so kind to tell me how to do so, it would be very helpfull.