From 07c629bdaa0098cf6415bbd0f36b6a87048ad28d Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Sun, 3 Dec 2023 21:20:16 +0100 Subject: [PATCH] add gold to the player --- src/player.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/player.rs b/src/player.rs index 05dbca2..c803073 100644 --- a/src/player.rs +++ b/src/player.rs @@ -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);