|
@@ -0,0 +1,93 @@
|
|
1
|
+
|
|
2
|
+/**
|
|
3
|
+* Guessing game
|
|
4
|
+* prompts user to guess a mystery number
|
|
5
|
+* after every guess, "Too large" "Too small" "Correct"
|
|
6
|
+* when terminating, display # of guesses
|
|
7
|
+* if the same number is used, it counts as 1 guess
|
|
8
|
+*/
|
|
9
|
+
|
|
10
|
+import java.util.*;
|
|
11
|
+
|
|
12
|
+public class TooLargeTooSmall {
|
|
13
|
+
|
|
14
|
+ public static int randomNumGenerator(){
|
|
15
|
+ // this section starts the game by letting the user select
|
|
16
|
+ // a game mode
|
|
17
|
+ Random num = new Random();
|
|
18
|
+ Scanner keyboard = new Scanner(System.in);
|
|
19
|
+ System.out.println("Welcome to the guessing game... \n"
|
|
20
|
+ + "Select a mode \n"
|
|
21
|
+ + "1: Easy (Number between 1-10)\n"
|
|
22
|
+ + "2: Normal (Number between 1-20)\n"
|
|
23
|
+ + "3: Hard (Number between 1-50)\n"
|
|
24
|
+ + "Select by inputing the number");
|
|
25
|
+
|
|
26
|
+ int gameMode = keyboard.nextInt();
|
|
27
|
+
|
|
28
|
+ int min = 0;
|
|
29
|
+ int max = 0;
|
|
30
|
+
|
|
31
|
+ switch (gameMode) {
|
|
32
|
+
|
|
33
|
+ case 2: min = 1;
|
|
34
|
+ max = 20;
|
|
35
|
+ break;
|
|
36
|
+ case 3: min = 1;
|
|
37
|
+ max = 50;
|
|
38
|
+ break;
|
|
39
|
+ default: min = 1;
|
|
40
|
+ max = 10;
|
|
41
|
+ break;
|
|
42
|
+
|
|
43
|
+ }
|
|
44
|
+
|
|
45
|
+ int guessMe = num.nextInt((max - min) + 1) + min;
|
|
46
|
+
|
|
47
|
+ return guessMe;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ public void guessing() {
|
|
51
|
+
|
|
52
|
+ int numberToGuess = randomNumGenerator();
|
|
53
|
+
|
|
54
|
+ boolean guessFound = false;
|
|
55
|
+
|
|
56
|
+ int guessCounter = 0,
|
|
57
|
+ previousGuess = 0;
|
|
58
|
+
|
|
59
|
+ Scanner keyboard = new Scanner(System.in);
|
|
60
|
+ // While loop until guess is correct.
|
|
61
|
+ // interacts with other methods
|
|
62
|
+ // guess checker - will check guess against other guesses
|
|
63
|
+ // boolean, if currentGuess = previousGuess, repeatGuess = true/false
|
|
64
|
+
|
|
65
|
+ while (guessFound == false) {
|
|
66
|
+ // guess found - returns win message and how many guesses
|
|
67
|
+ System.out.println("What's your guess?!");
|
|
68
|
+ int currentGuess = keyboard.nextInt();
|
|
69
|
+
|
|
70
|
+ // if statement to check if guess is correct
|
|
71
|
+
|
|
72
|
+ if (currentGuess == numberToGuess) {
|
|
73
|
+ System.out.println("You guessed the right number!"
|
|
74
|
+ + "\n"
|
|
75
|
+ + "It took you this many guesses: "
|
|
76
|
+ + guessCounter);
|
|
77
|
+ guessFound = true;
|
|
78
|
+
|
|
79
|
+ } else {
|
|
80
|
+ // check guess
|
|
81
|
+ if (currentGuess != previousGuess) {
|
|
82
|
+ guessCounter++;
|
|
83
|
+ previousGuess = currentGuess;
|
|
84
|
+ }
|
|
85
|
+ System.out.println("That wasn't the right answer. Guess again!");
|
|
86
|
+ }
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ System.out.println("");
|
|
90
|
+
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+}
|