forked from Devinterview-io/git-interview-questions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer
32 lines (27 loc) · 826 Bytes
/
Player
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
public class Player {
private int health;
private int strength;
private int attack;
public Player(int health, int strength, int attack) {
this.health = health;
this.strength = strength;
this.attack = attack;
}
public boolean isAlive() {
return health > 0;
}
public void attack(Player opponent, Die die) {
int attackRoll = die.roll();
int attackDamage = attack * attackRoll;
opponent.defend(attackDamage, die);
}
public void defend(int incomingDamage, Die die) {
int defenseRoll = die.roll();
int defense = strength * defenseRoll;
int netDamage = Math.max(0, incomingDamage - defense);
health = Math.max(0, health - netDamage);
}
public int getHealth() {
return health;
}
}