How to code card seperating
Hi How to C sharp Code for 52 cards should randomly divide by to and 26 card should one side and another 26 card other side please healp me
Answer by Karishi · Sep 09, 2020 at 11:11 PM
If your deck is a List of Card gameObjects and your two subdecks are as well, you can grab a random card from that list, add it to the first subdeck.
Repeat this using a loop until you've done it for half your deck size.
int halfDeckSize = deck.Count / 2;
while (deck.Count > halfDeckSize)
{
int nextCard = Random.Range(0, deck.Count);
subDeck1Cards.Add(deck[nextCard]);
deck.Remove(deck[nextCard]);
}
Then do a similar loop to shuffle the remaining cards into the second subdeck.
while (deck.Count > 0)
{
int nextCard = Random.Range(0, deck.Count);
subDeck2Cards.Add(deck[nextCard]);
deck.Remove(deck[nextCard]);
}
This should end you up with two shuffled half-decks.
Your answer
Follow this Question
Related Questions
Creating obj/prefab > Card. Better way? 0 Answers
struggling with Playing card game in unity 2 Answers
Creating card game - copy of list to new list - creating cycle 1 Answer
How to change UI Image Sprite from a list with UI Buttons to navigate back and forth? 0 Answers
Digital Card Game Phases 0 Answers