'How to make this work without random but in turn?
Solution 1:[1]
Just as in real live:
Initially compose and shuffle the deck once
You could do this using Linq
OrderByand as sort factor useRandom.value.Or if it is actually not even desired to be shuffled then just ignore this step.
Then draw cards sequentially from the shuffled deck
I would use a
Queuewhich basically is the closest to a real live card deck: First-in first-out collection which allows to draw a card and at the same time remove it from the deck.
So something like e.g.
using System.Linq;
using System.Collections.Generic;
using UnityEngine;
...
private Queue<Card> deck;
public void InitializeDeck()
{
deck = new Queue<Card>(resourseManager.cards.OrderBy(c => Random.value));
// or if you want to skip the randomization entirely simply skip the OrderBy part
//deck = new Queue<Card>(resourseManager.cards);
}
public void NewCard()
{
if(deck == null)
{
Debug.LogError($"{nameof(deck)} is not initialized! You need to call {nameof(Initialize)} before you can call {nameof(NewCard)}!", this);
return;
}
if(deck.Count == 0)
{
Debug.LogError($"No cards available, {nameof(deck)} is empty!", this);
return;
}
// returns the next card and removes it from the deck
var card = deck.Dequeue();
LoadCard(card);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |







