Advertisement
Guest User

HiLowBS

a guest
May 22nd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. /*
  2. Joo_Dennis_HiLow.java
  3. Description: With the three modes for the game, the user must guess the computer's number following the rules for the selected mode
  4. Date Created: April 18, 2019
  5. Date Modified: April 24, 2019
  6. */
  7. // The "HiLowMethods" class.
  8. import java.awt.*;
  9. import hsa.Console;
  10. import java.util.Random;
  11.  
  12. public class HiLowMethods
  13. {
  14. static Console c; // The output console
  15.  
  16. public static void main (String[] args)
  17. {
  18. c = new Console ();
  19.  
  20. int min = 0;
  21. int max = 0;
  22. int choice = 1;
  23. Random rand = new Random ();
  24. int compnum = 0;
  25.  
  26. int answer = compnum;
  27. double totalwins = 0; //total wins
  28. double totalgames = 0; //total games played
  29.  
  30. c.println ("Hello! Please choose what level difficulty you want to play : ");
  31. c.println ("Type 1 for Easy Option : Number range is from 1 - 100, five guesses.");
  32. c.println ("Type 2 for Difficult Option: Number range is from 1 - 1000, ten guesses.");
  33. c.println ("Type 3 for Customized: Number range and amount of guesses in from your choice.");
  34. c.print ("Input here : ");
  35. choice = c.readInt ();
  36.  
  37. Guess (choice, min, max);
  38. ending (totalwins, totalgames);
  39.  
  40.  
  41. int endchoice = 1;
  42. c.print ("Do you want to play again? Y - 1, N - 2 : ");
  43. endchoice = c.readInt ();
  44. totalgames++;
  45. }
  46.  
  47.  
  48. // Place your program here. 'c' is the output console
  49. // main method
  50.  
  51.  
  52. public static int min (int choice)
  53. {
  54. int min = 0;
  55. if (choice == 1 || choice == 2)
  56. {
  57. min = 1;
  58. }
  59. if (choice == 3)
  60. {
  61. c.print ("Okay user! Please input a range. Start with your minimum number : ");
  62. min = c.readInt ();
  63. }
  64. return min;
  65. }
  66.  
  67.  
  68. public static int max (int choice)
  69. {
  70. int max = 0;
  71. if (choice == 1)
  72. {
  73. max = 100;
  74. }
  75. else if (choice == 2)
  76. {
  77. max = 1000;
  78. }
  79. else if (choice == 3)
  80. {
  81. c.print ("Please input maximum number : ");
  82. max = c.readInt ();
  83. }
  84. return max;
  85. }
  86.  
  87.  
  88. public static void Guess (int choice, int min, int max)
  89. {
  90.  
  91. int endchoice = 1; //option where the player can either play again or quit the game
  92.  
  93. Random rand = new Random ();
  94.  
  95. int num; //number guess
  96. int guess, compnum = 0; //guess is the amount of guesses they have left, minimum number, maximum number, computer's number
  97. double totalwins = 0; //total wins
  98. double totalgames = 0; //total games played
  99. if (choice == 1)
  100. {
  101. guess = 0;
  102. compnum = rand.nextInt (max - min + 1) + min;
  103.  
  104. c.print ("I'm thinking of a number between 1 to 100, please guess a number! : ");
  105. while (guess < 5)
  106. {
  107. num = c.readInt ();
  108. if (num < 0 || num > 100)
  109. {
  110. c.print ("Invalid number, please input a number between 1 - 100 : ");
  111. guess++;
  112. }
  113. else if (num > compnum)
  114. {
  115. c.print ("Number is too high! ");
  116. guess++;
  117. }
  118. else if (num < compnum)
  119. {
  120. c.print ("Number is too low! ");
  121. guess++;
  122. }
  123. else
  124. {
  125. c.print ("Correct number! The number was : " + compnum + " ");
  126. totalwins++;
  127. guess = 6;
  128. }
  129. if (guess == 5)
  130. {
  131. c.print ("Sorry! The number was : " + compnum + " ");
  132. }
  133. else
  134. {
  135. c.print (" Try again. : ");
  136. }
  137.  
  138. }
  139. c.print ("Do you want to play again? Y - 1, N - 2 : ");
  140. endchoice = c.readInt ();
  141. totalgames++;
  142.  
  143. }
  144.  
  145.  
  146. else if (choice == 2)
  147. {
  148. guess = 0;
  149. compnum = rand.nextInt (max - min + 1) + min;
  150. c.print ("I'm thinking of a number between 1 to 1000, please guess a number! : ");
  151. while (guess < 10)
  152. {
  153. num = c.readInt ();
  154. if (num < 0 || num > 1000)
  155. {
  156. c.print ("Invalid number, please input a number between 1 - 100 : ");
  157. guess++;
  158. }
  159. else if (num > compnum)
  160. {
  161. c.print ("Number is too high! ");
  162. guess++;
  163. }
  164. else if (num < compnum)
  165. {
  166. c.print ("Number is too low! ");
  167. guess++;
  168. }
  169. else
  170. {
  171. c.print ("Correct number! The number was : " + compnum);
  172. totalwins++;
  173. guess = 11;
  174. }
  175. if (guess == 10)
  176. {
  177. c.print ("Sorry! The number was : " + compnum + " ");
  178. }
  179. else
  180. {
  181. c.print (" Try again : ");
  182. }
  183. }
  184. c.print ("Do you want to play again? Y - 1, N - 2 : ");
  185. endchoice = c.readInt ();
  186. totalgames++;
  187. }
  188.  
  189.  
  190. else if (choice == 3)
  191. {
  192. guess = 0;
  193. c.print ("Input amount of guesses : ");
  194. int newguess = c.readInt ();
  195. compnum = rand.nextInt (max - min + 1) + min;
  196. c.print ("I'm thinking of a number between " + min + " and " + max + ". Input a guess : ");
  197. while (guess < newguess)
  198. {
  199. num = c.readInt ();
  200. if (num < min || num > max)
  201. {
  202. c.print ("Please input a valid number within range : ");
  203. guess++;
  204. }
  205. else if (num > compnum)
  206. {
  207. c.print ("Number is too high! ");
  208. guess++;
  209. }
  210. else if (num < compnum)
  211. {
  212. c.print ("Number is too low! ");
  213. guess++;
  214. }
  215. else
  216. {
  217. c.print ("Correct number! The number was : " + compnum + " ");
  218. totalwins++;
  219. guess = newguess + 1;
  220. }
  221. if (guess == newguess)
  222. {
  223. c.print ("Sorry! The number was : " + compnum + " ");
  224. }
  225. else
  226. {
  227. c.print (" Try again. : ");
  228. }
  229.  
  230. }
  231.  
  232.  
  233. }
  234. // return guess;
  235. }
  236.  
  237.  
  238. public static void ending (double totalwins, double totalgames)
  239. {
  240. c.println ("Total wins : " + totalwins);
  241. c.println ("Total games : " + totalgames);
  242. c.print ("Win percentage : ");
  243. c.print ((totalwins / totalgames) * 100, 0, 2);
  244. c.print ("%");
  245. }
  246. }
  247.  
  248.  
  249. // HiLowMethods class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement