-
Notifications
You must be signed in to change notification settings - Fork 543
Implement Bowling #213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement Bowling #213
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1e14ac0
Add tests for Bowling exercise
whit0694 4644815
Handle results, reorder tests, fix errors
whit0694 e3d3c20
Include crate, stylistic fixes
whit0694 64d8305
Ignore all but first test
whit0694 ef00726
Make game mutable
whit0694 3e7025f
Error on the first roll after the game is complete
whit0694 7ed61e2
Spelling
whit0694 dc78283
Test name clarification
whit0694 37f247d
Declare results of game.roll as unused
whit0694 fd2f804
Very rough implementation
whit0694 882c9bb
Add fill-ball edge cases
whit0694 ac5a7e4
Refactoring of example
whit0694 b7effb3
Use `sum()`
whit0694 a6d751b
Place bowling after queen-attack
whit0694 d2d1d17
Ignore recently added test
whit0694 d3ca115
A more readable bonus_valid
whit0694 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
"robot-simulator", | ||
"bracket-push", | ||
"queen-attack", | ||
"bowling", | ||
"sublist", | ||
"space-age", | ||
"allergies", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
/target/ | ||
|
||
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[package] | ||
name = "bowling" | ||
version = "0.0.0" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
|
||
pub struct BowlingGame { | ||
frames: Vec<Frame>, | ||
} | ||
|
||
struct Frame { | ||
rolls: Vec<u16>, | ||
bonus: Vec<u16>, | ||
} | ||
|
||
impl Frame { | ||
fn score(&self) -> u16 { | ||
self.roll_score() + self.bonus_score() | ||
} | ||
|
||
fn roll_score(&self) -> u16 { | ||
self.rolls.iter().sum() | ||
} | ||
|
||
fn bonus_score(&self) -> u16 { | ||
self.bonus.iter().sum() | ||
} | ||
|
||
fn is_valid(&self) -> bool { | ||
self.rolls_valid() && self.bonus_valid() | ||
} | ||
|
||
fn rolls_valid(&self) -> bool { | ||
self.roll_score() <= 10 | ||
} | ||
|
||
fn bonus_valid(&self) -> bool { | ||
if self.is_open() || !self.bonus_done() { | ||
return true; | ||
} | ||
|
||
if self.is_spare() { | ||
return self.bonus_score() <= 10; | ||
} | ||
|
||
if let Some(first) = self.bonus.iter().next() { | ||
if *first == 10 { | ||
self.bonus_score() <= 20 | ||
} else { | ||
self.bonus_score() <= 10 | ||
} | ||
} else { | ||
unreachable!(); | ||
} | ||
} | ||
|
||
fn is_complete(&self) -> bool { | ||
self.is_open() || self.bonus_done() | ||
} | ||
|
||
fn rolls_done(&self) -> bool { | ||
self.rolls.len() == 2 || self.is_strike() | ||
} | ||
|
||
fn bonus_done(&self) -> bool { | ||
(self.is_spare() && self.bonus.len() == 1) || (self.is_strike() && self.bonus.len() == 2) | ||
} | ||
|
||
fn is_open(&self) -> bool { | ||
self.rolls.len() == 2 && self.roll_score() < 10 | ||
} | ||
|
||
fn is_spare(&self) -> bool { | ||
self.rolls.len() == 2 && self.roll_score() == 10 | ||
} | ||
|
||
fn is_strike(&self) -> bool { | ||
self.rolls.len() == 1 && self.roll_score() == 10 | ||
} | ||
|
||
fn add_roll(&mut self, roll: u16) { | ||
if !self.is_complete() { | ||
if self.is_spare() || self.is_strike() { | ||
self.bonus.push(roll) | ||
} else { | ||
self.rolls.push(roll) | ||
} | ||
} | ||
} | ||
|
||
fn new() -> Self { | ||
Frame { | ||
rolls: vec![], | ||
bonus: vec![], | ||
} | ||
} | ||
} | ||
|
||
impl BowlingGame { | ||
pub fn new() -> Self { | ||
BowlingGame { frames: vec![Frame::new()] } | ||
} | ||
|
||
pub fn roll(&mut self, pins: u16) -> Result<(), &'static str> { | ||
if pins > 10 { | ||
Err("Greater than 10 pins") | ||
} else { | ||
if self.score().is_ok() { | ||
return Err("Game Finished. No more rolls."); | ||
} | ||
|
||
for mut frame in self.frames.iter_mut() { | ||
frame.add_roll(pins) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. big hammer, adding it to all the frames instead of just the ones that need it. But ultimately I don't mind |
||
} | ||
|
||
if self.frames.iter().any(|f| !f.is_valid()) { | ||
return Err("Invalid Roll"); | ||
} | ||
|
||
if self.frames.iter().last().unwrap().rolls_done() && self.frames.len() < 10 { | ||
self.frames.push(Frame::new()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn score(&self) -> Result<u16, &'static str> { | ||
if !self.is_done() { | ||
Err("Game Incomplete") | ||
} else { | ||
Ok(self.frames.iter().fold(0, |acc, r| acc + r.score())) | ||
} | ||
} | ||
|
||
fn is_done(&self) -> bool { | ||
self.frames.len() == 10 && self.frames.iter().all(|f| f.is_complete()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you expressed discontent about it, are you looking for ideas? I could suggest either early
return
, or transformations of the formif A then B else true
to!A || B