Guest User

Untitled

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