Advertisement
Guest User

TwentyOne.cs

a guest
Oct 15th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7.  
  8. namespace SharedGamesClasses {
  9.  
  10. /// <summary>
  11. /// TwentyOne.cs plays the game of Twenty One -
  12. /// a card game whereby two parties compete to get a hand of playing cards
  13. /// whose summed facevalues are closest to 21, without exceeding it.
  14. ///
  15. /// </summary>
  16. public static class TwentyOne {
  17. public const int NUM_OF_PLAYERS = 2;
  18. public const int USER = 0;
  19. public const int DEALER = 1;
  20.  
  21. private const string USERS_NAME = "Player";
  22. private const string DEALERS_NAME = "Dealer";
  23.  
  24. private const int NUM_OF_STARTING_CARDS = 2;
  25.  
  26. // More constants and/or class variables may be needed.
  27.  
  28. private static CardPile cardPile = new CardPile(true);
  29. private static Hand[] hands = new Hand[NUM_OF_PLAYERS];
  30. private static int[] pointsTotal = new int[NUM_OF_PLAYERS] { 0, 0 };
  31. private static int[] numOfGamesWon = new int[NUM_OF_PLAYERS] { 0, 0 };
  32.  
  33. private static int numOfUserAcesWithValueOne;
  34.  
  35. /// <summary>
  36. /// Set up to play the game,
  37. /// by shuffling the pile of cards and dealing the two hands.
  38. /// </summary>
  39. public static void SetUpGame() {
  40. cardPile.Shuffle();
  41. hands[USER] = new Hand(cardPile.DealCards(NUM_OF_STARTING_CARDS));
  42. hands[DEALER] = new Hand(cardPile.DealCards(NUM_OF_STARTING_CARDS));
  43. numOfUserAcesWithValueOne = 0;
  44. } //end SetUpGame
  45.  
  46. /// <summary>
  47. /// Plays the game of Twenty One until the user decides to stop.
  48. /// </summary>
  49. public static void PlayConsole() {
  50. SetUpGame();
  51. DisplayHand(DEALER);
  52. DisplayHand(USER);
  53. // Play 21 until the user decides to stop.
  54. } //end PlayConsole
  55.  
  56. /// <summary>
  57. /// Output the hand of cards of either the User or the Dealer
  58. /// using DisplayHand of Hand class
  59. /// Pre: hands initialised
  60. /// Post: Displays the hand of cards held by the specified player.
  61. /// </summary>
  62. public static void DisplayHand(int player) {
  63. string player_name;
  64.  
  65. if (player == USER) {
  66. player_name = USERS_NAME;
  67. } else {
  68. player_name = DEALERS_NAME;
  69. }
  70.  
  71. Console.Write("{0} has ", player_name);
  72. hands[player].DisplayHand(false, false);
  73. // Code needed.
  74. } // end DisplayHand
  75.  
  76.  
  77. /* The following methods are intended to be called by your GUI code.
  78. * I.e. you shouldn't need them when writing your Console game,
  79. * but do not delete them. */
  80.  
  81. /// <summary>
  82. /// Helps the GUI to display what is in a player's hand.
  83. /// </summary>
  84. /// <returns>the Hand of the specified player</returns>
  85. public static Hand GetHand(int player) {
  86. return hands[player];
  87. }
  88.  
  89. /// <summary>
  90. /// Helps the GUI to display the number of Aces that
  91. /// the user has chosen to have value 1, rather than 11.
  92. /// </summary>
  93. /// <returns>the numOfUserAcesWithValueOne</returns>
  94. public static int GetNumOfUserAcesWithValueOne() {
  95. return numOfUserAcesWithValueOne;
  96. }
  97.  
  98. /// <summary>
  99. /// Helps the GUI to display the points total of each player.
  100. /// </summary>
  101. /// <returns>the points total of the specified player</returns>
  102. public static int GetPointsTotal(int player) {
  103. return pointsTotal[player];
  104. }
  105.  
  106. /// <summary>
  107. /// Helps the GUI to display the number of games won by each player.
  108. /// </summary>
  109. /// <returns>the number of games won by the specified player</returns>
  110. public static int GetNumOfGamesWon(int player) {
  111. return numOfGamesWon[player];
  112. }
  113.  
  114. } //end class TwentyOne
  115. } //end namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement