COMP2396
Loading...
Searching...
No Matches
Character.java
Go to the documentation of this file.
1package assignment2;
2
3/**
4 * The Character class represents any game character with a name, skill level, and energy level.
5 * A character can attack using a weapon and can be hurt by attacks.
6 */
7public class Character {
8 private final String characterName;
9 private final int skillLevel;
10 private int energyLevel;
11
12 /**
13 * Constructs a new Character with the specified name, energy level, and skill level.
14 *
15 * @param name the name of the character
16 * @param energyLevel the initial energy level of the character
17 * @param skillLevel the skill level of the character
18 */
19 public Character(String name, int energyLevel, int skillLevel) {
20 this.characterName = name;
21 this.energyLevel = Math.max(0, energyLevel);
22 this.skillLevel = Math.max(0, skillLevel);
23 }
24
25 /**
26 * Returns the name of the character.
27 *
28 * @return the character's name
29 */
30 public String getName() {
31 return characterName;
32 }
33
34 /**
35 * Returns the skill level of the character.
36 *
37 * @return the character's skill level
38 */
39 public int getSkillLevel() {
40 return skillLevel;
41 }
42
43 /**
44 * Returns the current energy level of the character.
45 *
46 * @return the character's current energy level
47 */
48 public int getEnergyLevel() {
49 return energyLevel;
50 }
51
52 /**
53 * Reduces the energy level of the character by the specified attack amount.
54 *
55 * @param attackAmount the amount of energy to reduce
56 * @return the actual amount of energy reduced
57 */
58 public int hurt(int attackAmount) {
59 this.energyLevel -= attackAmount;
60 return attackAmount;
61 }
62
63 /**
64 * Calculates the total attack amount generated by the character.
65 * It is the sum of the weapon's power and the character's skill level.
66 *
67 * @param w1 the weapon used for the attack
68 * @return the total attack amount
69 */
70 public int attack(Weapon w1) {
71 return w1.shoot() + this.skillLevel;
72 }
73
74 /**
75 * Determines whether the character has lost the combat.
76 * A character loses if their energy level is zero or below.
77 *
78 * @return true if the character has lost, false otherwise
79 */
80 public boolean isLose() {
81 return this.energyLevel <= 0;
82 }
83}
The Character class represents any game character with a name, skill level, and energy level.
Definition Character.java:7
int hurt(int attackAmount)
Reduces the energy level of the character by the specified attack amount.
boolean isLose()
Determines whether the character has lost the combat.
int getSkillLevel()
Returns the skill level of the character.
int getEnergyLevel()
Returns the current energy level of the character.
Character(String name, int energyLevel, int skillLevel)
Constructs a new Character with the specified name, energy level, and skill level.
int attack(Weapon w1)
Calculates the total attack amount generated by the character.
String getName()
Returns the name of the character.
final String characterName
Definition Character.java:8
The Weapon class represents any game weapon with a name and power level.
Definition Weapon.java:7
int shoot()
Simulates shooting the weapon and returns the power generated.
Definition Weapon.java:36