//-----------------------------------------------------------------------------
// CardsCollection.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
namespace CardsFramework
{
///
/// Card related holding event information of a
///
public class CardEventArgs : EventArgs
{
public TraditionalCard Card { get; set; }
}
///
/// A packet of cards
///
///
/// A card packet may be initialized with a collection of cards.
/// It may lose cards or deal them to , but may
/// not receive new cards unless derived and overridden.
///
public class CardPacket
{
protected List cards { get; set; }
///
/// An event which triggers when a card is removed from the collection.
///
public event EventHandler LostCard;
public int Count { get { return cards.Count; } }
///
/// Initializes a card collection by simply allocating a new card list.
///
protected CardPacket()
{
cards = new List();
}
///
/// Returns a card at a specified index in the collection.
///
/// The card's index.
/// The card at the specified index.
public TraditionalCard this[int index]
{
get
{
return cards[index];
}
}
///
/// Initializes a new instance of the class.
///
/// The number of decks to add to
/// the collection.
/// The amount of jokers in each deck.
/// The suits to add to each decks. Suits are specified
/// as flags and several can be added.
/// The card values which will appear in each deck.
/// values are specified as flags and several can be added.
public CardPacket(int numberOfDecks, int jokersInDeck,
CardSuit suits, CardValue cardValues)
{
cards = new List();
for (int deckIndex = 0; deckIndex < numberOfDecks; deckIndex++)
{
AddSuit(suits, cardValues);
for (int j = 0; j < jokersInDeck / 2; j++)
{
cards.Add(new TraditionalCard(CardSuit.Club,
CardValue.FirstJoker, this));
cards.Add(new TraditionalCard(CardSuit.Club,
CardValue.SecondJoker, this));
}
if (jokersInDeck % 2 == 1)
{
cards.Add(new TraditionalCard(CardSuit.Club,
CardValue.FirstJoker, this));
}
}
}
///
/// Adds suits of cards to the collection.
///
/// The suits to add to each decks. Suits are specified
/// as flags and several can be added.
/// The card values which will appear in each deck.
/// values are specified as flags and several can be added.
private void AddSuit(CardSuit suits, CardValue cardValues)
{
if ((suits & CardSuit.Club) == CardSuit.Club)
{
AddCards(CardSuit.Club, cardValues);
}
if ((suits & CardSuit.Diamond) == CardSuit.Diamond)
{
AddCards(CardSuit.Diamond, cardValues);
}
if ((suits & CardSuit.Heart) == CardSuit.Heart)
{
AddCards(CardSuit.Heart, cardValues);
}
if ((suits & CardSuit.Spade) == CardSuit.Spade)
{
AddCards(CardSuit.Spade, cardValues);
}
}
///
/// Adds cards to the collection.
///
/// The suit of the added cards.
/// The card values which will appear in each deck.
/// values are specified as flags and several can be added.
private void AddCards(CardSuit suit,
CardValue cardValues)
{
if ((cardValues & CardValue.Ace) == CardValue.Ace)
{
cards.Add(new TraditionalCard(suit, CardValue.Ace, this));
}
if ((cardValues & CardValue.Two) == CardValue.Two)
{
cards.Add(new TraditionalCard(suit, CardValue.Two, this));
}
if ((cardValues & CardValue.Three) == CardValue.Three)
{
cards.Add(new TraditionalCard(suit, CardValue.Three, this));
}
if ((cardValues & CardValue.Four) == CardValue.Four)
{
cards.Add(new TraditionalCard(suit, CardValue.Four, this));
}
if ((cardValues & CardValue.Five) == CardValue.Five)
{
cards.Add(new TraditionalCard(suit, CardValue.Five, this));
}
if ((cardValues & CardValue.Six) == CardValue.Six)
{
cards.Add(new TraditionalCard(suit, CardValue.Six, this));
}
if ((cardValues & CardValue.Seven) == CardValue.Seven)
{
cards.Add(new TraditionalCard(suit, CardValue.Seven, this));
}
if ((cardValues & CardValue.Eight) == CardValue.Eight)
{
cards.Add(new TraditionalCard(suit, CardValue.Eight, this));
}
if ((cardValues & CardValue.Nine) == CardValue.Nine)
{
cards.Add(new TraditionalCard(suit, CardValue.Nine, this));
}
if ((cardValues & CardValue.Ten) == CardValue.Ten)
{
cards.Add(new TraditionalCard(suit, CardValue.Ten, this));
}
if ((cardValues & CardValue.Jack) == CardValue.Jack)
{
cards.Add(new TraditionalCard(suit, CardValue.Jack, this));
}
if ((cardValues & CardValue.Queen) == CardValue.Queen)
{
cards.Add(new TraditionalCard(suit, CardValue.Queen, this));
}
if ((cardValues & CardValue.King) == CardValue.King)
{
cards.Add(new TraditionalCard(suit, CardValue.King, this));
}
}
///
/// Shuffles the cards in the packet by randomly changing card placement.
///
public void Shuffle()
{
Random random = new Random();
List shuffledDeck = new List();
while (cards.Count > 0)
{
TraditionalCard card = cards[random.Next(0, cards.Count)];
cards.Remove(card);
shuffledDeck.Add(card);
}
cards = shuffledDeck;
}
///
/// Shuffles the cards in the packet using a specified seed for deterministic shuffling.
///
/// The seed for the random number generator.
public void Shuffle(int seed)
{
Random random = new Random(seed);
List shuffledDeck = new List();
while (cards.Count > 0)
{
TraditionalCard card = cards[random.Next(0, cards.Count)];
cards.Remove(card);
shuffledDeck.Add(card);
}
cards = shuffledDeck;
}
///
/// Removes the specified card from the packet. The first matching card
/// will be removed.
///
/// The card to remove.
/// The card that was removed from the collection.
///
/// Please note that removing a card from a packet may only be performed internally by
/// other card-framework classes to maintain the principle that a card may only be held
/// by one only at any given time.
///
internal TraditionalCard Remove(TraditionalCard card)
{
if (cards.Contains(card))
{
cards.Remove(card);
if (LostCard != null)
{
LostCard(this, new CardEventArgs() { Card = card });
}
return card;
}
return null;
}
///
/// Removes all the cards from the collection.
///
/// A list of all the cards that were removed.
internal List Remove()
{
List cards = this.cards;
this.cards = new List();
return cards;
}
///
/// Deals the first card from the collection to a specified hand.
///
/// The destination hand.
/// The card that was moved to the hand.
public TraditionalCard DealCardToHand(Hand destinationHand)
{
TraditionalCard firstCard = cards[0];
firstCard.MoveToHand(destinationHand);
return firstCard;
}
///
/// Deals several cards to a specified hand.
///
/// The destination hand.
/// The amount of cards to deal.
/// A list of the cards that were moved to the hand.
public List DealCardsToHand(Hand destinationHand, int count)
{
List dealtCards = new List();
for (int cardIndex = 0; cardIndex < count; cardIndex++)
{
dealtCards.Add(DealCardToHand(destinationHand));
}
return dealtCards;
}
}
}