developmaunt #5
25
Mau/Deck.cs
25
Mau/Deck.cs
@@ -13,9 +13,16 @@ public class Deck
|
|||||||
*/
|
*/
|
||||||
public Deck()
|
public Deck()
|
||||||
{
|
{
|
||||||
|
// Create a new set of cards
|
||||||
CreateSet();
|
CreateSet();
|
||||||
|
|
||||||
|
// Shuffle the deck
|
||||||
ShuffleDeck();
|
ShuffleDeck();
|
||||||
|
|
||||||
|
// Draw the first card
|
||||||
var initialCard = DrawCard();
|
var initialCard = DrawCard();
|
||||||
|
|
||||||
|
// Set the current card to the first card
|
||||||
CurrentCard = initialCard;
|
CurrentCard = initialCard;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,7 +34,10 @@ public class Deck
|
|||||||
*/
|
*/
|
||||||
public void AddCardToUsedDeck(Card card)
|
public void AddCardToUsedDeck(Card card)
|
||||||
{
|
{
|
||||||
|
// Add the card to the used deck
|
||||||
_usedDeck.Add(card);
|
_usedDeck.Add(card);
|
||||||
|
|
||||||
|
// Set the current card to the given card
|
||||||
CurrentCard = card;
|
CurrentCard = card;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,9 +60,13 @@ public class Deck
|
|||||||
*/
|
*/
|
||||||
public Card DrawCard()
|
public Card DrawCard()
|
||||||
{
|
{
|
||||||
|
// Check if the deck is empty, if so, reshuffle it
|
||||||
if (_unusedDeck.Count == 0) ReshuffleDeck();
|
if (_unusedDeck.Count == 0) ReshuffleDeck();
|
||||||
|
|
||||||
|
// Grab the first card from the deck and remove it
|
||||||
var card = _unusedDeck[0];
|
var card = _unusedDeck[0];
|
||||||
_unusedDeck.RemoveAt(0);
|
_unusedDeck.RemoveAt(0);
|
||||||
|
|
||||||
return card;
|
return card;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,6 +78,7 @@ public class Deck
|
|||||||
*/
|
*/
|
||||||
public IEnumerable<Card> DrawCards(int amount)
|
public IEnumerable<Card> DrawCards(int amount)
|
||||||
{
|
{
|
||||||
|
// Create a list of cards and add the drawn cards to it
|
||||||
var cards = new List<Card>();
|
var cards = new List<Card>();
|
||||||
for (var i = 0; i < amount; i++)
|
for (var i = 0; i < amount; i++)
|
||||||
{
|
{
|
||||||
@@ -103,14 +118,17 @@ public class Deck
|
|||||||
*/
|
*/
|
||||||
private void ReshuffleDeck()
|
private void ReshuffleDeck()
|
||||||
{
|
{
|
||||||
|
// Move all used cards back to the unused deck
|
||||||
_unusedDeck.AddRange(_usedDeck);
|
_unusedDeck.AddRange(_usedDeck);
|
||||||
_usedDeck.Clear();
|
_usedDeck.Clear();
|
||||||
|
|
||||||
if (_unusedDeck.Count == 0)
|
// If there are no cards left, create a new set and add it
|
||||||
|
if (!_unusedDeck.Any())
|
||||||
{
|
{
|
||||||
CreateSet();
|
CreateSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Shuffle the deck
|
||||||
ShuffleDeck();
|
ShuffleDeck();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,9 +139,14 @@ public class Deck
|
|||||||
*/
|
*/
|
||||||
private void ShuffleDeck()
|
private void ShuffleDeck()
|
||||||
{
|
{
|
||||||
|
// Clear the unused ceck
|
||||||
var unusedDeckCopy = new List<Card>(_unusedDeck);
|
var unusedDeckCopy = new List<Card>(_unusedDeck);
|
||||||
_unusedDeck.Clear();
|
_unusedDeck.Clear();
|
||||||
|
|
||||||
|
// Shuffle the deck
|
||||||
unusedDeckCopy = unusedDeckCopy.OrderBy(x => Guid.NewGuid()).ToList();
|
unusedDeckCopy = unusedDeckCopy.OrderBy(x => Guid.NewGuid()).ToList();
|
||||||
|
|
||||||
|
// Add the shuffled deck back to the unused deck
|
||||||
_unusedDeck.AddRange(unusedDeckCopy);
|
_unusedDeck.AddRange(unusedDeckCopy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user