-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
96 lines (95 loc) · 2.36 KB
/
GuessingGame.java
File metadata and controls
96 lines (95 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.*;
public class GuessingGame {
public static void main(String[] args){
mainloop();
}
public static void mainloop(){
boolean done = true;//if i started with false i'd have to while(done = false)
int a = 0;//let's init some vars
int b = 0;
int tries = 0;
int bestTries = 9999999;//9999999>1 million :)
int games = 0;
int guesses = 0;
while(done){
games = games+1;
tries = 0;
a = -1;//makes sure b != a
b = RandInt();
while(a != b){
tries = tries+1;//increments tries for game only
a = GetInput(/*b*/);
/*uncomment the b here as well as on line 41 and 43
* to enable cheat mode (can see random number)
* in the method GetInput*/
int bigger = isBigger(a,b);
String bigness = PrintOutput(bigger, tries);
System.out.println(bigness);
guesses = guesses + 1;//increments total guess #
}
bestTries = BestTry(bestTries, tries);
done = AskDone();
}
System.out.println("Games played = "+ games);
System.out.println("Moves/game = "+ ((guesses*1.0)/games));
System.out.println("Total guesses = "+ guesses);
System.out.println("Best game = "+bestTries);
}
public static int RandInt(){//grabs a random int
Random rand = new Random();
int a = rand.nextInt(100)+1;
return a;
}
public static int GetInput(/*int b*/){//method to grab an guess
Scanner console = new Scanner(System.in);
System.out.print("Your guess? "/*+"(awnser is "+b+")"*/);
int input = console.nextInt();
return input;
}
public static boolean AskDone(){//method to ask another game
Scanner console = new Scanner(System.in);
System.out.println("play again? (y/n)");
String input = console.next();
if(input.equalsIgnoreCase("y")){
return true;
}
else{
return false;
}
}
public static int isBigger(int a, int b){//returns an integer value for > < =
if(a==b){
return 0;
}
else if(a<b){
return 1;
}
else{
return 2;
}
}
public static String PrintOutput(int a, int b){//Turns isBigger into words, special exception for first guess luck
if(a==0){
if(b == 1){
return "You got it on the first try!";
}
else{
return "You got it in "+b+" tries.";
}
}
else if(a==1){
return "It's higher.";
}
else{
return "It's lower.";
}
}
public static int BestTry(int a, int b){//comparator to return best game
if(a>b){
return b;
}
else{
return a;
}
}
}//EOF