-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCharacter.java
More file actions
87 lines (67 loc) · 2.41 KB
/
Character.java
File metadata and controls
87 lines (67 loc) · 2.41 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
/*=============================================
class Character -- superclass for combatants in Ye Olde RPG
=============================================*/
public abstract class Character {
// ~~~~~~~~~~~ INSTANCE VARIABLES ~~~~~~~~~~~
protected int _hitPts;
protected int _strength;
protected int _defense;
protected double _attack;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*=============================================
default constructor
pre: instance vars are declared
post: initializes instance vars.
=============================================*/
public Character() {
_hitPts = 125;
_strength = 100;
_defense = 40;
_attack = .4;
}
// ~~~~~~~~~~~~~~ ACCESSORS ~~~~~~~~~~~~~~~~~
public int getDefense() { return _defense; }
public int getHP() {return _hitPts;}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*=============================================
boolean isAlive() -- tell whether I am alive
post: returns boolean indicated alive or dead
=============================================*/
public boolean isAlive() {
return _hitPts > 0;
}
/*=============================================
int attack(Character) -- simulates attack on instance of class Monster
pre: Input not null
post: Calculates damage to be inflicted, flooring at 0.
Calls opponent's lowerHP() method to inflict damage.
Returns damage dealt.
=============================================*/
public abstract int attackSpecial(Character opponent);
public int attackNormal( Character opponent ) {
int damage = (int)( (_strength * _attack) - opponent.getDefense() );
//System.out.println( "\t\t**DIAG** damage: " + damage );
if ( damage < 0 )
damage = 0;
opponent.lowerHP( damage );
return damage;
}//end attack
/*=============================================
void lowerHP(int) -- lowers life by input value
pre: Input >= 0
post: Life instance var is lowered by input ammount.
=============================================*/
public void lowerHP( int damageInflicted ) {
_hitPts = _hitPts - damageInflicted;
}
public abstract String about();
public void specialize() {
_attack = .75;
_defense = 20;
}
//revert to normal mode
public void normalize() {
_attack = .4;
_defense = 40;
}
}//end class Character