namespace MauMau_Server.Mau; public class Deck { private List _unusedDeck = new(); private List _usedDeck = new(); public Card CurrentCard; /** * * Creates a new deck instance with a new shuffled set of cards * */ public Deck() { CreateSet(); ShuffleDeck(); CurrentCard = DrawCard(); _usedDeck.Add(CurrentCard); } /** * * Adds the given card to the used cards deck. * * The card to add to the used cards deck. */ public void AddCardToUsedDeck(Card card) { _usedDeck.Add(card); } /** * * Adds the given list of cards to the used cards deck. * * The list of cards to add to the used cards deck. */ public void AddCardsToUsedDeck(IEnumerable cards) { _usedDeck.AddRange(cards); } /** * * Creates a new deck of cards and adds them to the unused deck. * */ private void CreateSet() { foreach (CardType cardType in Enum.GetValues(typeof(CardType))) { if (cardType == CardType.JOKER) { _unusedDeck.Add(new Card(cardType, CardValue.RED)); _unusedDeck.Add(new Card(cardType, CardValue.BLACK)); continue; } foreach (CardValue cardValue in Enum.GetValues(typeof(CardValue))) { if (cardValue is CardValue.RED or CardValue.BLACK) continue; _unusedDeck.Add(new Card(cardType, cardValue)); } } } /** * * Draws a card from the deck. * If the deck is empty, the deck is reshuffled with . * */ public Card DrawCard() { if (_unusedDeck.Count == 0) ReshuffleDeck(); var card = _unusedDeck[0]; _unusedDeck.RemoveAt(0); return card; } /** * * Take a given amount of cards from the deck. This method calls for each card. * * The amount of cards to draw from the deck. */ public IEnumerable DrawCards(int amount) { var cards = new List(); for (var i = 0; i < amount; i++) { cards.Add(DrawCard()); } return cards; } /** * * Moves all the used cards back to the unused deck and shuffles it. * If there are no cards to reshuffle, a new set of cards is created and shuffled. * */ private void ReshuffleDeck() { _unusedDeck.AddRange(_usedDeck); _usedDeck.Clear(); if (_unusedDeck.Count == 0) { CreateSet(); } ShuffleDeck(); } /** * * Shuffles all the cards in the deck using the Fisher-Yates algorithm. * */ private void ShuffleDeck() { _unusedDeck = _unusedDeck.OrderBy(x => Guid.NewGuid()).ToList(); } }