Search Unity

I don't know exactly what I'm Asking... (Arrays)

Discussion in 'Scripting' started by caseyboundless, May 13, 2016.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I have this script that will output a shuffled deck of 52 cards beautifully to the console. Now I'm trying to check the output name so I can assign each sprite to that name of the card. Example if output is "Ace Of Spades" then display the sprite of Ace Of Spades. Totally lost here.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DeckOfCards : MonoBehaviour {
    5.  
    6.     private Card[] deck; // array of cards for the deck from Card.cs
    7.     private int currentCard; // know what card we are on out of the deck
    8.     private const int NUMBER_OF_CARDS = 52; // constant value of 52 this never changes
    9.     public System.Random ranNum = new System.Random(); // for making a random number to shuffle
    10.  
    11.  
    12.     public DeckOfCards() // constructor to default deck of cards
    13.     {
    14.          string [] faces = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"}; //array of this type
    15.          string [] suits = {"Clubs", "Spades", "Hearts", "Diamonds"}; // array of this type
    16.    
    17.         deck = new Card[NUMBER_OF_CARDS]; // create a new deck with 52 cards from the card class or Card.cs
    18.         currentCard = 0; // the current card needs to be zero at start
    19.         ranNum = new System.Random (); // create a random number
    20.    
    21.         for (int count = 0; count < deck.Length; count++) // count starts at zero. While count is less than the deck size then count.
    22.         {
    23.             deck[count] = new Card (faces[count % 13] , suits[count / 13]); // so starts at 0 adds a face then "Of" then adds suit
    24.         }
    25.    
    26.  
    27.  
    28.  
    29.  
    30.         if (faces.Equals("One") && suits.Equals("Clubs"))
    31.         {
    32.             print("test test test ");
    33.  
    34.         }
    35.    
    36.     }
    37.  
    38.  
    39.     public void Shuffle() // shuffle the deck
    40.     {
    41.         currentCard = 0; // always start over when shuffle make current card 0
    42.         for (int first = 0; first < deck.Length; first ++)
    43.         {
    44.  
    45.  
    46.             int second = ranNum.Next(NUMBER_OF_CARDS); // pull out a card of the deck
    47.             Card temp = deck[first]; // set first card into temp
    48.             deck[first] = deck[second]; // set the first card into second
    49.             deck[second] = temp; // then put the card back into first?
    50.        
    51.        
    52.         }
    53.    
    54.     }
    55.         public Card DealCard() // deal the cards
    56.         {
    57.         if(currentCard < deck.Length) // if cards exist
    58.         {
    59.             return deck[currentCard++]; // return card then return next
    60.         }
    61.         else {
    62.             return null; // stop return nothing
    63.    
    64.         }
    65.    
    66.    
    67. }
    68.  
    69.  
    70.  
    71.  
    72.  
    73.     void Update()
    74.     {
    75.            
    76.     }
    77.  
    78.  
    79.  
    80.  
    81.  
    82. }
    83.  




    Display Code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PrintCard : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     private DeckOfCards deal;
    8.     void Awake()
    9.     {
    10.         deal = gameObject.GetComponent<DeckOfCards>();
    11.  
    12.     }
    13.  
    14.     void Start ()
    15.     {
    16.         DeckOfCards deck1 = new DeckOfCards (); // create a deck
    17.         deck1.Shuffle(); // shuffle the deck
    18.  
    19.         for(int i = 0; i < 52; i ++)
    20.         {
    21.             print( deck1.DealCard());
    22.         }
    23.         if(deck1.name.Equals("One Of Clubs"))
    24.         {
    25.             print("fkfkfkfkfkf");
    26.        
    27.         }
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update ()
    32.     {
    33.        
    34.     }
    35.  
    36.  
    37.    
    38.  
    39.  
    40. }
     

    Attached Files:

  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,632
    I made a solitaire in unity, so had to tackle this sort of thing. I'd say keep face value as integers, rather than strings (maybe 1 through 13), although if this is a card game with slow pace it won't really matter.

    And so you'll need sprites for the visuals of the entire deck, and a prefab for a gameobject they can be assigned to. You could use either ui images, or just regular sprite gameobjects. You probably should assign a script to them that keeps info like face value and suit, and as your "dealing" the cards (probably some animation) you keep populating that scripts variables so later you can check it in comparisons. When your shuffling and moving to foundations, or whatever, you assign the sprite based on the shuffle, probably with a few loops that create a path to a resource to load the sprite (that's how I did it for simplicity) and assign it.

    For the suit you could use char like 'd' / 'h' / 's' / 'c' instead of a whole string, and check in comparisons that way.

    This means during assignment of textures you could do something like "if (thisCard.suit == 'd') { path = thisCard.value.ToString () + "_d"}; // load from resources or something"

    Hope that helps get you started!
     
    Last edited: May 13, 2016
unityunity