Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3. public class GuessingGame {
  4. //First text based game. Guess the random number that will be generated, and gather points!
  5. public static void main(String[] args) {
  6.  
  7. try (Scanner store = new Scanner(System.in)){ //This < is used to create a new scanner variable . Used the try method because Eclipse was giving a 'resource leak'
  8. Random rnd = new Random();
  9.  
  10.  
  11. //Only 4 rounds so 4 random number ints.
  12. int guess; //Will store the number that you will guess.
  13. int totalpoints = 0;
  14. char readytoplay;
  15. int randomnumber1 = rnd.nextInt(1)+1; //First level of random numbers
  16. int randomnumber2 = rnd.nextInt(2)+1;
  17. int randomnumber3 = rnd.nextInt(4)+1;
  18. int randomnumber4 = rnd.nextInt(9)+1;
  19.  
  20.  
  21. System.out.println("Welcome to Guessing game!");
  22. System.out.println("In this game, you will guess a number to see how lucky you are.");
  23. System.out.println("As you progress, it gets harder and harder. ");
  24. System.out.println("You will gain points as you go. ");
  25. System.out.println("Are you ready to play? Y/N");
  26. readytoplay = store.next().charAt(0); //This will store a CHAR
  27. System.out.println("You chose: " + readytoplay);
  28.  
  29. while(true){
  30. if(readytoplay == 'y' ||readytoplay == 'Y'){ //Possible response types for Yes.
  31. System.out.println("Okay! Great! Lets get started.");
  32. break;
  33. }
  34. else if(readytoplay == 'n' ||readytoplay == 'N'){ //Possible response types for No.
  35. System.out.println("If you don't want to play, please exit.");
  36. readytoplay = store.next().charAt(0);
  37. break;
  38. }
  39. else{
  40. System.out.println("You must answer EITHER (Y)es or (n)o"); //Letting them know they didn't answer either Y, y, N, n so it was a invalid response.
  41. readytoplay = store.next().charAt(0); // This will store their answer for them to re submit a valid response.
  42.  
  43. continue;
  44.  
  45. }
  46. }
  47. System.out.println("Okay. So let's explain how this works. ");
  48. try { // This will delay the Strings being typed. 1500 = 1.5 secs.
  49. Thread.sleep(900); //
  50. } // I added these to make the text easy to read for the user
  51. catch (InterruptedException ex) // To not spam them with everything to read all at once.
  52. { // Not sure of an easier way to do this so maybe a TODO: getting a easier method..
  53. //
  54. } //
  55. System.out.println("You will be given a 'Range' of numbers to guess for the answer.");
  56. try {
  57. Thread.sleep(900);
  58. }
  59. catch (InterruptedException ex)
  60. {
  61.  
  62. }
  63. System.out.println("FOR EXAMPLE:");
  64. try {
  65. Thread.sleep(900);
  66. }
  67. catch (InterruptedException ex)
  68. {
  69.  
  70. }
  71. System.out.println("1-3");
  72. try {
  73. Thread.sleep(900);
  74. }
  75. catch (InterruptedException ex)
  76. {
  77.  
  78. }
  79. System.out.println("You can guess either 1, 2, or 3.");
  80. try {
  81. Thread.sleep(900);
  82. }
  83. catch (InterruptedException ex)
  84. {
  85.  
  86. }
  87. System.out.println("The machine will generate a random number between either of those, if yours is correct, you advance!");
  88. try {
  89. Thread.sleep(900);
  90. }
  91. catch (InterruptedException ex)
  92. {
  93.  
  94. }
  95. System.out.println("Let's try this out.");
  96. try {
  97. Thread.sleep(900);
  98. }
  99. catch (InterruptedException ex)
  100. {
  101.  
  102. }
  103. System.out.println("The first set is 1-2. Pick 1, or 2.");
  104. guess = store.nextInt();
  105.  
  106.  
  107. //ROUND 1
  108. if (guess == randomnumber1) {
  109. System.out.println("You were correct.");
  110. System.out.println("You guessed "+guess);
  111. System.out.println("The random number was "+randomnumber1);
  112. totalpoints = +1; //This will add points to their total points.
  113. System.out.println("You gained 1 point, Your total points are now: " +totalpoints); //Obviously letting them know how many points they now have.
  114.  
  115. }
  116. else {
  117. System.out.println("You didn't guess correctly.");
  118.  
  119. System.out.println("Your number:" +guess);
  120. System.out.println("The random number:" +randomnumber1);
  121. System.exit(0); //Exiting the game, they did not have any points at the time so not displaying any points .
  122. }
  123. {
  124.  
  125. //ROUND 2
  126. System.out.println("Good job , by the way! You got lucky! Literally!");
  127. try { //
  128. Thread.sleep(1000); //
  129. } //
  130. catch (InterruptedException ex) //Pausing for a second to not spam.. Letting them know round 2 is starting.
  131. { //
  132. //
  133. } //
  134. System.out.println("Next round. Your numbers are 1-3. 1,2, or 3 ");
  135. System.out.println("Pick your number:");
  136. guess = store.nextInt();
  137.  
  138. if (guess == randomnumber2) {
  139. System.out.println("You were correct. ");
  140. System.out.println("You guessed "+guess);
  141. System.out.println("The random number was " +randomnumber2);
  142. totalpoints = +2;
  143. System.out.println("You gain 2 points for that one! you now have: " +totalpoints+ "points!");
  144. }
  145. else {
  146. System.out.println("You were not correct, sorry..");
  147. System.out.println("You guessed: " +guess);
  148. System.out.println("The random number was " +randomnumber2);
  149. System.out.println("Game over. You ended with: " +totalpoints+ " point");
  150. System.exit(0);
  151. }
  152. try { //
  153. Thread.sleep(1000); //
  154. } //
  155. catch (InterruptedException ex) //Pausing for a second to not spam.. Letting them know round 3 is starting.
  156. { //
  157.  
  158. } //
  159.  
  160. //ROUND 3
  161. System.out.println("Round 3. You will need to guess 1-5. Good luck on this one...");
  162. System.out.println("Guess now. 1,2,3,4, or 5.");
  163. guess = store.nextInt();
  164.  
  165. if (guess == randomnumber3) {
  166. System.out.println("You were correct. ");
  167. System.out.println("You guessed "+guess);
  168. System.out.println("The random number was " +randomnumber3);
  169. totalpoints = +3;
  170. System.out.println("You gain 3 points for that one! you now have: " +totalpoints+ " points!");
  171. }
  172. else {
  173. System.out.println("You were not correct, sorry..");
  174. System.out.println("You guessed: " +guess);
  175. System.out.println("The random number was " +randomnumber3);
  176. System.out.println("Game over. You ended with: " +totalpoints+ " point");
  177. System.exit(0);
  178. }
  179. try { //
  180. Thread.sleep(1000); //
  181. }
  182. catch (InterruptedException ex) //Pausing for a second to not spam.. Letting them know round 4 is starting.
  183. { //
  184. //
  185. } //
  186.  
  187. //ROUND 4
  188. System.out.println("You are really lucky. Maybe you should play the lottery");
  189. System.out.println("Although, I believe this will be a little more difficult.");
  190. System.out.println("You will be given a random number 1-10. 1,2,3,4,5,6,7,8,9, or 10.");
  191. System.out.println("You have a 10% chance. Goodluck.);");
  192. System.out.println("Guess now:");
  193. guess = store.nextInt();
  194.  
  195. if (guess == randomnumber4) {
  196. System.out.println("You were correct. ");
  197. System.out.println("You guessed " +guess);
  198. System.out.println("The random number was " +randomnumber4);
  199. totalpoints = + 4;
  200. System.out.println("You gain 4 points. You now have: " +totalpoints+ "points!");
  201.  
  202. }
  203. else {
  204. System.out.println("You were wrong. You guessed " +guess);
  205. System.out.println("The random number was: " +randomnumber4);
  206. System.out.println("You ended the game with " +totalpoints+ "points.");
  207. System.exit(0);
  208. }
  209. try { //
  210. Thread.sleep(500); //
  211. } //
  212. catch (InterruptedException ex) //Pausing for a second to not spam..
  213. { //
  214. //
  215. } //
  216. System.out.println("CONGRATULATIONS! You beat all the odds, somehow..");
  217. System.out.println("You ended the game with: " +totalpoints);
  218. System.exit(0);
  219.  
  220. }
  221. }
  222. }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement