summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArne Dußin2021-04-26 10:35:30 +0200
committerArne Dußin2021-04-26 10:35:30 +0200
commit86b0f0028d720807c1388d7026cb093fecf882ca (patch)
tree7756aacd4cb2ff0c95beb70c69b20f39c8213c9a
parent7ae33fba15b2adf9f903869b3c896a7490427b04 (diff)
downloadcappuccino-86b0f0028d720807c1388d7026cb093fecf882ca.tar.gz
cappuccino-86b0f0028d720807c1388d7026cb093fecf882ca.zip
Implement swapping of two stats
-rw-r--r--src/character/stats.rs23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/character/stats.rs b/src/character/stats.rs
index d405eb7..6c4a54f 100644
--- a/src/character/stats.rs
+++ b/src/character/stats.rs
@@ -47,7 +47,17 @@ impl Stats
pub fn swap(&mut self, val1: &str, val2: &str) -> bool
{
- unimplemented!();
+ /* Find the positions of the two arguments */
+ match (STATS.binary_search(&val1), STATS.binary_search(&val2)) {
+ (Ok(pos1), Ok(pos2)) => {
+ /* Swap the two values without making the borrow checker mad. */
+ let tmp = self.values[pos1];
+ self.values[pos1] = self.values[pos2];
+ self.values[pos2] = tmp;
+ true
+ },
+ _ => false,
+ }
}
pub fn get(&self, attribute: &str) -> Option<u8>
@@ -114,4 +124,15 @@ mod tests
assert!(!stats.swap("", ""));
assert_eq!(stats, test_stats());
}
+
+ #[test]
+ fn swap_two_arguments()
+ {
+ let mut stats = test_stats();
+ assert_eq!(stats.get("charisma"), Some(1));
+ assert_eq!(stats.get("strength"), Some(6));
+ assert!(stats.swap("strength", "charisma"));
+ assert_eq!(stats.get("charisma"), Some(6));
+ assert_eq!(stats.get("strength"), Some(1));
+ }
}