-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.java
More file actions
166 lines (146 loc) · 3.27 KB
/
Character.java
File metadata and controls
166 lines (146 loc) · 3.27 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
/**
*
*/
import java.util.Random;
/**
* class: Character
*
* course: ITEC 3150 Spring 2015
*
* written: April 1, 2015
*
* @author Alejandro Guzman
* @version 1.0
*
*
* This is a template for a general character. It contains attributes
* name, health, attack and getters and setters for the attributes. It
* contains the isAlive, attack, and toString method.
*
*
*/
public abstract class Character {
/**
* A string to hold the name of the character.
*/
protected String name;
/**
* An integer to keep track of the health.
*/
protected int healthPoints;
/**
* An integer to keep track of the characters attack points.
*/
protected int attackPoints;
/**
* This constructor assigns a passed name to name, sets the health points to
* a default value of 100 and sets the attack points to a random value
* between 7 and 15.
*
* @param name
*/
public Character(String name) {
this.name = name;
healthPoints = 100; // set default health to 100
Random r = new Random();
int rand = r.nextInt((29 - 17) + 1) + 17; // random number for monster
// and player
attackPoints = rand; // sets the attack to a pseudo-random number
// between 7 and 15
}
/**
* This constructor assigns the name, health, and attack.
*
* @param name
* @param healthPoints
* @param attackPoints
*/
public Character(String name, int healthPoints, int attackPoints) {
this.name = name;
this.healthPoints = healthPoints;
this.attackPoints = attackPoints;
}
/**
* isAlive Tests if the health is equal to or below 0.
*
* @return A boolean indicating if the character is alive.
*/
public boolean isAlive() {
boolean isAlive = true;
if (healthPoints <= 0) {
isAlive = false;
}
return isAlive;
}
/**
* attack Take the attackPoints from the character and an item in inventory
* and deduct the total amount from the opponent.
*
* @param c
* character being attacked
* @param i
* item being used
*/
public void attack(Character c, Item i) {
int damage = attackPoints + i.getEffect();
c.healthPoints = c.healthPoints - damage;
}
/**
* attack Deduct the attackPoints of this character from the opponent.
*
* @param c
* character being attacked
*/
public void attack(Character c) {
int damage = attackPoints;
c.healthPoints = c.healthPoints - damage;
}
/**
* toString
*
* @return A string representing of this character.
*/
@Override
public String toString() {
return name + " has " + healthPoints + " health and " + attackPoints
+ " attack.";
}
/**
* getHealthPoints
*
* @return healthPoints
*/
public int getHealthPoints() {
return healthPoints;
}
/**
* setHealthPoints Set the healthPoints.
*
* @param healthPoints
*/
public void setHealthPoints(int healthPoints) {
this.healthPoints = healthPoints;
}
/**
* getAttackPoints
*
* @return the attackPoints
*/
public int getAttackPoints() {
return attackPoints;
}
/**
* setAttackPoints Set the attackPoints.
*
* @param attackPoints
*/
public void setAttackPoints(int attackPoints) {
this.attackPoints = attackPoints;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}