Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CSharp/Blackbaud.Interview.Cards/Deck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,16 @@ public Card NextCard()
return null;
}
}
public void ShuffleCards(IShuffler shuffler, int? shuffleRounds = null, int? seed = null)
{
if(shuffler is null) throw new ArgumentNullException(nameof(shuffler));
var cards=_stackOfCards.Reverse().ToList();
shuffler.shuffle(cards,shuffleRounds,seed);
_stackOfCards.Clear();
foreach(var card in cards)
{
_stackOfCards.Push(card);
}
}

}
4 changes: 2 additions & 2 deletions CSharp/Blackbaud.Interview.Cards/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ static void Main()

// Create a new deck
var deck = Deck.NewDeck();

IShuffler shuffler=new FisherYatesShuffler();
// TODO: shuffle the deck
Console.WriteLine("Shuffling...");

deck.ShuffleCards(shuffler,shuffleRounds:100,seed:null);
// Deal all the cards
while (!deck.Empty)
{
Expand Down
6 changes: 6 additions & 0 deletions CSharp/Blackbaud.Interview.Cards/Shuffler/IShuffler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Blackbaud.Interview.Cards.Shuffler;

public interface IShuffler
{
void shuffle(IList<Card> cards,int?shuffleRounds=null,int?seed=null);
}
32 changes: 32 additions & 0 deletions CSharp/Blackbaud.Interview.Cards/Shuffler/Shuffler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace Blackbaud.Interview.Cards.Shuffler;

public class FisherYatesShuffler : IShuffler
{
public void shuffle(IList<Card> cards, int? shuffleRounds = null, int? seed = null)
{
if(cards is null) throw new ArgumentNullException(nameof(cards));
int n=cards.Count;
Random rand = seed.HasValue ? new Random(seed.Value) : new Random();
if(shuffleRounds==null)
{
for(int i=n-1;i>0;i--)
{
int j=rand.Next(i+1);
(cards[i],cards[j])=(cards[j],cards[i]);
}
}
else
{
int rounds=shuffleRounds.Value;
for(int r = 0; r < rounds; r++)
{
int i=rand.Next(n);
int j=rand.Next(n);
if(i==j) continue;
(cards[i],cards[j])=(cards[j],cards[i]);
}
}
}


}