-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.java
278 lines (211 loc) · 4.65 KB
/
Player.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.util.Random;
/** Creates player class and methods.
*/
public class Player {
// Constant Var:
private static int MAX_HP = 60;
private static int MAX_ATK = 25;
private static int MAX_DEF = 10;
private static int level = 1;
// Instance Varibles:
private int hp = 60;
private int attack = 25;
private int defense = 10;
private int luck = 3;
private int goldCount = 0;
private String name = "";
/** Normal Constructor.
* @param nameIn - name
*/
public Player(String nameIn) {
hp = MAX_HP;
attack = MAX_ATK;
defense = MAX_DEF;
name = nameIn;
}
/** Override normal constructor for testing.
* Adds 'level' param.
* @param hpIn - hp
* @param attackIn - attack
* @param defenseIn - defense
* @param levelIn - level
*/
public Player(int hpIn, int attackIn, int defenseIn, int levelIn) {
hp = hpIn;
attack = attackIn;
defense = defenseIn;
level = levelIn;
}
/** Sets Hp.
* @param hpIn - hp
*/
public void setHp(int hpIn) {
hp = hpIn;
if (hp > MAX_HP) {
hp = MAX_HP;
}
}
/** Return Hp.
* @return hp
*/
public int getHp() {
return hp;
}
/** Return max_hp.
* @return MAX_HP
*/
public int getMaxHp() {
return MAX_HP;
}
/** Sets attack.
* @param atkIn - attack
*/
public void setAttack(int atkIn) {
attack = atkIn;
if (attack > MAX_ATK) {
attack = MAX_ATK;
}
}
/** Return attack.
* @return attack
*/
public int getAttack() {
return attack;
}
/** Sets Defense.
* @param defIn - defense
*/
public void setDefense(int defIn) {
defense = defIn;
if (defense > MAX_DEF) {
defense = MAX_DEF;
}
}
/** Return Defense.
* @return defense
*/
public int getDefense() {
return defense;
}
/** Return name.
* @return name
*/
public String getName() {
return name;
}
/** Takes hit (Hp - hitIn).
* @param hitIn - hit
*/
public void takeHit(int hitIn) {
hp -= hitIn;
if (hp < 0) {
hp = 0;
}
}
/** Levels up.
* @param in - int 0, 1, 2
*/
public void levelUp(int in) {
level += 1;
//Player chose atk:
if (in == 1) {
MAX_ATK += 10;
}
else {
MAX_ATK += 5;
}
//Player chose def:
if (in == 2) {
MAX_DEF += 10;
}
else {
MAX_DEF += 5;
}
//Player chose Hp:
if (in == 3) {
MAX_HP += 20;
}
else {
MAX_HP += 10;
}
hp = MAX_HP;
attack = MAX_ATK;
defense = MAX_DEF;
}
/** Return current level.
* @return level
*/
public int getLevel() {
return level;
}
/** Regains Health.
* @param hpIn - hp
*/
public void restoreHp(int hpIn) {
hp += hpIn;
if (hp > MAX_HP) {
hp = MAX_HP;
}
}
/** Checks to see if player hits (RNG).
* @param chance = 'dice' roll (0,9)
* @return true/false
*/
public boolean checkHit(int chance) {
Random rn = new Random();
int hit = rn.nextInt(10);
if (hit >= chance) {
return true;
}
else {
return false;
}
}
/** Sets luck.
* @param luckIn - luck
*/
public void setLuck(int luckIn) {
luck = luckIn;
}
/** Return luck.
* @return luck
*/
public int getLuck() {
return luck;
}
/** Adds gold.
* @param goldIn - gold
*/
public void addGold(int goldIn) {
goldCount += goldIn;
}
/** Returns goldCount.
*
* @return goldCount
*/
public int getGold() {
return goldCount;
}
/** Return died.
* @return output
*/
public String died() {
String output = "";
output += "\nYou died!";
output += "\n--------------------------------------------------------";
return output;
}
/** Return player stats.
* @return output
*/
public String toString() {
String output = "";
output += name + " stats:"
+ " Health: " + hp
+ " Attack: " + attack
+ " Defense: " + defense
+ " Level: " + level
+ " Gold: " + goldCount;
return output;
}
}