unit: do you create a random deck of 7 cards every time you use them all?

It turns out that this week I was showing this method to my students of game mechanics. 🙂

One of my favorite ways to work with a deck is to use an online version of Fisher-Yates Shuffle (also called Knuth shuffle)

IEnumerator ShuffleBag(params t[] articles) {
int remaining = items.Length;

while (true) {
// Choose a random object from what's left of our deck.
// (That is, the entries 0 ... remaining-1)
int drawIndex = Random.Range (0, remaining);
T drawnItem = elements[drawIndex];

// Remove the object from the deck by changing it to the end.
articles[drawIndex] = articles[remaining];
articles[remaining] = drawnItem;

// Present the element drawn as our new element .current,
// and wait until they ask us to draw again.
yield return drawItem;

// If we run out of letters when we ask for a new one,
// rearrange the discards to make a new deck.
yes (remaining == 0)
remaining = items.Length;

You can configure it like this:

var bag = ShuffleBag (

Then, draw a new tetromino from the bag like this:

bag.MoveNext ();
var tetromino = bag.current;

There will always be a new tetromino ready: the bag will automatically reorganize the discards when empty, and deliver them in a new random order.