/Assignment 2/SharedGamesClasses/TwentyOne [backup].cs

https://bitbucket.org/critnal/csharp · C# · 407 lines · 203 code · 175 blank · 29 comment · 73 complexity · a9e6cd9f926b447266ba0d40468c60ff MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. namespace SharedGamesClasses {
  7. public static class TwentyOne {
  8. const int PLAYER = 0;
  9. const int DEALER = 1;
  10. const int ACE_ELEVEN = 11;
  11. const int ACE_ONE = 1;
  12. const int TEN = 10;
  13. const int TWO = 2;
  14. const int TWENTY_ONE = 21;
  15. const string PLAYERS_NAME = "Player";
  16. const string DEALERS_NAME = "Dealer";
  17. private static Pack pack = new Pack();
  18. private static ArrayList[] hands = new ArrayList[2];
  19. private static bool playerTurn;
  20. private static bool dealerTurn;
  21. private static int playerHandTotal = 0;
  22. private static int dealerHandTotal = 0;
  23. private static int playerScore = 0;
  24. private static int dealerScore = 0;
  25. private static string gameWinner;
  26. private static string outPutMessage;
  27. /// <summary>
  28. /// Plays the game of Twenty One until the user decides to stop.
  29. /// </summary>
  30. public static void Play() {
  31. do { // until user chooses to stop
  32. SetUpGame();
  33. PlayerTurn();
  34. if (dealerTurn) {
  35. DealerTurn();
  36. }
  37. OutPutWinner();
  38. DisplayScores();
  39. } while (StillPlaying());
  40. } // end Play
  41. /// <summary>
  42. /// Set up to play the game,
  43. /// by shuffling the pack of cards and
  44. /// dealing the two hands
  45. /// </summary>
  46. private static void SetUpGame() {
  47. for (int i = 0; i < hands.Length; i++) {
  48. hands[i] = new ArrayList();
  49. }
  50. DealHands();
  51. DisplayHand(hands[PLAYER], PLAYERS_NAME);
  52. DisplayHand(hands[DEALER], DEALERS_NAME);
  53. } // end SetUpGame
  54. private static void DealHands() {
  55. pack.Shuffle();
  56. hands[PLAYER] = pack.DealTwoCards();
  57. hands[DEALER] = pack.DealTwoCards();
  58. } // end DealHands
  59. private static void PlayerTurn() {
  60. do { // until player stays
  61. playerHandTotal = CurrentPoints(PLAYER);
  62. if (playerHandTotal == TWENTY_ONE) {
  63. gameWinner = PLAYERS_NAME;
  64. outPutMessage = "player 21";
  65. playerTurn = false;
  66. dealerTurn = false;
  67. } else if (Bust(playerHandTotal)) {
  68. gameWinner = DEALERS_NAME;
  69. outPutMessage = "player bust";
  70. playerTurn = false;
  71. dealerTurn = false;
  72. } else if (PlayerWantToHit()) {
  73. Hit(PLAYER);
  74. DisplayHand(hands[PLAYER], PLAYERS_NAME);
  75. playerTurn = true;
  76. } else {
  77. playerTurn = false;
  78. dealerTurn = true;
  79. Console.WriteLine("\n\tPlayer stays with {0} points.", playerHandTotal);
  80. }
  81. } while (playerTurn);
  82. } // end PlayerTurn
  83. private static void DealerTurn() {
  84. do { // until dealer wins or loses
  85. dealerHandTotal = CurrentPoints(DEALER);
  86. if (Bust(dealerHandTotal)) {
  87. outPutMessage = "dealer bust";
  88. gameWinner = PLAYERS_NAME;
  89. dealerTurn = false;
  90. } else if (dealerHandTotal > playerHandTotal) {
  91. gameWinner = DEALERS_NAME;
  92. dealerTurn = false;
  93. } else if (dealerHandTotal <= playerHandTotal) {
  94. Hit(DEALER);
  95. Console.WriteLine("\n\tDealer Hit");
  96. DisplayHand(hands[DEALER], DEALERS_NAME);
  97. dealerTurn = true;
  98. }
  99. } while (dealerTurn);
  100. } // end DealerTurn
  101. private static int CurrentPoints(int who) {
  102. ArrayList hand = hands[who];
  103. Card card;
  104. int handTotal = 0;
  105. for (int i = 0; i < hand.Count; i++ ) {
  106. card = (Card) hand[i];
  107. if (card.GetFaceValue() == FaceValue.Ace) {
  108. if (handTotal < ACE_ELEVEN) {
  109. handTotal += ACE_ELEVEN;
  110. } else {
  111. handTotal += ACE_ONE;
  112. }
  113. } else if (((int)card.GetFaceValue() + TWO) > TEN) {
  114. handTotal += TEN;
  115. } else {
  116. handTotal += (int)card.GetFaceValue() + TWO;
  117. }
  118. }
  119. return handTotal;
  120. } //end Play
  121. private static bool Bust(int handTotal) {
  122. return handTotal > TWENTY_ONE;
  123. } // end Bust
  124. private static bool PlayerWantToHit() {
  125. string userInput;
  126. do { // while input is not valid
  127. // prompt the player.
  128. Console.WriteLine("\nDo you want to hit (H) or stay (S):");
  129. // get and check the player's input.
  130. userInput = Console.ReadLine();
  131. if (userInput != "H" && userInput != "h" && userInput != "S" && userInput != "s") {
  132. Console.WriteLine("\nPlease enter either Y or N");
  133. }
  134. } while (userInput != "H" && userInput != "h" && userInput != "S" & userInput != "s");
  135. if (userInput == "H" || userInput == "h") {
  136. return true;
  137. } else {
  138. return false;
  139. }
  140. } // end of WantToHit
  141. private static bool DealerWantToHit() {
  142. return dealerHandTotal <= playerHandTotal;
  143. } // end DealerWantToHit
  144. private static void Hit(int who) {
  145. hands[who].Add(pack.DealOneCard());
  146. } // end Hit
  147. /// <summary>
  148. /// Output the hand of cards of either the Player or the Dealer
  149. /// </summary>
  150. /// <param name="hand"> a player's hand which consists of two or more cards</param>
  151. /// <param name="who">the player whose hand it is</param>
  152. public static void DisplayHand(ArrayList hand, string who) {
  153. // the items of the hand arraylist parameter need to be assigned
  154. // to individual card objects in order to perform the GetFaceValue()
  155. // accessor method on them
  156. Card tempCard1 = (Card) hand[0];
  157. Card tempCard2 = (Card) hand[1];
  158. Console.Write("\n\t{0} has {1}, {2}", who, tempCard1.GetFaceValue(), tempCard2.GetFaceValue());
  159. if (hand.Count > 2) {
  160. for (int i = 2; i < hand.Count; i++) {
  161. Card tempCard = (Card)hand[i];
  162. Console.Write(", {0}", tempCard.GetFaceValue());
  163. }
  164. }
  165. Console.WriteLine();
  166. } // end DisplayHand
  167. private static void OutPutWinner() {
  168. switch (outPutMessage) {
  169. case "player 21":
  170. Console.WriteLine("\n\tPlayer wins with 21!");
  171. break;
  172. case "player bust":
  173. Console.WriteLine("\n\tPlayer has gone bust with {0} points", playerHandTotal);
  174. break;
  175. case "dealer bust" :
  176. Console.WriteLine("\n\tDealer has gone bust with {0} points", dealerHandTotal);
  177. break;
  178. default :
  179. Console.WriteLine("\n\tPlayer has {0} points, Dealer has {1} points.", playerHandTotal, dealerHandTotal);
  180. break;
  181. }
  182. Console.WriteLine("\n\t{0} wins", gameWinner);
  183. outPutMessage = "null";
  184. } // end OutPutWInner
  185. private static void DisplayScores() {
  186. UpdateScores();
  187. Console.WriteLine("\nPlayer has won {0} games and Dealer has won {1} games.",
  188. playerScore, dealerScore);
  189. } // end DisplayScores
  190. private static void UpdateScores() {
  191. if (gameWinner == "Player") {
  192. playerScore++;
  193. } else {
  194. dealerScore++;
  195. }
  196. } // end UpdateScores
  197. private static bool StillPlaying() {
  198. // check if player wants to play again
  199. if (WantToPlayAgain_ConsoleOnly()) {
  200. DealHands();
  201. return true;
  202. } else {
  203. return false;
  204. }
  205. } // end PlayAgain
  206. /// Asks the user if they wish to play again.
  207. /// Pre: none
  208. /// Post: whether player wishes to play again.
  209. /// </summary>
  210. /// <returns>
  211. /// true if player wishes to play again,
  212. /// false otherwise
  213. /// </returns>
  214. private static bool WantToPlayAgain_ConsoleOnly() {
  215. string userInput;
  216. do { // while input is not valid
  217. // prompt the player.
  218. Console.WriteLine("\nPlay again? (Y or N):");
  219. // get and check the player's input.
  220. userInput = Console.ReadLine();
  221. if (userInput != "Y" && userInput != "y" && userInput != "N" && userInput != "n") {
  222. Console.WriteLine("**Please enter either Y or N");
  223. }
  224. } while (userInput != "Y" && userInput != "y" && userInput != "N" & userInput != "n");
  225. if (userInput == "Y" || userInput == "y") {
  226. return true;
  227. } else {
  228. return false;
  229. }
  230. } // end WantToPlayAgain_ConsoleOnly
  231. } // end class TwentyOne
  232. } // end namespace