IShuffle Redux: Explicitly Implementing shuffle

I'm back at it…

What I think I'm doing: public interface IDeck { void Shuffle(ConcurrentDictionary<int, Card> T ); }

void IDeck.Shuffle(ConcurrentDictionary<int, Card> T ) { Random RNG = new Random();

 if (T != null) { var n = T.Count; while (n > 1) { n--; var k = RNG.Next(n + 1); var value = T[k]; T[k] = T[n]; T[n] = value; } } else throw new NullReferenceException(); } 

but, when I make the call: IDeck.Shuffle(Cards); //Error: An object reference is required ???

The call happens in this context: public Deck() { const int MaxThreads = 2; const int NumberOfCards = 52; Cards = new ConcurrentDictionary<int, Card>(MaxThreads, NumberOfCards);

 var cardIndex = 0; foreach (var s in Suits) { foreach (var r in Ranks) { if (!Cards.TryAdd(cardIndex, new Card(r, s))) { Debug.WriteLine("WARNING: TryAdd failed when adding card to Cards"); } cardIndex++; } } IDeck.Shuffle(Cards); //Error: An object reference is required ??? } 

Hmm… Probably something simple, but I can't find anything helpful on MSDN.

Help, as always, is appreciated.

http://ift.tt/1Or4mpB

by SemanticAgitator via /r/csharp

Leave a Reply