mirror of
https://github.com/ArthurDanjou/exercism-rust.git
synced 2026-01-28 10:50:29 +01:00
Initial commit
This commit is contained in:
49
role-playing-game/src/lib.rs
Normal file
49
role-playing-game/src/lib.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
// This stub file contains items that aren't used yet; feel free to remove this module attribute
|
||||
// to enable stricter warnings.
|
||||
#![allow(unused)]
|
||||
|
||||
pub struct Player {
|
||||
pub health: u32,
|
||||
pub mana: Option<u32>,
|
||||
pub level: u32,
|
||||
}
|
||||
|
||||
impl Player {
|
||||
pub fn revive(&self) -> Option<Player> {
|
||||
if self.health > 0 {
|
||||
None
|
||||
} else {
|
||||
if self.level >= 10 {
|
||||
Some(Player {
|
||||
health: 100,
|
||||
mana: Some(100),
|
||||
level: self.level,
|
||||
})
|
||||
} else {
|
||||
Some(Player {
|
||||
health: 100,
|
||||
mana: None,
|
||||
level: self.level,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cast_spell(&mut self, mana_cost: u32) -> u32 {
|
||||
if self.level >= 10 {
|
||||
if self.mana < Some(mana_cost) {
|
||||
0
|
||||
} else {
|
||||
self.mana = Some(self.mana.unwrap() - mana_cost);
|
||||
mana_cost * 2
|
||||
}
|
||||
} else {
|
||||
if self.health > mana_cost {
|
||||
self.health -= mana_cost;
|
||||
} else {
|
||||
self.health = 0;
|
||||
}
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user