Struct pokelab::pokemon::charmander::Charmander
source · pub struct Charmander { /* private fields */ }
Implementations§
source§impl Charmander
impl Charmander
Implement the Charmander struct here.
sourcepub fn new(name: String) -> Self
pub fn new(name: String) -> Self
Takes in a name
as a String
and creates a new Charmander
struct with the following default values:
level: 0
health: 100
attack: 42
defense: 33
Note: We 0-level because we’re programmers 😎
sourcepub fn level_up(&mut self, levels: usize)
pub fn level_up(&mut self, levels: usize)
Increases the level of the Charmander
by the input levels
.
sourcepub fn get_health(&self) -> usize
pub fn get_health(&self) -> usize
Return the health value of the Charmander
.
The health value is calculated by the following formula: (health + (5 * level))
.
sourcepub fn get_attack(&self) -> usize
pub fn get_attack(&self) -> usize
Returns the attack value of the Charmander
.
The attack value is calculated by the following formula: (attack + (3 * level))
.
sourcepub fn get_defense(&self) -> usize
pub fn get_defense(&self) -> usize
Returns the defense value of the Charmander
.
The defense value is calculated by the following formula: (defense + (4 * level))
.
sourcepub fn take_damage(&mut self, damage: usize)
pub fn take_damage(&mut self, damage: usize)
Takes damage
subtracted by Charmander
’s defense
.
If the Charmander
takes more damage than it has health,
this function should panic with the message
"{name} fainted!"
, where "{name}"
is the name of the Charmander
.
In other words, we should panic if the Charmander
hits 0 health.
let mut charmander = Charmander::new(String::from("Ben"));
assert_eq!(charmander.get_health(), 100);
assert_eq!(charmander.get_defense(), 33);
charmander.take_damage(30);
assert_eq!(charmander.get_health(), 100); // Not enough damage to overcome defense
charmander.take_damage(66);
assert_eq!(charmander.get_health(), 67); // 33 actual damage taken
sourcepub fn attack(&self, other: &mut Self)
pub fn attack(&self, other: &mut Self)
Attacks another Charmander
struct and deals damage.
attack
will do damage to the other
Charmander
equal to its current attack. Will panic if the other Charmander
faints.
let mut attacker = Charmander::new(String::from("David"));
let mut defender = Charmander::new(String::from("Connor"));
assert_eq!(attacker.get_attack(), 42);
assert_eq!(defender.get_defense(), 33);
attacker.attack(&mut defender);
assert_eq!(defender.get_health(), 91); // 9 damage
attacker.level_up(1);
attacker.attack(&mut defender);
assert_eq!(defender.get_health(), 79); // 12 damage
sourcepub fn fight(charmander1: &mut Self, charmander2: &mut Self)
pub fn fight(charmander1: &mut Self, charmander2: &mut Self)
Pits two Charmander
s against each other!
Note that this is an associated function, so you cannot write charmander.fight(other)
.
The Charmander
with the higher level will go first.
Will panic if a Charmander
faints.
let mut charmander1 = Charmander::new(String::from("Ben"));
let mut charmander2 = Charmander::new(String::from("Connor"));
charmander1.level_up(5);
assert_eq!(charmander1.get_health(), 125); // +25
assert_eq!(charmander1.get_attack(), 57); // +15
assert_eq!(charmander1.get_defense(), 53); // +20
Charmander::fight(&mut charmander1, &mut charmander2);
assert_eq!(charmander1.get_health(), 125); // no damage taken
assert_eq!(charmander2.get_health(), 76); // 57 - 33 = 24 damage taken