/// <summary> /// Breaks deck into as many as 7 piles and places the cards one by one into piles /// </summary> /// <param name="deck"></param> /// <returns></returns> public CardCollectionOperationResult PileShuffle(Deck deck) { int pileCount = Math.Min(DEFAULT_PILE_COUNT, deck.Count); int cardCount = deck.Count; LinkedList<Deck> piles = new LinkedList<Deck>(new Deck[pileCount]); for (int i = 0; i < cardCount; ++i) { Card card = deck.ElementAt(0); Deck currentPile = piles.ElementAt(0); deck.RemoveAt(0); currentPile.Insert(0, card); piles.AddLast(currentPile); piles.RemoveFirst(); } if (deck.Count == 0) deck = new Deck(); foreach (Deck pile in piles) deck.AddRange(pile); return new CardCollectionOperationResult(deck); }
public CardCollectionOperationResult OverhandShuffle(Deck deck) { Deck rightHandHalf = new Deck(deck.GetRange(0, (deck.Count / 2))); Deck leftHandHalf = new Deck(deck.GetRange((deck.Count / 2), deck.Count)); int overhandChunk = (Math.Max(rightHandHalf.Count, leftHandHalf.Count) / DEFAULT_OVERHAND_CHUNKS); deck = new Deck(); while (rightHandHalf.Count > 0) { int takeChunk = Math.Min(overhandChunk, rightHandHalf.Count); deck.InsertRange(0, rightHandHalf.Take(takeChunk)); rightHandHalf.RemoveRange(0, takeChunk); } while (leftHandHalf.Count > 0) { int takeChunk = Math.Min(overhandChunk, leftHandHalf.Count); deck.InsertRange(0, leftHandHalf.Take(takeChunk)); leftHandHalf.RemoveRange(0, takeChunk); } return new CardCollectionOperationResult(deck); }
/// <summary> /// Places a card randomly inside the deck /// </summary> /// <param name="deck"></param> /// <param name="card"></param> /// <returns></returns> public CardCollectionOperationResult PlaceRandom(Deck deck, Card card) { CheckDeckIntegrity(deck); Random random = new Random(); return PlaceCardAtIndex(deck, card, random.Next(0, deck.Count)); }
/// <summary> /// Places a card on the bottom of the deck /// </summary> /// <param name="deck"></param> /// <param name="card"></param> /// <returns></returns> public CardCollectionOperationResult PlaceBottom(Deck deck, Card card) { CheckDeckIntegrity(deck); deck.Add(card); return new CardCollectionOperationResult(deck); }
/// <summary> /// Removes a random card from the deck /// </summary> /// <param name="deck"></param> /// <returns></returns> public CardCollectionOperationResult RemoveRandom(Deck deck) { CheckDeckIntegrity(deck); Random random = new Random(); return RemoveCardAtIndex(deck, random.Next(0, deck.Count)); }
/// <summary> /// Removes the bottom card of the deck /// </summary> /// <param name="deck"></param> /// <returns></returns> public CardCollectionOperationResult RemoveBottom(Deck deck) { CheckDeckIntegrity(deck); return RemoveCardAtIndex(deck, deck.Count); }
/// <summary> /// Places a card on top of the deck /// </summary> /// <param name="deck"></param> /// <param name="card"></param> /// <returns></returns> public CardCollectionOperationResult PlaceTop(Deck deck, Card card) { CheckDeckIntegrity(deck); return PlaceCardAtIndex(deck, card, 0); }
/// <summary> /// Removes a card a specified index /// </summary> /// <param name="deck"></param> /// <param name="index"></param> /// <returns></returns> private CardCollectionOperationResult RemoveCardAtIndex(Deck deck, int index) { Card card = deck[index]; deck.RemoveAt(index); return new CardCollectionOperationResult(deck, card); }
/// <summary> /// Places a card at a specified index of the deck /// </summary> /// <param name="deck"></param> /// <param name="card"></param> /// <param name="index"></param> /// <returns></returns> private CardCollectionOperationResult PlaceCardAtIndex(Deck deck, Card card, int index) { deck.Insert(index, card); return new CardCollectionOperationResult(deck); }
/// <summary> /// Checks to make sure the deck is initialized /// </summary> /// <param name="deck"></param> private void CheckDeckIntegrity(Deck deck) { if (deck == null) throw new EmptyDeckException("Null deck found while trying to shuffle"); }
/// <summary> /// Shuffles the deck /// </summary> /// <param name="deck"></param> public CardCollectionOperationResult Shuffle(Deck deck) { CheckDeckIntegrity(deck); for (int i = 0; i < DEFAULT_SHUFFLE_COUNT; ++i) { CardShuffler shuffler = new CardShuffler(); CardCollectionOperationResult resultantShuffle; Random random = new Random(); switch (random.Next(3)) { case 0: resultantShuffle = shuffler.RifleShuffle(deck); break; case 1: resultantShuffle = shuffler.OverhandShuffle(deck); break; case 2: resultantShuffle = shuffler.PileShuffle(deck); break; default: resultantShuffle = shuffler.RifleShuffle(deck); break; } deck = resultantShuffle.Deck; } return new CardCollectionOperationResult(deck); }
/// <summary> /// Removes the top card of the deck /// </summary> /// <param name="deck"></param> /// <returns></returns> public CardCollectionOperationResult RemoveTop(Deck deck) { CheckDeckIntegrity(deck); return RemoveCardAtIndex(deck, 0); }
public CardCollectionOperationResult RifleShuffle(Deck deck) { Deck rightHandHalf = new Deck(deck.GetRange(0, (deck.Count / 2))); Deck leftHandHalf = new Deck(deck.GetRange((deck.Count / 2), deck.Count)); Random random = new Random(); int rifleChunk = 0; deck = new Deck(); while (rightHandHalf.Count > 0 || leftHandHalf.Count > 0) { if (rightHandHalf.Count > 0) { rifleChunk = Math.Min(random.Next(1, 3), rightHandHalf.Count); deck.AddRange(rightHandHalf.Take(rifleChunk)); rightHandHalf.RemoveRange(0, rifleChunk); } if (leftHandHalf.Count > 0) { rifleChunk = Math.Min(random.Next(1, 3), leftHandHalf.Count); deck.AddRange(leftHandHalf.Take(rifleChunk)); leftHandHalf.RemoveRange(0, rifleChunk); } } return new CardCollectionOperationResult(deck); }