'How to make this work without random but in turn?

1st part of the code

2d part of the code

3d part of the code

4th part of the code

5th part of the code

6th part of the code

7th part of the code

Can anyone help? The cards on the table are generated randomly, according to this code, there are 5 cards. The question is how to make them not randomly generated, but in turn, one by one



Solution 1:[1]

Just as in real live:

  • Initially compose and shuffle the deck once

    You could do this using Linq OrderBy and as sort factor use Random.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 Queue which 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