Hello, I'm trying to make it so my deck shuffles and when it draws the cards to the player it removes the cards it has given to player. I've managed to make it so when I press "S" it takes a random card from the Deck and puts it into the player1 pile. I'm not sure if I'm doing this correct if not could someone point me in the right direction? as you can see I'm trying to draw 6 cards into the players hand. Code (CSharp): using UnityEngine; using System.Collections; using System.Collections.Generic; public class deal_cards : MonoBehaviour { // Use this for initialization public bool shuffling = false; public List<GameObject> deckofCards= new List<GameObject>(); public List<GameObject> player1Hand = new List<GameObject>(); void Start() { } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.S)) { int newNumber = Random.Range(0, deckofCards.Count); // for (int i = 0; i < letters.Count; i++) { player1Hand.Add(deckofCards[newNumber]); //letters.Remove(tempLetters[newNumber]); } } if(Input.GetKeyDown(KeyCode.S) && player1Hand.Count == 6) { } } }
Looks good. Add to player list then remove from deck list. You could even make the deckofcards and playershand classes and add a removefromdeck and addtodeck method and expose them. You could go as far as making the cards themselves a class so that you can give each one an individual set of attributes
Just something quick: Code (CSharp): if (Input.GetKeyDown(KeyCode.S)) { if (deckofCards.Count >= 6) { // Draw 6 cards into Player's hand for (int i = 0; i < 6; i++) { int newNumber = Random.Range(0, deckofCards.Count); player1Hand.Add(deckofCards[newNumber]); deckofCards.RemoveAt(newNumber); } // Show Player's hand when done for (int i = 0; i < player1Hand.Count; i++) { Debug.Log("Player has " + player1Hand[i]); } } else { Debug.Log("Not enough cards. Only " + deckofCards.Count + " remains"); } }
Be aware, though, that if remove an element in the middle of the list, you're going to have an empty space. [x,y,z] RemoveAt(1) [x, null, z] And now you have a Count of 2, but 3 spaces in the list. You can take care of this by moving the last item to the empty space.
You don't need to if using a list, since remove will do that for you. You should be able to keep grabbing things using Random.Range(0, list.Count)
I kinda understand what you mean pal, could you do a quick example of what you mean fully so i can get a better understanding as I'm still learning. You dont have to make a extremely detailed example just a simple example showing me what you mean
Cheers pal, would I use the same way to shuffle a deck from one list to another list? using the random.range?
Lists resize automatically. I would deft go with pixpushes example since its functionally sound and I'm on an iPad now and cannot give you an example. I think that is e best solution.
I've made it so i can shuffle the deck and then put the randomly picked cards from the shuffled deck into the players, the only problem I'm having is that it duplicates the card which is dealt. Is they anyway I can check to see if the prefab is being used, bare in mind I wanna load the cards from the resource folder in the future so I can add more decks to play with.
I'm making a card game too, so let me lend you a hand . This is how I manage to shuffle the "Deck". This will be done once, at the beginning of the game so you don't have to do Random.Range every time. Code (CSharp): public void Shuffle<T>(this IList<T> ts) { var count = ts.Count; var last = count - 1; for (var i = 0; i < last; ++i) { var r = Random.Range(i, count); var tmp = ts[i]; ts[i] = ts[r]; ts[r] = tmp; } } After we shuffle the list we convert it to a Queue. Code (CSharp): CardsQueue = new Queue<CardsClass>(ShuffledList); Now if we need to draw the next card, just dequeue the top one from the deck and it will be removed automatically from the deck. Code (CSharp): CardClass nextcard = CardsQueue.Dequeue(); //Do whatever you want with card nextcard.GivetoPlayer(); Hope it helps.
I've already got it to shuffle the cards by putting the deck randomly into another then getting it to draw said amount from shuffle deck into the players hand... for what the cards are going to do, i was thinking more dragging and dropping the cards as both players will be dragging the cards from each hand to start a pile. Cheers though i see how it looks for the first card on the deck so that comes in handy