summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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));
+ }
}