TraditionalCard.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //-----------------------------------------------------------------------------
  2. // TraditionalCard.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Microsoft.Xna.Framework;
  11. namespace CardsFramework
  12. {
  13. /// <summary>
  14. /// Enum defining the various types of cards for a traditional-western card-set
  15. /// </summary>
  16. [Flags]
  17. public enum CardSuit
  18. {
  19. Heart = 0x01,
  20. Diamond = 0x02,
  21. Club = 0x04,
  22. Spade = 0x08,
  23. // Sets:
  24. AllSuits = Heart | Diamond | Club | Spade
  25. }
  26. /// <summary>
  27. /// Enum defining the various types of card values for a traditional-western card-set
  28. /// </summary>
  29. [Flags]
  30. public enum CardValue
  31. {
  32. Ace = 0x01,
  33. Two = 0x02,
  34. Three = 0x04,
  35. Four = 0x08,
  36. Five = 0x10,
  37. Six = 0x20,
  38. Seven = 0x40,
  39. Eight = 0x80,
  40. Nine = 0x100,
  41. Ten = 0x200,
  42. Jack = 0x400,
  43. Queen = 0x800,
  44. King = 0x1000,
  45. FirstJoker = 0x2000,
  46. SecondJoker = 0x4000,
  47. // Sets:
  48. AllNumbers = 0x3FF,
  49. NonJokers = 0x1FFF,
  50. Jokers = FirstJoker | SecondJoker,
  51. AllFigures = Jack | Queen | King,
  52. }
  53. /// <summary>
  54. /// Traditional-western card
  55. /// </summary>
  56. /// <remarks>
  57. /// Each card has a defined <see cref="CardSuit">Type</see> and <see cref="CardValue">Value</see>
  58. /// as well as the <see cref="CardPacket"/> in which it is being held.
  59. /// A card may not be held in more than one <see cref="CardPacket"/>. This is achived by enforcing any card transfer
  60. /// operation between <see cref="CarkPacket"/>s and <see cref="Hand"/>s to be performed only from within the card's
  61. /// <see cref="MoveToHand"/> method only. This method accesses <c>internal</c> <see cref="Hand.Add"/> method and
  62. /// <see cref="CardPacket.Remove"/> method accordingly to complete the card transfer operation.
  63. /// </remarks>
  64. public class TraditionalCard
  65. {
  66. /// <summary>
  67. /// Factory method for deserialization, creates a card with no holding collection.
  68. /// </summary>
  69. public static TraditionalCard Create(CardSuit type, CardValue value)
  70. {
  71. return new TraditionalCard(type, value, null);
  72. }
  73. public CardSuit Type { get; set; }
  74. public CardValue Value { get; set; }
  75. public CardPacket HoldingCardCollection;
  76. /// <summary>
  77. /// Initializes a new instance of the <see cref="TraditionalCard"/> class.
  78. /// </summary>
  79. /// <param name="type">The card suit. Supports only a single value.</param>
  80. /// <param name="value">The card's value. Only single values are
  81. /// supported.</param>
  82. /// <param name="holdingCardCollection">The holding card collection.</param>
  83. internal TraditionalCard(CardSuit type, CardValue value,
  84. CardPacket holdingCardCollection)
  85. {
  86. // Check for single type
  87. switch (type)
  88. {
  89. case CardSuit.Club:
  90. case CardSuit.Diamond:
  91. case CardSuit.Heart:
  92. case CardSuit.Spade:
  93. break;
  94. default:
  95. {
  96. throw new ArgumentException(
  97. "type must be single value", "type");
  98. }
  99. }
  100. // Check for single value
  101. switch (value)
  102. {
  103. case CardValue.Ace:
  104. case CardValue.Two:
  105. case CardValue.Three:
  106. case CardValue.Four:
  107. case CardValue.Five:
  108. case CardValue.Six:
  109. case CardValue.Seven:
  110. case CardValue.Eight:
  111. case CardValue.Nine:
  112. case CardValue.Ten:
  113. case CardValue.Jack:
  114. case CardValue.Queen:
  115. case CardValue.King:
  116. case CardValue.FirstJoker:
  117. case CardValue.SecondJoker:
  118. break;
  119. default:
  120. {
  121. throw new ArgumentException(
  122. "value must be single value", "value");
  123. }
  124. }
  125. Type = type;
  126. Value = value;
  127. HoldingCardCollection = holdingCardCollection;
  128. }
  129. /// <summary>
  130. /// Moves the card from its current <see cref="CardPacket"/> to the specified <paramref name="hand"/>.
  131. /// This method of operation prevents any one card instance from being held by more than one
  132. /// <see cref="CardPacket"/> at the same time.
  133. /// </summary>
  134. /// <param name="hand">The receiving hand.</param>
  135. public void MoveToHand(Hand hand)
  136. {
  137. HoldingCardCollection.Remove(this);
  138. HoldingCardCollection = hand;
  139. hand.Add(this);
  140. }
  141. }
  142. }