152 lines
4.2 KiB
C#
152 lines
4.2 KiB
C#
namespace MauMau_Server.Mau;
|
|
|
|
public class Deck
|
|
{
|
|
private readonly List<Card> _unusedDeck = new();
|
|
private readonly List<Card> _usedDeck = new();
|
|
public Card CurrentCard { get; private set; }
|
|
|
|
/**
|
|
* <summary>
|
|
* Creates a new deck instance with a new shuffled set of cards
|
|
* </summary>
|
|
*/
|
|
public Deck()
|
|
{
|
|
// Create a new set of cards
|
|
CreateSet();
|
|
|
|
// Shuffle the deck
|
|
ShuffleDeck();
|
|
|
|
// Draw the first card
|
|
var initialCard = DrawCard();
|
|
|
|
// Set the current card to the first card
|
|
CurrentCard = initialCard;
|
|
}
|
|
|
|
/**
|
|
* <summary>
|
|
* Adds the given card to the used cards deck and sets the current card to the given card.
|
|
* </summary>
|
|
* <param name="card">The card to add to the used cards deck.</param>
|
|
*/
|
|
public void AddCardToUsedDeck(Card card)
|
|
{
|
|
// Add the card to the used deck
|
|
_usedDeck.Add(card);
|
|
|
|
// Set the current card to the given card
|
|
CurrentCard = card;
|
|
}
|
|
|
|
/**
|
|
* <summary>
|
|
* Adds the given list of cards to the used cards deck.
|
|
* </summary>
|
|
* <param name="cards">The list of cards to add to the used cards deck.</param>
|
|
*/
|
|
public void AddCardsToUsedDeck(IEnumerable<Card> cards)
|
|
{
|
|
_usedDeck.AddRange(cards);
|
|
}
|
|
|
|
/**
|
|
* <summary>
|
|
* Draws a card from the deck.
|
|
* If the deck is empty, the deck is reshuffled with <see cref="ReshuffleDeck"/>.
|
|
* </summary>
|
|
*/
|
|
public Card DrawCard()
|
|
{
|
|
// Check if the deck is empty, if so, reshuffle it
|
|
if (_unusedDeck.Count == 0) ReshuffleDeck();
|
|
|
|
// Grab the first card from the deck and remove it
|
|
var card = _unusedDeck[0];
|
|
_unusedDeck.RemoveAt(0);
|
|
|
|
return card;
|
|
}
|
|
|
|
/**
|
|
* <summary>
|
|
* Take a given amount of cards from the deck. This method calls <see cref="DrawCard"/> for each card.
|
|
* </summary>
|
|
* <param name="amount">The amount of cards to draw from the deck.</param>
|
|
*/
|
|
public IEnumerable<Card> DrawCards(int amount)
|
|
{
|
|
// Create a list of cards and add the drawn cards to it
|
|
var cards = new List<Card>();
|
|
for (var i = 0; i < amount; i++)
|
|
{
|
|
cards.Add(DrawCard());
|
|
}
|
|
return cards;
|
|
}
|
|
|
|
/**
|
|
* <summary>
|
|
* Creates a new deck of cards and adds them to the unused deck.
|
|
* </summary>
|
|
*/
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* <summary>
|
|
* 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.
|
|
* </summary>
|
|
*/
|
|
private void ReshuffleDeck()
|
|
{
|
|
// Move all used cards back to the unused deck
|
|
_unusedDeck.AddRange(_usedDeck);
|
|
_usedDeck.Clear();
|
|
|
|
// If there are no cards left, create a new set and add it
|
|
if (!_unusedDeck.Any())
|
|
{
|
|
CreateSet();
|
|
}
|
|
|
|
// Shuffle the deck
|
|
ShuffleDeck();
|
|
}
|
|
|
|
/**
|
|
* <summary>
|
|
* Shuffles all the cards in the deck using the Fisher-Yates algorithm.
|
|
* </summary>
|
|
*/
|
|
private void ShuffleDeck()
|
|
{
|
|
// Clear the unused ceck
|
|
var unusedDeckCopy = new List<Card>(_unusedDeck);
|
|
_unusedDeck.Clear();
|
|
|
|
// Shuffle the deck
|
|
unusedDeckCopy = unusedDeckCopy.OrderBy(x => Guid.NewGuid()).ToList();
|
|
|
|
// Add the shuffled deck back to the unused deck
|
|
_unusedDeck.AddRange(unusedDeckCopy);
|
|
}
|
|
} |