Advertisement
Guest User

Untitled

a guest
Jul 5th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CardGame
  8. {
  9. enum Suit { Hearts, Diamonds, Clubs, Spades };
  10. enum FaceValue { Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace };
  11. class Card
  12. {
  13. private Suit mySuit;
  14. private FaceValue myFaceValue;
  15.  
  16. //default constructor
  17. public Card()
  18. {
  19. mySuit = Suit.Hearts;
  20. myFaceValue = FaceValue.Two;
  21. }
  22.  
  23. //regular constructor
  24. public Card(Suit s, FaceValue v)
  25. {
  26. mySuit = s;
  27. myFaceValue = v;
  28. }
  29.  
  30. //Prints the card, example: Nine of Clubs
  31. public override string ToString()
  32. {
  33. string result = string.Format("{0} of {1}", this.myFaceValue, this.mySuit);
  34. return result;
  35. }
  36.  
  37. public Suit GetSuit()
  38. {
  39. return mySuit;
  40. }
  41.  
  42. public FaceValue GetFaceValue()
  43. {
  44. return myFaceValue;
  45. }
  46.  
  47.  
  48. //compare just on face values
  49. //compare card one to card 2. Is going to return 0, -1, or 1
  50. // # implements IComparaible.CompareTo
  51. public int CompareTo(object obj)
  52. {
  53. if (obj == null) return 1;
  54.  
  55. Card secondCard = obj as Card;
  56.  
  57. if (secondCard != null)
  58. return this.myFaceValue.CompareTo(secondCard.myFaceValue);
  59. else
  60. throw new ArgumentException("Object is not a Card");
  61. }
  62.  
  63. //Equal is similar to CompareTo
  64. public override bool Equals(object obj)
  65. {
  66. bool same;
  67. int result;
  68. result = this.CompareTo(obj);
  69. if (result == 0) same = true;
  70. else same = false;
  71. return same;
  72. }
  73.  
  74. // GetHashCode is part to the IComparable interface
  75. // Not used here, but provided to get a clean built.
  76. public override int GetHashCode()
  77. {
  78. return ((int)this.myFaceValue);
  79. }
  80. }/*class Card*/
  81.  
  82. class Deck
  83. {
  84. private const int CARDSINDECK = 52;
  85. private List<Card> theDeck = new List<Card>();
  86. private Random randomCardSelector = new Random();
  87. private int cardIndex;
  88.  
  89. public Deck()
  90. {
  91. cardIndex = 0;
  92.  
  93. //for (Suit suit = Suit.Hearts; suit <= Suit.Spades; suit++)
  94. //{
  95. // for (FaceValue faceValue = FaceValue.Two; faceValue <= FaceValue.Ace; faceValue++)
  96. // {
  97. // this.theDeck.Add(new Card(suit, faceValue));
  98. // }
  99. //}
  100.  
  101. foreach (Suit s in Enum.GetValues(typeof(Suit)))
  102. {
  103. foreach (FaceValue f in Enum.GetValues(typeof(FaceValue)))
  104. {
  105. Card c = new Card(s,f);
  106. theDeck.Add(c);
  107. }
  108. }
  109. }
  110.  
  111. //Function returns the concatenation of all the cards in the deck
  112. //usefull for debugging: to see if it was properly shuffled
  113. public String Print()
  114. {
  115. String s = "";
  116. for (int i = 0; i < CARDSINDECK; i++)
  117. {
  118. s+= theDeck[i].ToString();
  119. }
  120. return s;
  121. }
  122.  
  123. //Is the deck empty?
  124. public bool EmptyDeck()
  125. {
  126. if (cardIndex < CARDSINDECK)
  127. {
  128. return false;
  129. }
  130. else return true;
  131. }
  132.  
  133. // make sure you don't take the same card
  134. // Pre-condition: The deck is not empty.
  135. // Check if the deck is not empty before calling this function
  136. public Card DealACard()
  137. {
  138. cardIndex++;
  139. return theDeck[cardIndex - 1];
  140. }
  141.  
  142.  
  143. // shuffle the Deck
  144. public void Shuffle()
  145. {
  146. Card temp;
  147. int mynum;
  148. for (int i = 0; i < CARDSINDECK; i++)
  149. {
  150. mynum = randomCardSelector.Next(CARDSINDECK);
  151. temp = theDeck[i];
  152. theDeck[i] = theDeck[mynum];
  153. theDeck[mynum] = temp;
  154.  
  155. }
  156. }
  157.  
  158. }/*Class Deck*/
  159.  
  160. class Program
  161. {
  162. static void Main(string[] args)
  163. {
  164. //Card card1;
  165. Deck aDeck = new Deck();
  166. aDeck.Shuffle();
  167.  
  168.  
  169. //card1 = aDeck.DealACard();
  170.  
  171. //Console.WriteLine(card1.GetFaceValue() + " of " + card1.GetSuit());
  172. //Console.WriteLine("Shuffled");
  173. //Console.WriteLine(aDeck.Print());
  174. //Console.ReadKey();
  175.  
  176.  
  177. int player1 = 0;
  178. int player2 = 0;
  179. int pot = 2;
  180. int round = 0;
  181. while (!aDeck.EmptyDeck())
  182. {
  183. round += 1;
  184. Console.WriteLine("Round{0}", round);
  185.  
  186. Card card1 = aDeck.DealACard();
  187. Card card2 = aDeck.DealACard();
  188.  
  189. Console.WriteLine("Player One card = {0}", card1.ToString());
  190. Console.WriteLine("Player Two card = {0}", card2.ToString());
  191. if (card1.Equals(card2))
  192. {
  193. /*war*/
  194. Console.WriteLine("It is a draw");
  195. if (aDeck.EmptyDeck())
  196. {
  197. /*this is last card so war can not continue*/
  198. player1 += pot / 2;
  199. player2 += pot / 2;
  200. }
  201. else
  202. {
  203. pot += 2;
  204. }
  205. }
  206. else if (card1.CompareTo(card2) == 1){
  207. Console.WriteLine("Player One wins this round. They get {0} points!",pot);
  208. player1 +=pot;
  209. pot = 2;
  210. }
  211. else{
  212. Console.WriteLine("Player Two wins this round. They get {0} points!",pot);
  213. player2 +=pot;
  214. pot = 2;
  215. }
  216. Console.WriteLine("Current Score");
  217. Console.WriteLine("Player1 = {0}", player1);
  218. Console.WriteLine("Player2 = {0}", player2);
  219. Console.Write("Press return to continue. . .");
  220. Console.ReadLine();
  221. }
  222. Console.WriteLine("Thank you for playing!");
  223. Console.ReadKey();
  224. }
  225. }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement