Browse Source

Started new exercise: Rust Bowling

master
Julio Biason 4 years ago
commit
b6f8f965cc
  1. 1
      rust/bowling/.exercism/metadata.json
  2. 8
      rust/bowling/.gitignore
  3. 4
      rust/bowling/Cargo.toml
  4. 141
      rust/bowling/README.md
  5. 21
      rust/bowling/src/lib.rs
  6. 449
      rust/bowling/tests/bowling.rs

1
rust/bowling/.exercism/metadata.json

@ -0,0 +1 @@
{"track":"rust","exercise":"bowling","id":"2ea4af54bcad4192b8d673a03079f1b3","url":"https://exercism.io/my/solutions/2ea4af54bcad4192b8d673a03079f1b3","handle":"JBiason","is_requester":true,"auto_approve":false}

8
rust/bowling/.gitignore vendored

@ -0,0 +1,8 @@
# Generated by Cargo
# will have compiled files and executables
/target/
**/*.rs.bk
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock

4
rust/bowling/Cargo.toml

@ -0,0 +1,4 @@
[package]
edition = "2018"
name = "bowling"
version = "1.2.0"

141
rust/bowling/README.md

@ -0,0 +1,141 @@
# Bowling
Score a bowling game.
Bowling is a game where players roll a heavy ball to knock down pins
arranged in a triangle. Write code to keep track of the score
of a game of bowling.
## Scoring Bowling
The game consists of 10 frames. A frame is composed of one or two ball
throws with 10 pins standing at frame initialization. There are three
cases for the tabulation of a frame.
* An open frame is where a score of less than 10 is recorded for the
frame. In this case the score for the frame is the number of pins
knocked down.
* A spare is where all ten pins are knocked down by the second
throw. The total value of a spare is 10 plus the number of pins
knocked down in their next throw.
* A strike is where all ten pins are knocked down by the first
throw. The total value of a strike is 10 plus the number of pins
knocked down in the next two throws. If a strike is immediately
followed by a second strike, then the value of the first strike
cannot be determined until the ball is thrown one more time.
Here is a three frame example:
| Frame 1 | Frame 2 | Frame 3 |
| :-------------: |:-------------:| :---------------------:|
| X (strike) | 5/ (spare) | 9 0 (open frame) |
Frame 1 is (10 + 5 + 5) = 20
Frame 2 is (5 + 5 + 9) = 19
Frame 3 is (9 + 0) = 9
This means the current running total is 48.
The tenth frame in the game is a special case. If someone throws a
strike or a spare then they get a fill ball. Fill balls exist to
calculate the total of the 10th frame. Scoring a strike or spare on
the fill ball does not give the player more fill balls. The total
value of the 10th frame is the total number of pins knocked down.
For a tenth frame of X1/ (strike and a spare), the total value is 20.
For a tenth frame of XXX (three strikes), the total value is 30.
## Requirements
Write code to keep track of the score of a game of bowling. It should
support two operations:
* `roll(pins : int)` is called each time the player rolls a ball. The
argument is the number of pins knocked down.
* `score() : int` is called only at the very end of the game. It
returns the total score for that game.
## Rust Installation
Refer to the [exercism help page][help-page] for Rust installation and learning
resources.
## Writing the Code
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 all ignored tests without editing the tests source file, use:
```bash
$ cargo test -- --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]
Make sure to read the [Modules][modules] chapter if you
haven't already, it will help you with organizing your files.
## Further improvements
After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
To format your solution, inside the solution directory use
```bash
cargo fmt
```
To see, if your solution contains some common ineffective use cases, inside the solution directory use
```bash
cargo clippy --all-targets
```
## 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 [exercism/rust](https://github.com/exercism/rust) repository on 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](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
[help-page]: https://exercism.io/tracks/rust/learning
[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
## Source
The Bowling Game Kata at but UncleBob [http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata](http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

21
rust/bowling/src/lib.rs

@ -0,0 +1,21 @@
#[derive(Debug, PartialEq)]
pub enum Error {
NotEnoughPinsLeft,
GameComplete,
}
pub struct BowlingGame {}
impl BowlingGame {
pub fn new() -> Self {
unimplemented!();
}
pub fn roll(&mut self, pins: u16) -> Result<(), Error> {
unimplemented!("Record that {} pins have been scored", pins);
}
pub fn score(&self) -> Option<u16> {
unimplemented!("Return the score if the game is complete, or None if not.");
}
}

449
rust/bowling/tests/bowling.rs

@ -0,0 +1,449 @@
use bowling::*;
#[test]
fn roll_returns_a_result() {
let mut game = BowlingGame::new();
assert!(game.roll(0).is_ok());
}
#[test]
#[ignore]
fn you_cannot_roll_more_than_ten_pins_in_a_single_roll() {
let mut game = BowlingGame::new();
assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft));
}
#[test]
#[ignore]
fn a_game_score_is_some_if_ten_frames_have_been_rolled() {
let mut game = BowlingGame::new();
for _ in 0..10 {
let _ = game.roll(0);
let _ = game.roll(0);
}
assert!(game.score().is_some());
}
#[test]
#[ignore]
fn you_cannot_score_a_game_with_no_rolls() {
let game = BowlingGame::new();
assert_eq!(game.score(), None);
}
#[test]
#[ignore]
fn a_game_score_is_none_if_fewer_than_ten_frames_have_been_rolled() {
let mut game = BowlingGame::new();
for _ in 0..9 {
let _ = game.roll(0);
let _ = game.roll(0);
}
assert_eq!(game.score(), None);
}
#[test]
#[ignore]
fn a_roll_is_err_if_the_game_is_done() {
let mut game = BowlingGame::new();
for _ in 0..10 {
let _ = game.roll(0);
let _ = game.roll(0);
}
assert_eq!(game.roll(0), Err(Error::GameComplete));
}
#[test]
#[ignore]
fn twenty_zero_pin_rolls_scores_zero() {
let mut game = BowlingGame::new();
for _ in 0..20 {
let _ = game.roll(0);
}
assert_eq!(game.score(), Some(0));
}
#[test]
#[ignore]
fn ten_frames_without_a_strike_or_spare() {
let mut game = BowlingGame::new();
for _ in 0..10 {
let _ = game.roll(3);
let _ = game.roll(6);
}
assert_eq!(game.score(), Some(90));
}
#[test]
#[ignore]
fn spare_in_the_first_frame_followed_by_zeros() {
let mut game = BowlingGame::new();
let _ = game.roll(6);
let _ = game.roll(4);
for _ in 0..18 {
let _ = game.roll(0);
}
assert_eq!(game.score(), Some(10));
}
#[test]
#[ignore]
fn points_scored_in_the_roll_after_a_spare_are_counted_twice_as_a_bonus() {
let mut game = BowlingGame::new();
let _ = game.roll(6);
let _ = game.roll(4);
let _ = game.roll(3);
for _ in 0..17 {
let _ = game.roll(0);
}
assert_eq!(game.score(), Some(16));
}
#[test]
#[ignore]
fn consecutive_spares_each_get_a_one_roll_bonus() {
let mut game = BowlingGame::new();
let _ = game.roll(5);
let _ = game.roll(5);
let _ = game.roll(3);
let _ = game.roll(7);
let _ = game.roll(4);
for _ in 0..15 {
let _ = game.roll(0);
}
assert_eq!(game.score(), Some(31));
}
#[test]
#[ignore]
fn if_the_last_frame_is_a_spare_you_get_one_extra_roll_that_is_scored_once() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(5);
let _ = game.roll(5);
let _ = game.roll(7);
assert_eq!(game.score(), Some(17));
}
#[test]
#[ignore]
fn a_strike_earns_ten_points_in_a_frame_with_a_single_roll() {
let mut game = BowlingGame::new();
let _ = game.roll(10);
for _ in 0..18 {
let _ = game.roll(0);
}
assert_eq!(game.score(), Some(10));
}
#[test]
#[ignore]
fn points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus() {
let mut game = BowlingGame::new();
let _ = game.roll(10);
let _ = game.roll(5);
let _ = game.roll(3);
for _ in 0..16 {
let _ = game.roll(0);
}
assert_eq!(game.score(), Some(26));
}
#[test]
#[ignore]
fn consecutive_strikes_each_get_the_two_roll_bonus() {
let mut game = BowlingGame::new();
let _ = game.roll(10);
let _ = game.roll(10);
let _ = game.roll(10);
let _ = game.roll(5);
let _ = game.roll(3);
for _ in 0..12 {
let _ = game.roll(0);
}
assert_eq!(game.score(), Some(81));
}
#[test]
#[ignore]
fn a_strike_in_the_last_frame_earns_a_two_roll_bonus_that_is_counted_once() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
let _ = game.roll(7);
let _ = game.roll(1);
assert_eq!(game.score(), Some(18));
}
#[test]
#[ignore]
fn a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
let _ = game.roll(7);
let _ = game.roll(3);
assert_eq!(game.score(), Some(20));
}
#[test]
#[ignore]
fn strikes_with_the_two_roll_bonus_do_not_get_a_bonus_roll() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
let _ = game.roll(10);
let _ = game.roll(10);
assert_eq!(game.score(), Some(30));
}
#[test]
#[ignore]
fn a_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(7);
let _ = game.roll(3);
let _ = game.roll(10);
assert_eq!(game.score(), Some(20));
}
#[test]
#[ignore]
fn all_strikes_is_a_perfect_score_of_300() {
let mut game = BowlingGame::new();
for _ in 0..12 {
let _ = game.roll(10);
}
assert_eq!(game.score(), Some(300));
}
#[test]
#[ignore]
fn you_cannot_roll_more_than_ten_pins_in_a_single_frame() {
let mut game = BowlingGame::new();
assert!(game.roll(5).is_ok());
assert_eq!(game.roll(6), Err(Error::NotEnoughPinsLeft));
}
#[test]
#[ignore]
fn first_bonus_ball_after_a_final_strike_cannot_score_an_invalid_number_of_pins() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft));
}
#[test]
#[ignore]
fn the_two_balls_after_a_final_strike_cannot_score_an_invalid_number_of_pins() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
assert!(game.roll(5).is_ok());
assert_eq!(game.roll(6), Err(Error::NotEnoughPinsLeft));
}
#[test]
#[ignore]
fn the_two_balls_after_a_final_strike_can_be_a_strike_and_non_strike() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
assert!(game.roll(10).is_ok());
assert!(game.roll(6).is_ok());
}
#[test]
#[ignore]
fn the_two_balls_after_a_final_strike_cannot_be_a_non_strike_followed_by_a_strike() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
assert!(game.roll(6).is_ok());
assert_eq!(game.roll(10), Err(Error::NotEnoughPinsLeft));
}
#[test]
#[ignore]
fn second_bonus_ball_after_a_final_strike_cannot_score_an_invalid_number_of_pins_even_if_first_is_strike(
) {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
assert!(game.roll(10).is_ok());
assert_eq!(game.roll(11), Err(Error::NotEnoughPinsLeft));
}
#[test]
#[ignore]
fn if_the_last_frame_is_a_strike_you_cannot_score_before_the_extra_rolls_are_taken() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(10);
assert_eq!(game.score(), None);
let _ = game.roll(10);
assert_eq!(game.score(), None);
let _ = game.roll(10);
assert!(game.score().is_some());
}
#[test]
#[ignore]
fn if_the_last_frame_is_a_spare_you_cannot_create_a_score_before_extra_roll_is_taken() {
let mut game = BowlingGame::new();
for _ in 0..18 {
let _ = game.roll(0);
}
let _ = game.roll(5);
let _ = game.roll(5);
assert_eq!(game.score(), None);
let _ = game.roll(10);
assert!(game.score().is_some());
}
#[test]
#[ignore]
fn cannot_roll_after_bonus_roll_for_spare() {
let mut game = BowlingGame::new();
for _ in 0..9 {
let _ = game.roll(0);
let _ = game.roll(0);
}
let _ = game.roll(7);
let _ = game.roll(3);
assert!(game.roll(2).is_ok());
assert_eq!(game.roll(2), Err(Error::GameComplete));
}
#[test]
#[ignore]
fn cannot_roll_after_bonus_roll_for_strike() {
let mut game = BowlingGame::new();
for _ in 0..9 {
let _ = game.roll(0);
let _ = game.roll(0);
}
let _ = game.roll(10);
let _ = game.roll(3);
assert!(game.roll(2).is_ok());
assert_eq!(game.roll(2), Err(Error::GameComplete));
}
#[test]
#[ignore]
fn last_two_strikes_followed_by_only_last_bonus_with_non_strike_points() {
let mut game = BowlingGame::new();
for _ in 0..16 {
let _ = game.roll(0);
}
let _ = game.roll(10);
let _ = game.roll(10);
let _ = game.roll(0);
let _ = game.roll(1);
assert_eq!(game.score(), Some(31));
}
Loading…
Cancel
Save