Skip to content
Snippets Groups Projects
Commit e890d6f1 authored by Martin Vatshelle's avatar Martin Vatshelle
Browse files

Code to be used in lecture

parent 36a56d87
Branches main
No related tags found
No related merge requests found
package lecture6_interface;
public class Pokemon {
String name;
int healthPoints;
int strength;
// Constructor
Pokemon(String name, int strength){
this.name = name;
this.healthPoints = 100;
this.strength = strength;
}
/**
* Get name of the pokémon
* @return name of pokémon
*/
String getName() {
return name;
}
/**
* Get strength of the pokémon
* @return strength of pokémon
*/
int getStrength() {
return strength;
}
/**
* Get current health points of pokémon
* @return current HP of pokémon
*/
int getCurrentHP() {
return healthPoints;
}
/**
* Check if the pokémon is alive.
* A pokemon is alive if current HP is higher than 0
* @return true if current HP > 0, false if not
*/
boolean isAlive() {
return healthPoints>0;
}
/**
* Damage the pokémon. This method reduces the number of
* health points the pokémon has by <code>damageTaken</code>.
* If <code>damageTaken</code> is higher than the number of current
* health points then set current HP to 0.
*
* @param damageTaken
*/
void damage(int damageTaken) {
healthPoints -= damageTaken;
if(healthPoints<0)
healthPoints = 0;
}
/**
* Attack another pokémon. The method conducts an attack by <code>this</code>
* on <code>target</code>. Calculate the damage using the pokémons strength
* and a random element. Reduce <code>target</code>s health.
*
* If <code>target</code> has 0 HP then print that it was defeated.
*
* @param target pokémon that is being attacked
*/
void attack(Pokemon target) {
int dmg = Math.max(this.strength-target.strength,1);
target.damage(dmg);
}
@Override
public String toString() {
return name+" has "+healthPoints+" left.";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment