add gold to the player

This commit is contained in:
Joachim Lusiardi 2023-12-03 21:20:16 +01:00
parent 220607133a
commit 07c629bdaa
1 changed files with 20 additions and 0 deletions

View File

@ -6,6 +6,7 @@ pub struct Player {
position: Position,
life: i16,
max_life: i16,
gold: usize,
}
impl Player {
@ -15,6 +16,7 @@ impl Player {
position: Position::new(0, 0, 0),
life: max_life,
max_life,
gold: 0,
}
}
pub fn get_name(&self) -> String {
@ -29,6 +31,12 @@ impl Player {
pub fn get_position(&mut self) -> &mut Position {
&mut self.position
}
/// add the given amount to the players gold stash
pub fn retrieve_gold(&mut self, amount: usize) { self.gold += amount }
/// return the size of the players gold stash
pub fn get_gold(&self) -> usize { self.gold }
}
#[test]
@ -38,10 +46,21 @@ fn test_get_name() {
position: Position::new(0, 1, 1),
life: 5,
max_life: 10,
gold: 0,
};
assert_eq!(p.get_name(), "Teddy Tester");
}
#[test]
fn test_can_receive_gold() {
let mut p = Player::new("Teddy Tester", 10);
assert_eq!(p.get_gold(), 0);
p.retrieve_gold(13);
p.retrieve_gold(10);
assert_eq!(p.get_gold(), 23);
}
#[test]
fn test_change_life() {
let mut p = Player {
@ -49,6 +68,7 @@ fn test_change_life() {
position: Position::new(0, 1, 1),
life: 5,
max_life: 10,
gold: 0,
};
assert_eq!(p.life, 5);
p.change_life(-2);