generated from OOP2020/lab1-start
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGuessingGame.java
117 lines (99 loc) · 2.77 KB
/
GuessingGame.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
import java.util.Random;
/**
* Guess a secret number between 1 and an upper bound.
*
* @author Teeranut Sawanyawat 6210545491
*/
public class GuessingGame {
// count the number of round that player played
private int counter;
// upper bound for secret number
private int upperBound;
// the secret number
private int secret;
// hint for most recent guess
private String message;
/**
* Initialize a new game with a default upperbound.
*/
public GuessingGame(int upperbound) {
this.upperBound = upperbound;
secret = getRandomNumber(this.upperBound);
String hint = "I'm thinking of a number between 1 and " + upperBound;
setMessage(hint);
}
public GuessingGame() {
this(100);
}
/**
* Get a random number between 1 and limit.
*
* @param limit is the upper limit for the random number.
* @return a random integer
*/
private int getRandomNumber(int limit) {
// use a seed so the random numbers are not repeatable
long seed = System.nanoTime();
Random rand = new Random(seed);
return 1 + rand.nextInt(limit);
}
/**
* Evaluate a guess.
*
* @param number is a guess of the secret number.
* @return true if the guess is correct, false otherwise.
*/
public boolean guess(int number) {
message = makeHint(number, secret);
++this.counter;
return (number == secret);
}
/**
* Get a message (hint) based on most recent guess. If nothing has been guessed
* yet then the hint describes the game.
*
* @return a message about game or most recent guess
*/
public String getMessage() {
return this.message;
}
/**
* Get the game upper bound for the secret number.
*/
public int getUpperBound() {
return this.upperBound;
}
public void setMessage(String msg) {
this.message = msg;
}
/**
* Create a hint based on the last guess.
*
* @param number is the most recent guess
* @param secret is the secret number
*/
protected String makeHint(int number, int secret) {
if (number == secret) {
return "Correct!";
} else if (number - secret < -upperBound / 4)
return "WAY too small, dude.";
else if (number < secret)
return "Too small.";
else if (number - secret > upperBound / 4)
return "WAY too large, man.";
else
return "Too large.";
}
/**
* Provide a started message to the player
*/
public String toString() {
return "Guess a secret number.";
}
/**
* Get the game counter number.
*/
public int getCount() {
return counter;
}
}