Magnamura_de

Untitled

Apr 6th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class Deck
  8. {
  9.     //Note the distinct lack of MonoBehavior, it wasn't behaving nicely for Object shit.
  10.  
  11.     private List<Card> deck = new List<Card>();     // Here we do some Object shit and apparently make it into a list.
  12.     private const int NUMBER_OF_CARDS = 52;         // This sets the max number of cards I guess.
  13.     private Random ranNum = new Random();           // Initiates the Random Handler for Shuffeling
  14.     public int Count() { get: return deck.Count; }  // returns the remaining Cards in the Deck
  15.  
  16.     /// <summary>
  17.     /// Initiator of Class "Deck"
  18.     /// </summary>
  19.     public Deck()                                                                                                                      
  20.     {
  21.         // Initating the Arrays containing all Card Values and Suits
  22.         string[] faces = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
  23.         string[] suits = { "Hearts", "Clubs", "Diamonds", "Spades" };
  24.  
  25.         // Looping through until we hit the NUMBER_OF_CARDS Limit
  26.         // Each Loop creating a new "Card" Class with it's unique face/suit combination
  27.         for (int count = 0; count < NUMBER_OF_CARDS; count++)
  28.         {
  29.             /*
  30.              * These are for debugging, not used but it's good to see how it's done in Unity since Console.WriteLine doesn't work there.
  31.             */
  32.             //int outcome = count % 13;
  33.             //int outcome2 = count / 13;    
  34.             //Debug.Log(outcome);
  35.             //Debug.Log(outcome2);
  36.  
  37.             // Create new Class "Card" with it's unique face/suit combination
  38.             // Using the Array Length of "faces" (13) to get all cards done nicely
  39.             deck.Add(new Card(faces[count % faces.Length], suits[count / faces.Length]));
  40.         }
  41.  
  42.     }
  43.     /// <summary>
  44.     /// Shuffles the Deck by randomly repacing Item positions within the Array/List
  45.     /// Or as John wrote: Shuffles the deck, using shit I have no idea about.
  46.     /// </summary>
  47.     public void Shuffle()  
  48.     {
  49.         for (int first = 0; first < deck.Count; first++)
  50.         {
  51.             int second = ranNum.Next(deck.Count);   // Chooses a random index
  52.             Card temp = deck[first];                // Reference to the Card in the Deck with Index "first" and saves it as Card "temp"
  53.             deck[first] = deck[second];             // Changes the indexed Cards in the Deck which now doubles the Card "deck[second]"
  54.             deck[second] = temp;                    // Saves the Card "temp" to the Deck, replacing the second double Card from the previous Step
  55.         }
  56.     }
  57.     /// <summary>
  58.     /// Returns the Topmost Card from the Deck
  59.     /// </summary>
  60.     /// <returns>Class: Card</returns>
  61.     public Card DealCard()
  62.     {
  63.         if (deck.Count >= 0)    //Checks if there's any cards to draw, maybe.
  64.         {
  65.             Card C = deck[0];           //References to the first Card in the Deck
  66.             deck.Remove(C);             // Removes the Card from the Deck
  67.             //Debug.Log(C.ToString());  // debug Stuff again
  68.             return C;                   // Return Value
  69.         }
  70.         else
  71.             return null;    //If shit hits the fan then null. Other code prevents this from happening though. Or it should.
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment