Guest User

Untitled

a guest
Jun 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Games {
  7.  
  8.  
  9. /// <summary>
  10. /// Plays a game of two-up, using Simple rules or Complex rules
  11. /// </summary>
  12. class TwoUp {
  13.  
  14. //create two coin objects
  15. Coin coin1 = new Coin();
  16. Coin coin2 = new Coin();
  17.  
  18. //initialise
  19. bool coinOneHeads;
  20. bool coinTwoHeads;
  21. int playerWins = 0;
  22. int houseWins = 0;
  23.  
  24. string answer = "";
  25. string yes = "Y";
  26. string no = "N";
  27.  
  28. /// <summary>
  29. /// asks the user if (s)he whould like to play again,
  30. /// reads the response and sets it as variable 'answer'
  31. /// answer is then evaluated and action is taken further
  32. /// in the code in the 'Play' method.
  33. /// </summary>
  34. private void PlayAgain() {
  35. // displays wins/loses
  36. // asks and evaluates if the player would like to play again
  37. Console.WriteLine("You have {0} wins and I have {1} wins.", playerWins, houseWins);
  38. Console.WriteLine("Would you like to play again? (Y/N)");
  39. answer = Console.ReadLine();
  40.  
  41. while ((answer.ToUpper() != yes) && (answer.ToUpper() != no)) {
  42. Console.WriteLine("Please enter the correct response, (Y/N)");
  43. answer = Console.ReadLine();
  44. }//end while
  45.  
  46. if (answer == no) {
  47.  
  48. Console.WriteLine("Thank you for playing");
  49.  
  50. }//end if
  51. }//end PlayAgain
  52.  
  53.  
  54. /// <summary>
  55. /// Flips the two coins and
  56. /// determines the facevalue of both
  57. /// and assigns the variables
  58. /// coinXIsHeads to be true or false.
  59. /// </summary>
  60. private void FlipCoinsAndCheck() {
  61.  
  62. coin1.Flip();
  63.  
  64. if (coin1.IsHeads() == true) {
  65. coinOneHeads = true;
  66. } else if (coin1.IsHeads() == false) {
  67. coinOneHeads = false;
  68. }//end else if
  69.  
  70. coin2.Flip();
  71.  
  72. if (coin2.IsHeads() == true) {
  73. coinTwoHeads = true;
  74. } else if (coin2.IsHeads() == false) {
  75. coinTwoHeads = false;
  76. }//end else if
  77.  
  78. }//end FlipCoinsAndCheck
  79.  
  80. /// <summary>
  81. /// Plays TwoUp by the simple rules
  82. /// as long as the menuOption is equal
  83. /// to one.
  84. /// </summary>
  85. private void SimpleRules() {
  86. const int WIN = 1;
  87.  
  88. // flip both coins and checks facevalue
  89. FlipCoinsAndCheck();
  90.  
  91. // if 'IsHeads' is true for both
  92. // player wins
  93. if ((coinOneHeads == true) && (coinTwoHeads == true)) {
  94. Console.WriteLine("Your threw Heads - You've won!");
  95. playerWins = playerWins + WIN;
  96.  
  97. // else if 'IsHeads' is false for both
  98. // house/computer wins
  99. } else if ((coinOneHeads == false) && (coinTwoHeads == false)) {
  100. Console.WriteLine("Your threw Tails - I've won!");
  101. houseWins = houseWins + WIN;
  102. //end else if
  103.  
  104. // otherwise, while'IsHeads' is true for one
  105. // and not the other: flip again
  106. } while (((coinOneHeads == true) && (coinTwoHeads == false)) ||
  107. ((coinOneHeads == false) && (coinTwoHeads == true))) {
  108. Console.WriteLine("Your threw Odds - Flip again!");
  109. FlipCoinsAndCheck();
  110. }//end while
  111.  
  112. }//end SimpleRules
  113.  
  114.  
  115. /// <summary>
  116. /// Plays TwoUp by the complex rules
  117. /// as long as the menuOption is equal
  118. /// to two.
  119. /// </summary>
  120. private void ComplexRules() {
  121.  
  122. //initialise counters
  123. int headsCounter = 0;
  124. int tailsCounter = 0;
  125. int counterIsThree = 3;
  126.  
  127. // continue flipping coins and checking
  128. // for the faceValue to be heads/tails
  129. // until double heads/tails is
  130. // flipped thrice.
  131. do {
  132. FlipCoinsAndCheck();
  133.  
  134. if ((coinOneHeads == true) && (coinTwoHeads == true)) {
  135. Console.WriteLine("Your threw Heads!");
  136. headsCounter++;
  137.  
  138. } else if ((coinOneHeads == false) && (coinTwoHeads == false)) {
  139. Console.WriteLine("Your threw Tails!");
  140. tailsCounter++;
  141. //end else if
  142.  
  143. } else if (((coinOneHeads == true) && (coinTwoHeads == false)) ||
  144. ((coinOneHeads == false) && (coinTwoHeads == true))) {
  145. Console.WriteLine("Your threw Odds!");
  146.  
  147. }//end else if
  148. }while((headsCounter != counterIsThree) && (tailsCounter != counterIsThree));
  149. //end while
  150.  
  151. // if double heads is flipped three times, player wins!
  152. // else if double tails is flipped three times, player loses!
  153. if (headsCounter == counterIsThree) {
  154. playerWins++;
  155. } else if (tailsCounter == counterIsThree) {
  156. houseWins++;
  157.  
  158. }//end else if
  159.  
  160. }//end ComplexRules
  161.  
  162.  
  163. /// <summary>
  164. /// Plays the game by calling the
  165. /// game methods and 'play again' method
  166. /// and by the chosen rules as long as
  167. /// the variable 'answer' remains yes.
  168. /// </summary>
  169. /// <param name="menuOption"></param>
  170. public void Play(int menuOption) {
  171.  
  172. //initialise variables
  173. const int SIMPLERULES = 1;
  174. const int COMPLEXRULES = 2;
  175.  
  176. // menuoption will be determined as one
  177. // and therefore a game of two up
  178. // with simple rules, until the player
  179. // choses not to
  180. if (menuOption == SIMPLERULES) {
  181. do {
  182. SimpleRules();
  183. PlayAgain();
  184. } while (answer == yes);
  185.  
  186. // otherwise menuoption is two and therefore
  187. // play by the complex rules until the player
  188. // decides not to play anymore.
  189. } else if (menuOption == COMPLEXRULES) {
  190. do {
  191. ComplexRules();
  192. PlayAgain();
  193. } while (answer == yes);
  194.  
  195. } else {
  196. Console.WriteLine("If this message appears, then there has been an error with the menuOption");
  197. }
  198.  
  199.  
  200.  
  201.  
  202. }//end play
  203. } //end class TwoUp
  204. }//end Games
Add Comment
Please, Sign In to add comment