Implementation by Example

Creating a ‘Happy Families’ Deck

Happy families is a children's card game, similar to Go Fish (http://en.wikipedia.org/wiki/Happy_Families)

The source already exists in the project.

Creating a card class

First it is necessary to implement the ICard interface.

This is simple enough but supporting code is required to adequately  describe the card, so in this example CardFamily & CardTitle classes have been created.

It is important to ensure the FromDeckId property is set.

public class HappyFamiliesCard : ICard
  {
      public HappyFamiliesCard(CardFamily cardFamily, CardTitle cardTitle, Guid deckId)
      {
          CardFamily = cardFamily;
          CardTitle = cardTitle;
          FromDeckId = deckId;
          Code = cardTitle + cardFamily.Name;
      }

      public CardFamily CardFamily { get; set; }
      public CardTitle CardTitle { get; set; }

      #region ICard Members

      public string Code { get; private set; }
      public Guid FromDeckId { get; set; }
      public ICardSet AssignedTo { get; set; }
      public int StackOrder { get; set; }

      public string Description
      {
          get
          {
              var result = string.Format("{0} {1} the {2}", CardTitle, CardFamily.Name, CardFamily.Profession);

              switch (CardTitle)
              {
                  case CardTitle.Mrs:
                      return result + "'s Wife";
                  case CardTitle.Master:
                      return result + "'s Son";
                  case CardTitle.Miss:
                      return result + "'s Daughter";
                  default:
                      return result;
              }
          }
      }

      #endregion
  }

Creating the deck class

First it is necessary to inherit the generic abstract DeckBase  class, specifying the card created above.

In the example below an initialize method adds the required cards.

public class HappyFamiliesDeck : DeckBase<HappyFamiliesCard>
{
     public HappyFamiliesDeck()
     {
         Initialize();
     }

     private void Initialize()
     {
         foreach (var family in CardFamily.ToList())
             foreach (var value in Enum.GetNames(typeof (CardTitle)))
                 Add(new HappyFamiliesCard(family, (CardTitle) Enum.Parse(typeof (CardTitle), value), DeckId));
     }
}

Last edited Nov 6, 2010 at 9:55 AM by dve, version 4

Comments

No comments yet.