pub struct Charmander { /* private fields */ }

Implementations§

source§

impl Charmander

Implement the Charmander struct here.

source

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 😎

source

pub fn level_up(&mut self, levels: usize)

Increases the level of the Charmander by the input levels.

source

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)).

source

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)).

source

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)).

source

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
source

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
source

pub fn fight(charmander1: &mut Self, charmander2: &mut Self)

Pits two Charmanders 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

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.