mercredi 17 août 2016

How to use the same random seed between Windows Devices?

I am trying to shuffle cards in a deck using the same random seed so the decks will be random, but synced on both clients. I am using the following shuffle algorithm:

    internal void ShuffleDeck(int randomSeed)
    {
        _random = new Random(randomSeed);
        Cards.Card[] toShuffle = CardsInDeck.ToArray();
        Shuffle<Cards.Card>(toShuffle);
        CardsInDeck = toShuffle.ToList<Cards.Card>();
    }

    /// <summary>
    /// Shuffle the array.
    /// </summary>
    /// <typeparam name="T">Array element type.</typeparam>
    /// <param name="array">Array to shuffle.</param>
    private static void Shuffle<T>(T[] array)
    {
        int n = array.Length;
        for (int i = 0; i < n; i++)
        {
            // NextDouble returns a random number between 0 and 1.
            // ... It is equivalent to Math.random() in Java.
            int r = i + (int)(_random.NextDouble() * (n - i));
            T t = array[r];
            array[r] = array[i];
            array[i] = t;
        }
    }

When I run two instances of my card game on the same machine, the cards are shuffled and synced on both clients as expected, but when I run one instance on my computer and another in a HoloLens emulator, the cards use the same seed, but cards are not synced. Is there anyway to shuffle the cards and have them synced across multiple clients?

By Synced, I mean they are shuffled in the exact same way. IE when I run both clients the first time with four cards (a,b,c,d) the deck order is (b,c,a,d) on both clients. When I run the clients the second time the deck order is (c,d,a,b) on both clients.




Aucun commentaire:

Enregistrer un commentaire