Partial rewrite, missing:
- Correctly parsing incoming messages - Sending the gamestate after relevant actions
This commit is contained in:
91
Mau/Deck.cs
91
Mau/Deck.cs
@@ -2,47 +2,86 @@
|
||||
|
||||
public class Deck
|
||||
{
|
||||
public List<Card> UnusedDeck = new();
|
||||
public List<Card> UsedDeck = new();
|
||||
private List<Card> _unusedDeck = new();
|
||||
private List<Card> _usedDeck = new();
|
||||
|
||||
/**
|
||||
* <summary>
|
||||
* Creates a new deck instance with a new shuffled set of cards
|
||||
* </summary>
|
||||
*/
|
||||
public Deck()
|
||||
{
|
||||
CreateSet();
|
||||
ShuffleDeck();
|
||||
}
|
||||
|
||||
/**
|
||||
* <summary>
|
||||
* Adds the given card to the used cards deck.
|
||||
* </summary>
|
||||
* <param name="card">The card to add to the used cards deck.</param>
|
||||
*/
|
||||
public void AddCardToUsedDeck(Card card)
|
||||
{
|
||||
_usedDeck.Add(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>
|
||||
* 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));
|
||||
_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));
|
||||
_unusedDeck.Add(new Card(cardType, cardValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<Card> GetUnusedDeck()
|
||||
{
|
||||
return UnusedDeck;
|
||||
}
|
||||
|
||||
/**
|
||||
* <summary>
|
||||
* Draws a card from the deck.
|
||||
* If the deck is empty, the deck is reshuffled with <see cref="ReshuffleDeck"/>.
|
||||
* </summary>
|
||||
*/
|
||||
public Card DrawCard()
|
||||
{
|
||||
if (UnusedDeck.Count == 0) ReshuffleDeck();
|
||||
var card = UnusedDeck[0];
|
||||
UnusedDeck.RemoveAt(0);
|
||||
if (_unusedDeck.Count == 0) ReshuffleDeck();
|
||||
var card = _unusedDeck[0];
|
||||
_unusedDeck.RemoveAt(0);
|
||||
return card;
|
||||
}
|
||||
|
||||
public List<Card> DrawCards(int amount)
|
||||
/**
|
||||
* <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)
|
||||
{
|
||||
var cards = new List<Card>();
|
||||
for (var i = 0; i < amount; i++)
|
||||
@@ -51,18 +90,19 @@ public class Deck
|
||||
}
|
||||
return cards;
|
||||
}
|
||||
|
||||
public void AddCardToUsedDeck(Card card)
|
||||
{
|
||||
UsedDeck.Add(card);
|
||||
}
|
||||
|
||||
/**
|
||||
* <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()
|
||||
{
|
||||
UnusedDeck.AddRange(UsedDeck);
|
||||
UsedDeck.Clear();
|
||||
_unusedDeck.AddRange(_usedDeck);
|
||||
_usedDeck.Clear();
|
||||
|
||||
if (UnusedDeck.Count == 0)
|
||||
if (_unusedDeck.Count == 0)
|
||||
{
|
||||
CreateSet();
|
||||
}
|
||||
@@ -70,8 +110,13 @@ public class Deck
|
||||
ShuffleDeck();
|
||||
}
|
||||
|
||||
/**
|
||||
* <summary>
|
||||
* Shuffles all the cards in the deck using the Fisher-Yates algorithm.
|
||||
* </summary>
|
||||
*/
|
||||
private void ShuffleDeck()
|
||||
{
|
||||
UnusedDeck = UnusedDeck.OrderBy(x => Guid.NewGuid()).ToList();
|
||||
_unusedDeck = _unusedDeck.OrderBy(x => Guid.NewGuid()).ToList();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user