Last active
December 21, 2016 14:34
-
-
Save U007D/69e432f37401dbbe3597694491edb410 to your computer and use it in GitHub Desktop.
Bowling Game Kata in C++14 (using range-v3)
This file contains 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
//Can be called by client as follows: ScoreRollsByFrame({10, 3, 5, /* ... valid set of rolls */ }) | |
//Note: Cast required since C++'s usual arithmetic conversions yield u16 + u8 -> i32 (!) | |
#include "range/v3/all.hpp" | |
uint16_t Score(const std::vector<uint8_t>& rolls, const uint8_t frame) | |
{ | |
return frame > 10 || ranges::empty(rolls) | |
? 0 | |
: static_cast<uint16_t>(ranges::accumulate(rolls | |
| ranges::view::take(rolls[0] + rolls[1] >= 10 ? 3 : 2), 0) | |
+ Score(rolls | |
| ranges::view::drop(rolls[0] == 10 ? 1 : 2), frame + 1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment