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.
using UnityEngine;
using System.Collections;
public class DeckOfCards : MonoBehaviour {
private Card[] deck; // array of cards for the deck from Card.cs
private int currentCard; // know what card we are on out of the deck
private const int NUMBER_OF_CARDS = 52; // constant value of 52 this never changes
public System.Random ranNum = new System.Random(); // for making a random number to shuffle
public DeckOfCards() // constructor to default deck of cards
{
string [] faces = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"}; //array of this type
string [] suits = {"Clubs", "Spades", "Hearts", "Diamonds"}; // array of this type
deck = new Card[NUMBER_OF_CARDS]; // create a new deck with 52 cards from the card class or Card.cs
currentCard = 0; // the current card needs to be zero at start
ranNum = new System.Random (); // create a random number
for (int count = 0; count < deck.Length; count++) // count starts at zero. While count is less than the deck size then count.
{
deck[count] = new Card (faces[count % 13] , suits[count / 13]); // so starts at 0 adds a face then "Of" then adds suit
}
if (faces.Equals("One") && suits.Equals("Clubs"))
{
print("test test test ");
}
}
public void Shuffle() // shuffle the deck
{
currentCard = 0; // always start over when shuffle make current card 0
for (int first = 0; first < deck.Length; first ++)
{
int second = ranNum.Next(NUMBER_OF_CARDS); // pull out a card of the deck
Card temp = deck[first]; // set first card into temp
deck[first] = deck[second]; // set the first card into second
deck[second] = temp; // then put the card back into first?
}
}
public Card DealCard() // deal the cards
{
if(currentCard < deck.Length) // if cards exist
{
return deck[currentCard++]; // return card then return next
}
else {
return null; // stop return nothing
}
}
void Update()
{
}
}
Display Code
using UnityEngine;
using System.Collections;
public class PrintCard : MonoBehaviour {
// Use this for initialization
private DeckOfCards deal;
void Awake()
{
deal = gameObject.GetComponent<DeckOfCards>();
}
void Start ()
{
DeckOfCards deck1 = new DeckOfCards (); // create a deck
deck1.Shuffle(); // shuffle the deck
for(int i = 0; i < 52; i ++)
{
print( deck1.DealCard());
}
if(deck1.name.Equals("One Of Clubs"))
{
print("fkfkfkfkfkf");
}
}
// Update is called once per frame
void Update ()
{
}
}