From 25b7a8fb1e823a1bee6000f606cd38b3816817a6 Mon Sep 17 00:00:00 2001 From: DTieman Date: Wed, 12 Jun 2024 16:07:31 +0200 Subject: [PATCH] Solved sonarlint issue and some more code documentation --- Mau/Deck.cs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/Mau/Deck.cs b/Mau/Deck.cs index a45b1ec..3faaec8 100644 --- a/Mau/Deck.cs +++ b/Mau/Deck.cs @@ -13,9 +13,16 @@ public class Deck */ 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; } @@ -27,7 +34,10 @@ public class Deck */ 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; } @@ -50,9 +60,13 @@ public class Deck */ 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; } @@ -64,6 +78,7 @@ public class Deck */ public IEnumerable DrawCards(int amount) { + // Create a list of cards and add the drawn cards to it var cards = new List(); for (var i = 0; i < amount; i++) { @@ -103,14 +118,17 @@ public class Deck */ private void ReshuffleDeck() { + // Move all used cards back to the unused deck _unusedDeck.AddRange(_usedDeck); _usedDeck.Clear(); - if (_unusedDeck.Count == 0) + // If there are no cards left, create a new set and add it + if (!_unusedDeck.Any()) { CreateSet(); } + // Shuffle the deck ShuffleDeck(); } @@ -121,9 +139,14 @@ public class Deck */ private void ShuffleDeck() { + // Clear the unused ceck var unusedDeckCopy = new List(_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); } } \ No newline at end of file