diff --git a/rust/role-playing-game/.exercism/config.json b/rust/role-playing-game/.exercism/config.json new file mode 100644 index 0000000..95253e6 --- /dev/null +++ b/rust/role-playing-game/.exercism/config.json @@ -0,0 +1,18 @@ +{ + "blurb": "Learn about the `Option` enum by creating a minimal role-playing game", + "authors": [ + "seanchen1991", + "coriolinus" + ], + "files": { + "solution": [ + "src/lib.rs" + ], + "test": [ + "tests/role-playing-game.rs" + ], + "exemplar": [ + ".meta/exemplar.rs" + ] + } +} diff --git a/rust/role-playing-game/.exercism/metadata.json b/rust/role-playing-game/.exercism/metadata.json new file mode 100644 index 0000000..5c95ffd --- /dev/null +++ b/rust/role-playing-game/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"rust","exercise":"role-playing-game","id":"5a893684983c4379b6660897205530df","url":"https://exercism.org/tracks/rust/exercises/role-playing-game","handle":"JBiason","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/rust/role-playing-game/Cargo.lock b/rust/role-playing-game/Cargo.lock new file mode 100644 index 0000000..76f994c --- /dev/null +++ b/rust/role-playing-game/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "role_playing_game" +version = "0.1.0" diff --git a/rust/role-playing-game/Cargo.toml b/rust/role-playing-game/Cargo.toml new file mode 100644 index 0000000..2471854 --- /dev/null +++ b/rust/role-playing-game/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "role_playing_game" +version = "0.1.0" +edition = "2018" + diff --git a/rust/role-playing-game/HELP.md b/rust/role-playing-game/HELP.md new file mode 100644 index 0000000..b4252f8 --- /dev/null +++ b/rust/role-playing-game/HELP.md @@ -0,0 +1,85 @@ +# Help + +## Running the tests + +Execute the tests with: + +```bash +$ cargo test +``` + +All but the first test have been ignored. After you get the first test to +pass, open the tests source file which is located in the `tests` directory +and remove the `#[ignore]` flag from the next test and get the tests to pass +again. Each separate test is a function with `#[test]` flag above it. +Continue, until you pass every test. + +If you wish to run _only ignored_ tests without editing the tests source file, use: + +```bash +$ cargo test -- --ignored +``` + +If you are using Rust 1.51 or later, you can run _all_ tests with + +```bash +$ cargo test -- --include-ignored +``` + +To run a specific test, for example `some_test`, you can use: + +```bash +$ cargo test some_test +``` + +If the specific test is ignored, use: + +```bash +$ cargo test some_test -- --ignored +``` + +To learn more about Rust tests refer to the online [test documentation][rust-tests]. + +[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html + +## Submitting your solution + +You can submit your solution using the `exercism submit src/lib.rs` command. +This command will upload your solution to the Exercism website and print the solution page's URL. + +It's possible to submit an incomplete solution which allows you to: + +- See how others have completed the exercise +- Request help from a mentor + +## Need to get help? + +If you'd like help solving the exercise, check the following pages: + +- The [Rust track's documentation](https://exercism.org/docs/tracks/rust) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs) + +Should those resources not suffice, you could submit your (incomplete) solution to request mentoring. + +## Rust Installation + +Refer to the [exercism help page][help-page] for Rust installation and learning +resources. + +## Submitting the solution + +Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer. + +## Feedback, Issues, Pull Requests + +The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help! + +If you want to know more about Exercism, take a look at the [contribution guide]. + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. + +[help-page]: https://exercism.io/tracks/rust/learning +[github]: https://github.com/exercism/rust +[contribution guide]: https://exercism.io/docs/community/contributors \ No newline at end of file diff --git a/rust/role-playing-game/HINTS.md b/rust/role-playing-game/HINTS.md new file mode 100644 index 0000000..de2efd2 --- /dev/null +++ b/rust/role-playing-game/HINTS.md @@ -0,0 +1 @@ +# Hints \ No newline at end of file diff --git a/rust/role-playing-game/README.md b/rust/role-playing-game/README.md new file mode 100644 index 0000000..e4a0355 --- /dev/null +++ b/rust/role-playing-game/README.md @@ -0,0 +1,79 @@ +# Role-Playing Game + +Welcome to Role-Playing Game on Exercism's Rust Track. +If you need help running the tests or submitting your code, check out `HELP.md`. +If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :) + +## Introduction + +## Instructions + +You're working on implementing a role-playing game. The player's character is represented by the following: + +```rust +pub struct Player { + health: u32, + mana: Option, + level: u32, +} +``` + +Players in this game must reach level 10 before they unlock a mana pool so that they can start casting spells. Before that point, the Player's mana is `None`. + +You're working on two pieces of functionality in this game, the revive mechanic and the spell casting mechanic. + +The `revive` method should check to ensure that the Player is indeed dead (their health has reached 0), and if they are, the method should return a new Player instance with 100 health. +If the Player's level is 10 or above, they should also be revived with 100 mana. +If the Player's level is below 10, their mana should be `None`. The `revive` method should preserve the Player's level. + +```rust +let dead_player = Player { health: 0, mana: None, level: 2 }; +dead_player.revive() +// Returns Player { health: 100, mana: None, level: 2 } +``` + +If the `revive` method is called on a Player whose health is 1 or above, then the method should return `None`. + +```rust +let alive_player = Player { health: 1, mana: Some(15), level: 11 }; +alive_player.revive() +// Returns None +``` + +The `cast_spell` method takes a mutable reference to the Player as well as a `mana_cost` parameter indicating how much mana the spell costs. It returns the amount of damage that the cast spell performs, which will always be two times the mana cost of the spell if the spell is successfully cast. + +- If the player does not have access to a mana pool, attempting to cast the spell must decrease their health by the mana cost of the spell. The damage returned must be 0. + + ```rust + let not_a_wizard_yet = Player { health: 79, mana: None, level: 9 }; + assert_eq!(not_a_wizard_yet.cast_spell(5), 0) + assert_eq!(not_a_wizard_yet.health, 74); + assert_eq!(not_a_wizard_yet.mana, None); + ``` + +- If the player has a mana pool but insufficient mana, the method should not affect the pool, but instead return 0 + + ```rust + let low_mana_wizard = Player { health: 93, mana: Some(3), level: 12 }; + assert_eq!(low_mana_wizard.cast_spell(10), 0); + assert_eq!(low_mana_wizard.health, 93); + assert_eq!(low_mana_wizard.mana, Some(3)); + ``` + +- Otherwise, the `mana_cost` should be deducted from the Player's mana pool and the appropriate amount of damage should be returned. + + ```rust + let wizard = Player { health: 123, mana: Some(30), level: 18 }; + assert_eq!(wizard.cast_spell(10), 20); + assert_eq!(wizard.health, 123); + assert_eq!(wizard.mana, Some(20)); + ``` + +Have fun! + +## Source + +### Created by + +- @seanchen1991 +- @coriolinus \ No newline at end of file diff --git a/rust/role-playing-game/src/lib.rs b/rust/role-playing-game/src/lib.rs new file mode 100644 index 0000000..99b96e7 --- /dev/null +++ b/rust/role-playing-game/src/lib.rs @@ -0,0 +1,42 @@ +// This stub file contains items which 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, + pub level: u32, +} + +impl Player { + pub fn revive(&self) -> Option { + if self.health == 0 { + Some(Self { + health: 100, + mana: Some(100), + level: self.level, + }) + } else { + None + } + } + + pub fn cast_spell(&mut self, mana_cost: u32) -> u32 { + match self.mana { + Some(x) if x >= mana_cost => { + let remaining_mana = x - mana_cost; + if remaining_mana > 0 { + self.mana = Some(remaining_mana); + } else { + self.mana = None; + } + mana_cost * 2 + } + Some(x) => 0, + None => { + self.health = self.health.saturating_sub(mana_cost); + 0 + } + } + } +} diff --git a/rust/role-playing-game/tests/role-playing-game.rs b/rust/role-playing-game/tests/role-playing-game.rs new file mode 100644 index 0000000..f5f5e61 --- /dev/null +++ b/rust/role-playing-game/tests/role-playing-game.rs @@ -0,0 +1,99 @@ +use role_playing_game::*; + +#[test] +fn test_reviving_dead_player() { + let dead_player = Player { + health: 0, + mana: Some(0), + level: 34, + }; + let revived_player = dead_player + .revive() + .expect("reviving a dead player must return Some(player)"); + assert_eq!(revived_player.health, 100); + assert_eq!(revived_player.mana, Some(100)); + assert_eq!(revived_player.level, dead_player.level); +} + +#[test] +fn test_reviving_alive_player() { + let alive_player = Player { + health: 1, + mana: None, + level: 8, + }; + assert!(alive_player.revive().is_none()); +} + +#[test] +fn test_cast_spell_with_enough_mana() { + const HEALTH: u32 = 99; + const MANA: u32 = 100; + const LEVEL: u32 = 100; + const MANA_COST: u32 = 3; + + let mut accomplished_wizard = Player { + health: HEALTH, + mana: Some(MANA), + level: LEVEL, + }; + + assert_eq!(accomplished_wizard.cast_spell(MANA_COST), MANA_COST * 2); + assert_eq!(accomplished_wizard.health, HEALTH); + assert_eq!(accomplished_wizard.mana, Some(MANA - MANA_COST)); + assert_eq!(accomplished_wizard.level, LEVEL); +} + +#[test] +fn test_cast_spell_with_insufficient_mana() { + let mut no_mana_wizard = Player { + health: 56, + mana: Some(2), + level: 22, + }; + + // we want to clone so we can compare before-and-after effects of casting the spell, + // but we don't want to introduce that concept to the student yet, so we have to do it manually + let clone = Player { ..no_mana_wizard }; + + assert_eq!(no_mana_wizard.cast_spell(3), 0); + assert_eq!(no_mana_wizard.health, clone.health); + assert_eq!(no_mana_wizard.mana, clone.mana); + assert_eq!(no_mana_wizard.level, clone.level); +} + +#[test] +fn test_cast_spell_with_no_mana_pool() { + const MANA_COST: u32 = 10; + + let mut underleveled_player = Player { + health: 87, + mana: None, + level: 6, + }; + + let clone = Player { + ..underleveled_player + }; + + assert_eq!(underleveled_player.cast_spell(MANA_COST), 0); + assert_eq!(underleveled_player.health, clone.health - MANA_COST); + assert_eq!(underleveled_player.mana, clone.mana); + assert_eq!(underleveled_player.level, clone.level); +} + +#[test] +fn test_cast_large_spell_with_no_mana_pool() { + const MANA_COST: u32 = 30; + + let mut underleveled_player = Player { + health: 20, + mana: None, + level: 6, + }; + + assert_eq!(underleveled_player.cast_spell(MANA_COST), 0); + assert_eq!(underleveled_player.health, 0); + assert_eq!(underleveled_player.mana, None); + assert_eq!(underleveled_player.level, 6); +}