first steps with refs

This commit is contained in:
2023-11-27 19:21:10 +01:00
parent 227e44dd43
commit 677acf771a
6 changed files with 198 additions and 23 deletions

View File

@@ -12,7 +12,7 @@ impl Player {
pub fn new(name: &str, max_life: i16) -> Player {
Player {
name: name.to_string(),
position: Position::new(0, 1, 1),
position: Position::new(0, 0, 0),
life: max_life,
max_life,
}
@@ -26,6 +26,9 @@ impl Player {
pub fn get_life(&self) -> i16 {
self.life
}
pub fn get_position(&mut self) -> &mut Position {
&mut self.position
}
}
#[test]
@@ -54,4 +57,17 @@ fn test_change_life() {
assert_eq!(p.life, 10);
p.change_life(-12);
assert_eq!(p.life, 0);
}
}
#[test]
fn player_can_move() {
let mut p = Player::new("Teddy Tester", 10);
assert_eq!(p.get_position(), &Position::new(0, 0, 0));
p.get_position().change(1, 2);
assert_eq!(p.get_position(), &Position::new(0, 1, 2));
p.get_position().change(2, 1);
assert_eq!(p.get_position(), &Position::new(0, 3, 3));
p.get_position().set(1, 2, 3);
p.get_position().change(2, 1);
assert_eq!(p.get_position(), &Position::new(1, 4, 4));
}