Skip to content

Instantly share code, notes, and snippets.

@U007D
Last active December 21, 2016 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save U007D/69e432f37401dbbe3597694491edb410 to your computer and use it in GitHub Desktop.
Save U007D/69e432f37401dbbe3597694491edb410 to your computer and use it in GitHub Desktop.
Bowling Game Kata in C++14 (using range-v3)
//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