Advertisement
Guest User

card.cs

a guest
Oct 15th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SharedGamesClasses {
  7. public enum Suit { Clubs, Diamonds, Hearts, Spades }
  8.  
  9. public enum FaceValue {
  10. Two, Three, Four, Five, Six, Seven, Eight, Nine,
  11. Ten, Jack, Queen, King, Ace
  12. }
  13.  
  14. /// <summary>
  15. /// Default Constructor allows application to have "blank" cards
  16. ///
  17. /// </summary>
  18. public class Card : IEquatable<Card>, IComparable<Card> {
  19. // IEquatable allows Card objects to be compared to see if two cards have the same value.
  20. // IComparable allows Card objects to be sorted easily.
  21. // While the precise details of IEquatable and IComparable are unimportant for this assignment,
  22. // they are needed so that the Hand class's Sort and Contains methods each work correctly.
  23.  
  24. private Suit suit;
  25. private FaceValue faceValue;
  26.  
  27. // Default constructor: Card is "blank"
  28. public Card() {
  29. }
  30.  
  31. // The constructor normally used when you know its Suit and FaceValue values.
  32. public Card(Suit suit, FaceValue faceValue) {
  33. this.suit = suit;
  34. this.faceValue = faceValue;
  35. }
  36.  
  37. // This method lets you create a Card object from a string (which is normally entered by the user).
  38. // E.g. 10C for Ten Of Clubs; AH for Ace of Hearts.
  39. // Lowercase is also ok, e.g. 10c for Ten Of Clubs; ah for Ace of Hearts.
  40. // Returns a Card if the CardCode is valid; otherwise null.
  41. public static Card TryToCreateCard(string cardCode) {
  42.  
  43. bool validCardCode = true;
  44. cardCode = cardCode.ToUpper();
  45. int faceLength = cardCode.Length - 1;
  46. if (faceLength < 1)
  47. return null; // Otherwise Substring throws an exception.
  48. string faceAsString = cardCode.Substring(0, faceLength);
  49. string suitAsString = cardCode.Substring(faceLength, 1);
  50.  
  51. Suit suit = 0;
  52. FaceValue faceValue = 0;
  53.  
  54. switch (faceAsString) {
  55. case "2": faceValue = FaceValue.Two; break;
  56. case "3": faceValue = FaceValue.Three; break;
  57. case "4": faceValue = FaceValue.Four; break;
  58. case "5": faceValue = FaceValue.Five; break;
  59. case "6": faceValue = FaceValue.Six; break;
  60. case "7": faceValue = FaceValue.Seven; break;
  61. case "8": faceValue = FaceValue.Eight; break;
  62. case "9": faceValue = FaceValue.Nine; break;
  63. case "10": faceValue = FaceValue.Ten; break;
  64. case "J": faceValue = FaceValue.Jack; break;
  65. case "Q": faceValue = FaceValue.Queen; break;
  66. case "K": faceValue = FaceValue.King; break;
  67. case "A": faceValue = FaceValue.Ace; break;
  68. default: validCardCode = false; break;
  69. }
  70.  
  71. switch (suitAsString) {
  72. case "C": suit = Suit.Clubs; break;
  73. case "D": suit = Suit.Diamonds; break;
  74. case "H": suit = Suit.Hearts; break;
  75. case "S": suit = Suit.Spades; break;
  76. default: validCardCode = false; break;
  77. }
  78.  
  79. if (validCardCode) {
  80. return new Card(suit, faceValue);
  81. } else {
  82. return null;
  83. }
  84. }
  85.  
  86. // An accessor.
  87. public Suit GetSuit() {
  88. return suit;
  89. }
  90.  
  91. // Another accessor.
  92. public FaceValue GetFaceValue() {
  93. return faceValue;
  94. }
  95.  
  96. // Returns a string that describes the card's value.
  97. //
  98. // When shortFormat is False, the card's FaceValue is returned as its full name,
  99. // e.g. Two, Three, ..., King, Ace.
  100. // When shortFormat is True, the card's FaceValue is returned as one or two characters,
  101. // e.g. 2, 3, ... 10, J, Q, K, A.
  102. //
  103. // When displaySuit is False, the card's Suit is ignored.
  104. // When displaySuit is True, the card's Suit is added on the end,
  105. // e.g. " of Clubs" when shortFormat is False,
  106. // or "C" when shortFormat is True, as in "2C".
  107. public string ToString(bool shortFormat, bool displaySuit)
  108. {
  109. string returnString;
  110.  
  111. // Describe the FaceValue.
  112. FaceValue faceValue = GetFaceValue();
  113. string faceValueAsString = faceValue.ToString();
  114. if (shortFormat) {
  115. if (faceValue <= FaceValue.Ten) {
  116. faceValueAsString = (faceValue - FaceValue.Two + 2).ToString();
  117. } else {
  118. faceValueAsString = faceValueAsString.Substring(0, 1);
  119. }
  120. }
  121.  
  122. returnString = faceValueAsString;
  123.  
  124. // Describe the Suit.
  125. if (displaySuit) {
  126. string suit = GetSuit().ToString();
  127. if (shortFormat) {
  128. suit = suit.Substring(0, 1);
  129. returnString += suit;
  130. } else {
  131. returnString += " of " + suit;
  132. }
  133. }
  134.  
  135. return returnString;
  136. }
  137.  
  138. // Needed so that the Hand class's Contains method works correctly.
  139. // You should NOT need to call this Equals method directly in your own code.
  140. // Returns True if one card is equal to another, e.g. myCard.Equals(anotherCard).
  141. // Otherwise, returns False.
  142. public bool Equals(Card anotherCard) {
  143. return (this.suit == anotherCard.suit && this.faceValue == anotherCard.faceValue);
  144. }
  145.  
  146. // Needed so that the Hand class's Sort method works correctly.
  147. // You should NOT need to call this CompareTo method directly in your own code.
  148. // Returns:
  149. // -1 if the current card precedes anotherCard in the sort order;
  150. // 0 if the current card is in the same position as anotherCard in the sort order;
  151. // +1 if the current card follows anotherCard in the sort order.
  152. public int CompareTo(Card anotherCard) {
  153.  
  154. if (this.suit < anotherCard.suit) {
  155. return -1;
  156. } else if (this.suit > anotherCard.suit) {
  157. return 1;
  158. } else {
  159. if (this.faceValue < anotherCard.faceValue) {
  160. return -1;
  161. } else if (this.faceValue > anotherCard.faceValue) {
  162. return 1;
  163. } else {
  164. return 0;
  165. }
  166. }
  167. }
  168.  
  169. } //end class Card
  170. }//end namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement