|
@@ -10,36 +10,65 @@ import java.util.Scanner;
|
10
|
10
|
import java.util.Random;
|
11
|
11
|
|
12
|
12
|
public class GuessingGame{
|
13
|
|
-
|
|
13
|
+
|
14
|
14
|
public static void main(String[] args) {
|
15
|
|
-
|
|
15
|
+
|
16
|
16
|
System.out.println("GUESSING GAME");
|
17
|
|
-
|
|
17
|
+
|
18
|
18
|
Random rand = new Random();
|
19
|
|
- int answer = rand.nextInt(10) + 1;
|
20
|
|
- int numGuesses = 1;
|
|
19
|
+ int answer = rand.nextInt(9) + 1;
|
|
20
|
+ int numGuesses = 0;
|
21
|
21
|
int userGuess = 0;
|
22
|
|
-
|
23
|
|
- // Print mystery number for check
|
24
|
|
- // System.out.println(answer);
|
25
|
|
-
|
|
22
|
+ int lastGuess = 0;
|
|
23
|
+
|
|
24
|
+ boolean[] guessArray = new boolean[10];
|
|
25
|
+
|
|
26
|
+ for (int i = 0; i < 10; i++){
|
|
27
|
+ guessArray[i] = false;
|
|
28
|
+ //System.out.println(guessArray[i]);
|
|
29
|
+ };
|
|
30
|
+
|
26
|
31
|
while (userGuess != answer){
|
27
|
|
-
|
28
|
32
|
Scanner scan = new Scanner(System.in);
|
29
|
|
- System.out.println("Guess a number from 1 to 10:");
|
|
33
|
+ System.out.println("Guess a number from 0 to 9:");
|
30
|
34
|
userGuess = scan.nextInt();
|
31
|
|
-
|
32
|
|
- if (userGuess == answer) {
|
33
|
|
- System.out.println("correct guess in " + numGuesses + " guesses!!");
|
34
|
|
- } else {
|
35
|
|
- if (userGuess < answer){
|
36
|
|
- System.out.println("too small");
|
37
|
|
- } else {
|
38
|
|
- System.out.println("too large");
|
|
35
|
+
|
|
36
|
+ if (userGuess >= 0 && userGuess < 10){
|
|
37
|
+
|
|
38
|
+ for (int i = 0; i < 10; i++){
|
|
39
|
+ if(guessArray[userGuess] == false){
|
|
40
|
+
|
|
41
|
+ guessArray[userGuess] = true;
|
|
42
|
+ numGuesses++;
|
|
43
|
+
|
|
44
|
+ }
|
|
45
|
+ //Prints all boolean values of array
|
|
46
|
+ //System.out.println(guessArray[i]);
|
39
|
47
|
};
|
40
|
|
- numGuesses++;
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+ if (userGuess == answer) {
|
|
51
|
+ System.out.println("You guessed the mystery number " + answer + " correct ");
|
|
52
|
+ System.out.println("in " + numGuesses + " guesses!!");
|
|
53
|
+ } else {
|
|
54
|
+ if (userGuess < answer){
|
|
55
|
+ System.out.println("too small");
|
|
56
|
+ } else {
|
|
57
|
+ System.out.println("too large");
|
|
58
|
+ };
|
|
59
|
+
|
|
60
|
+ //Testing
|
|
61
|
+ //System.out.println("userGuess is: " + userGuess);
|
|
62
|
+ //System.out.println("lastGuess is: " + lastGuess);
|
|
63
|
+ //System.out.println("numGuesses is: " + numGuesses);
|
|
64
|
+
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+ lastGuess = userGuess;
|
|
68
|
+ } else {
|
|
69
|
+ System.out.println("You guessed out of the range! Guess again!!");
|
41
|
70
|
}
|
42
|
|
-
|
43
|
71
|
}
|
44
|
72
|
}
|
45
|
73
|
}
|
|
74
|
+
|