-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessNumber.java
146 lines (113 loc) · 3.26 KB
/
GuessNumber.java
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//Daniel Gelfand
//APCS1 pd2
//HW48 -- Keep Guessing
//2017-12-06
/*==================================================
class GuessNumber -- fun fun fun!
eg, sample interaction with end user:
Guess a # fr 1-100: 50
Too high
Guess a # fr 1-49: 25
Too low
Guess a # fr 26-49: 38
Correct! It took 3 guesses
==================================================*/
import cs1.Keyboard;
public class GuessNumber
{
//instance vars
private int _lo, _hi, _guessCtr, _target;
/*==================================================
constructor -- initializes a guess-a-number game
pre:
post: _lo is lower bound, _hi is upper bound,
_guessCtr is 1, _target is random int on range [_lo,_hi]
==================================================*/
public GuessNumber( int a, int b )
{
_lo = a;
_hi = b;
_guessCtr = 1;
_target = (int) (Math.random()*(b-a)) + a;
}
/*==================================================
void playRec() -- Prompts a user to guess until guess is correct.
Uses recursion.
pre:
post:
==================================================*/
public void playRec()
{
System.out.println("Guess a number from " + _lo + "-" + _hi );
int guess = Keyboard.readInt();
//make sure user gives valid inpit(within range)
if(guess > _hi || guess < _lo){
System.out.println("Input a number within the given range!");
playRec();
}
//call playRec() until user guesses right
if(guess > _target){
System.out.println("Guess too high");
_hi = guess - 1;
_guessCtr += 1;
playRec();
}
else if(_target > guess){
System.out.println("Guess too low");
_lo = 1 + guess;
_guessCtr += 1;
playRec();
}
else{
System.out.println("You guessed right after " + (_guessCtr) + " guesses");
}
}
/*==================================================
void playIter() -- Prompts a user to guess until guess is correct.
Uses iteration.
pre:
post:
==================================================*/
public void playIter()
{
int guess = 0;
//keep playing until the guess matches that random int
while(guess != _target){
System.out.println("Guess a number from " + _lo + "-" + _hi );
guess = Keyboard.readInt();
//make sure the user gives valid input(within range)
if(guess > _hi || guess < _lo){
System.out.println("Guess within the range!");
}
else if(guess > _target){
System.out.println("Guess too high");
_hi = guess - 1;
_guessCtr += 1;
}
else{
System.out.println("Guess too low");
_lo = 1 + guess;
_guessCtr += 1;
}
}
//started with _guessCtr = 1 so we subtract 1
System.out.println("You guessed right after " + (_guessCtr-1) + " guesses");
}
//wrapper for playRec/playIter to simplify calling
public void play()
{
//use one or the other below:
// playRec();
playIter();
}
//main method to run it all
public static void main( String[] args )
{
//instantiate a new game
GuessNumber g = new GuessNumber(1,100);
//start the game
g.play();
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
}//end main
}//end class