-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDungeonCharacter.java
More file actions
188 lines (149 loc) · 5.39 KB
/
DungeonCharacter.java
File metadata and controls
188 lines (149 loc) · 5.39 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
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
import java.util.Scanner;
public abstract class DungeonCharacter
{
private String name;
private int hitPoints;
private int attackSpeed;
private double chanceToHit;
private int damageMin, damageMax;
//-----------------------------------------------------------------
//explicit constructor to initialize instance variables -- it is called
// by derived classes
protected DungeonCharacter(String name, Details details)
{
if (this instanceof Hero) {
setHeroName();
}
else {
setName(name);
}
setHitPoints(details.getHealthPoints());
setAttackSpeed(details.getAttackSpeed());
setChanceToHit(details.getChanceToHit());
int mindamage = details.getDamageRange()[0];
int maxdamage = details.getDamageRange()[1];
setDamageRange(mindamage,maxdamage);
}//end constructor
public void setHitPoints(final int hitPoints) {
this.hitPoints = hitPoints;
}
public void setName(String name) {
this.name = name;
}
public void setHeroName() {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.print("Enter your hero's name: ");
String name = sc.nextLine();
setName(name);
}
public void setAttackSpeed(final int attackSpeed) {
this.attackSpeed = attackSpeed;
}
public void setChanceToHit(final double chanceToHit) {
this.chanceToHit = chanceToHit;
}
public void setDamageRange(final int damageMin, final int damageMax){
this.damageMin = damageMin;
this.damageMax = damageMax;
}
//-----------------------------------------------------------------
public String getName()
{
return name;
}//end getName
//-----------------------------------------------------------------
public int getHitPoints()
{
return hitPoints;
}//end getHitPoints
//-----------------------------------------------------------------
public int getAttackSpeed()
{
return attackSpeed;
}//end getAttackSpeed
/*-------------------------------------------------------
addHitPoints is used to increment the hitpoints a dungeon character has
Receives: number of hit points to add
Returns: nothing
This method calls: nothing
This method is called by: heal method of monsters and Sorceress
---------------------------------------------------------*/
public void addHitPoints(int hitPoints)
{
if (hitPoints <=0)
System.out.println("Hitpoint amount must be positive.");
else
{
this.hitPoints += hitPoints;
System.out.println(this.getName() + " now has " + this.getHitPoints() + " hitpoints\n");
}
}//end addHitPoints method
/*-------------------------------------------------------
subtractHitPoints is used to decrement the hitpoints a dungeon character has.
It also reports the damage and remaining hit points (these things could be
done in separate methods to make code more modular ;-)
Receives: number of hit points to subtract
Returns: nothing
This method calls: nothing
This method is called by: overridden versions in Hero and Monster
---------------------------------------------------------*/
public void subtractHitPoints(int hitPoints, DungeonCharacter opponent)
{
if (hitPoints <0)
System.out.println("Hitpoint amount must be positive.");
else if (hitPoints >0)
{
this.hitPoints -= hitPoints;
if (this.hitPoints < 0)
this.hitPoints = 0;
System.out.println(opponent.getName() + " hit " + getName() +
" for <" + hitPoints + "> points damage.");
System.out.println( getName() + " now has " +
getHitPoints() + " hit points remaining.");
System.out.println();
}//end else if
if (this.hitPoints == 0)
System.out.println(name + " has been killed :-(");
}//end method
/*-------------------------------------------------------
isAlive is used to see if a character is still alive by checking hit points
Receives: nothing
Returns: true is hero is alive, false otherwise
This method calls: nothing
This method is called by: unknown (intended for external use)
---------------------------------------------------------*/
// public boolean isAlive()
// {
// return (hitPoints > 0);
// }//end isAlive method
/*-------------------------------------------------------
attack allows character to attempt attack on opponent. First, chance to hit
is considered. If a hit can occur, then the damage is calculated based on
character's damage range. This damage is then applied to the opponenet.
Receives: opponent being attacked
Returns: nothing
This method calls: Math.random(), subtractHitPoints()
This method is called by: overridden versions of the method in monster and
hero classes and externally
---------------------------------------------------------*/
public void attack(DungeonCharacter opponent)
{
boolean canAttack;
int damage;
canAttack = Math.random() <= chanceToHit;
if (canAttack)
{
damage = (int)(Math.random() * (damageMax - damageMin + 1))
+ damageMin ;
opponent.subtractHitPoints(damage, this);
}//end if can attack
else
{
System.out.println(getName() + "'s attack on " + opponent.getName() +
" failed!");
System.out.println();
}//end else
}//end attack method
//-----------------------------------------------------------------
}//end class Character