A dice game called “Greed”












9












$begingroup$


This game is a variation of the Greed dice game. Imagine we have 5 dice. We roll, them, and jot down the results. Then, based on this chart:



Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 point


We calculate the score. This program exactly does that. It has two functions, one is greed() which takes a vector of 5 integers between 1 and 6 and calculates the score, and the other is greed_rand() which first generates the vector randomly, and then calculates the score.



#include <iostream>
#include <vector>
#include <random>
#include <map>

std::random_device seeder;
std::mt19937 engine(seeder());
std::uniform_int_distribution<int> dist(1, 6);

typedef std::vector<int> list_type;

int greed(list_type die_rolls)
{
int ret = 0;
std::map<int, int> cnt;

for (int i = 1; i <= 6; ++i)
{
cnt[i] = 0;
}

for (auto &d : die_rolls)
{
++cnt[d];
}

if (cnt[1] == 3)
{
ret += 1000;
}
if (cnt[1] == 2)
{
ret += 200;
}
if (cnt[1] == 1)
{
ret += 100;
}
if (cnt[1] == 4)
{
ret += 1100;
}
if (cnt[1] == 5)
{
ret += 1200;
}
if (cnt[2] == 3)
{
ret += 200;
}
if (cnt[3] == 3)
{
ret += 300;
}
if (cnt[4] == 3)
{
ret += 400;
}
if (cnt[5] == 1)
{
ret += 50;
}
if (cnt[5] == 2)
{
ret += 100;
}
if (cnt[5] == 3)
{
ret += 500;
}
if (cnt[5] == 4)
{
ret += 550;
}
if (cnt[5] == 5)
{
ret += 600;
}
if (cnt[6] == 3)
{
ret += 600;
}


return ret;

}


int greed_rand()
{
list_type rolls_rand;

for (int i = 1; i <= 5; ++i)
{
rolls_rand.push_back(dist(engine));
}

return greed(rolls_rand);
}


int main() {

list_type rolls = {1, 1, 1, 5, 5};
std::cout << greed(rolls) << std::endl;
std::cout << greed_rand() << std::endl;

return 0;
}


The output of this program is:



1100
150


Note: If you're on Windows, change the random seeder to time(NULL).










share|improve this question











$endgroup$








  • 6




    $begingroup$
    Not enough to warrant an answer but I'm surprised the very good answers we have don't mentioned the function names. Though the game is called greed, wouldn't calculate_score and roll_dice be way more explanatory as function names?
    $endgroup$
    – sbecker
    Nov 15 '18 at 8:23










  • $begingroup$
    @sbecker You're right, from now on I'll choose better function and variable names.
    $endgroup$
    – ChubakBidpaa
    Nov 15 '18 at 8:46






  • 2




    $begingroup$
    It's a bug or a feature? If the we got five 6, which score you expect? 0 or 600? Actually you get 0 but I think 600 seems more logic.
    $endgroup$
    – Calak
    Nov 15 '18 at 13:27










  • $begingroup$
    I know this game by another name but I cant recall what it is.
    $endgroup$
    – Matt
    Nov 15 '18 at 18:57










  • $begingroup$
    @Matt 10000, Greedy, zilch, Tora?
    $endgroup$
    – Calak
    Nov 15 '18 at 21:40
















9












$begingroup$


This game is a variation of the Greed dice game. Imagine we have 5 dice. We roll, them, and jot down the results. Then, based on this chart:



Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 point


We calculate the score. This program exactly does that. It has two functions, one is greed() which takes a vector of 5 integers between 1 and 6 and calculates the score, and the other is greed_rand() which first generates the vector randomly, and then calculates the score.



#include <iostream>
#include <vector>
#include <random>
#include <map>

std::random_device seeder;
std::mt19937 engine(seeder());
std::uniform_int_distribution<int> dist(1, 6);

typedef std::vector<int> list_type;

int greed(list_type die_rolls)
{
int ret = 0;
std::map<int, int> cnt;

for (int i = 1; i <= 6; ++i)
{
cnt[i] = 0;
}

for (auto &d : die_rolls)
{
++cnt[d];
}

if (cnt[1] == 3)
{
ret += 1000;
}
if (cnt[1] == 2)
{
ret += 200;
}
if (cnt[1] == 1)
{
ret += 100;
}
if (cnt[1] == 4)
{
ret += 1100;
}
if (cnt[1] == 5)
{
ret += 1200;
}
if (cnt[2] == 3)
{
ret += 200;
}
if (cnt[3] == 3)
{
ret += 300;
}
if (cnt[4] == 3)
{
ret += 400;
}
if (cnt[5] == 1)
{
ret += 50;
}
if (cnt[5] == 2)
{
ret += 100;
}
if (cnt[5] == 3)
{
ret += 500;
}
if (cnt[5] == 4)
{
ret += 550;
}
if (cnt[5] == 5)
{
ret += 600;
}
if (cnt[6] == 3)
{
ret += 600;
}


return ret;

}


int greed_rand()
{
list_type rolls_rand;

for (int i = 1; i <= 5; ++i)
{
rolls_rand.push_back(dist(engine));
}

return greed(rolls_rand);
}


int main() {

list_type rolls = {1, 1, 1, 5, 5};
std::cout << greed(rolls) << std::endl;
std::cout << greed_rand() << std::endl;

return 0;
}


The output of this program is:



1100
150


Note: If you're on Windows, change the random seeder to time(NULL).










share|improve this question











$endgroup$








  • 6




    $begingroup$
    Not enough to warrant an answer but I'm surprised the very good answers we have don't mentioned the function names. Though the game is called greed, wouldn't calculate_score and roll_dice be way more explanatory as function names?
    $endgroup$
    – sbecker
    Nov 15 '18 at 8:23










  • $begingroup$
    @sbecker You're right, from now on I'll choose better function and variable names.
    $endgroup$
    – ChubakBidpaa
    Nov 15 '18 at 8:46






  • 2




    $begingroup$
    It's a bug or a feature? If the we got five 6, which score you expect? 0 or 600? Actually you get 0 but I think 600 seems more logic.
    $endgroup$
    – Calak
    Nov 15 '18 at 13:27










  • $begingroup$
    I know this game by another name but I cant recall what it is.
    $endgroup$
    – Matt
    Nov 15 '18 at 18:57










  • $begingroup$
    @Matt 10000, Greedy, zilch, Tora?
    $endgroup$
    – Calak
    Nov 15 '18 at 21:40














9












9








9


2



$begingroup$


This game is a variation of the Greed dice game. Imagine we have 5 dice. We roll, them, and jot down the results. Then, based on this chart:



Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 point


We calculate the score. This program exactly does that. It has two functions, one is greed() which takes a vector of 5 integers between 1 and 6 and calculates the score, and the other is greed_rand() which first generates the vector randomly, and then calculates the score.



#include <iostream>
#include <vector>
#include <random>
#include <map>

std::random_device seeder;
std::mt19937 engine(seeder());
std::uniform_int_distribution<int> dist(1, 6);

typedef std::vector<int> list_type;

int greed(list_type die_rolls)
{
int ret = 0;
std::map<int, int> cnt;

for (int i = 1; i <= 6; ++i)
{
cnt[i] = 0;
}

for (auto &d : die_rolls)
{
++cnt[d];
}

if (cnt[1] == 3)
{
ret += 1000;
}
if (cnt[1] == 2)
{
ret += 200;
}
if (cnt[1] == 1)
{
ret += 100;
}
if (cnt[1] == 4)
{
ret += 1100;
}
if (cnt[1] == 5)
{
ret += 1200;
}
if (cnt[2] == 3)
{
ret += 200;
}
if (cnt[3] == 3)
{
ret += 300;
}
if (cnt[4] == 3)
{
ret += 400;
}
if (cnt[5] == 1)
{
ret += 50;
}
if (cnt[5] == 2)
{
ret += 100;
}
if (cnt[5] == 3)
{
ret += 500;
}
if (cnt[5] == 4)
{
ret += 550;
}
if (cnt[5] == 5)
{
ret += 600;
}
if (cnt[6] == 3)
{
ret += 600;
}


return ret;

}


int greed_rand()
{
list_type rolls_rand;

for (int i = 1; i <= 5; ++i)
{
rolls_rand.push_back(dist(engine));
}

return greed(rolls_rand);
}


int main() {

list_type rolls = {1, 1, 1, 5, 5};
std::cout << greed(rolls) << std::endl;
std::cout << greed_rand() << std::endl;

return 0;
}


The output of this program is:



1100
150


Note: If you're on Windows, change the random seeder to time(NULL).










share|improve this question











$endgroup$




This game is a variation of the Greed dice game. Imagine we have 5 dice. We roll, them, and jot down the results. Then, based on this chart:



Three 1's => 1000 points
Three 6's => 600 points
Three 5's => 500 points
Three 4's => 400 points
Three 3's => 300 points
Three 2's => 200 points
One 1 => 100 points
One 5 => 50 point


We calculate the score. This program exactly does that. It has two functions, one is greed() which takes a vector of 5 integers between 1 and 6 and calculates the score, and the other is greed_rand() which first generates the vector randomly, and then calculates the score.



#include <iostream>
#include <vector>
#include <random>
#include <map>

std::random_device seeder;
std::mt19937 engine(seeder());
std::uniform_int_distribution<int> dist(1, 6);

typedef std::vector<int> list_type;

int greed(list_type die_rolls)
{
int ret = 0;
std::map<int, int> cnt;

for (int i = 1; i <= 6; ++i)
{
cnt[i] = 0;
}

for (auto &d : die_rolls)
{
++cnt[d];
}

if (cnt[1] == 3)
{
ret += 1000;
}
if (cnt[1] == 2)
{
ret += 200;
}
if (cnt[1] == 1)
{
ret += 100;
}
if (cnt[1] == 4)
{
ret += 1100;
}
if (cnt[1] == 5)
{
ret += 1200;
}
if (cnt[2] == 3)
{
ret += 200;
}
if (cnt[3] == 3)
{
ret += 300;
}
if (cnt[4] == 3)
{
ret += 400;
}
if (cnt[5] == 1)
{
ret += 50;
}
if (cnt[5] == 2)
{
ret += 100;
}
if (cnt[5] == 3)
{
ret += 500;
}
if (cnt[5] == 4)
{
ret += 550;
}
if (cnt[5] == 5)
{
ret += 600;
}
if (cnt[6] == 3)
{
ret += 600;
}


return ret;

}


int greed_rand()
{
list_type rolls_rand;

for (int i = 1; i <= 5; ++i)
{
rolls_rand.push_back(dist(engine));
}

return greed(rolls_rand);
}


int main() {

list_type rolls = {1, 1, 1, 5, 5};
std::cout << greed(rolls) << std::endl;
std::cout << greed_rand() << std::endl;

return 0;
}


The output of this program is:



1100
150


Note: If you're on Windows, change the random seeder to time(NULL).







c++ game random c++14 dice






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 14:42









Toby Speight

23.9k639113




23.9k639113










asked Nov 15 '18 at 0:23









ChubakBidpaaChubakBidpaa

12217




12217








  • 6




    $begingroup$
    Not enough to warrant an answer but I'm surprised the very good answers we have don't mentioned the function names. Though the game is called greed, wouldn't calculate_score and roll_dice be way more explanatory as function names?
    $endgroup$
    – sbecker
    Nov 15 '18 at 8:23










  • $begingroup$
    @sbecker You're right, from now on I'll choose better function and variable names.
    $endgroup$
    – ChubakBidpaa
    Nov 15 '18 at 8:46






  • 2




    $begingroup$
    It's a bug or a feature? If the we got five 6, which score you expect? 0 or 600? Actually you get 0 but I think 600 seems more logic.
    $endgroup$
    – Calak
    Nov 15 '18 at 13:27










  • $begingroup$
    I know this game by another name but I cant recall what it is.
    $endgroup$
    – Matt
    Nov 15 '18 at 18:57










  • $begingroup$
    @Matt 10000, Greedy, zilch, Tora?
    $endgroup$
    – Calak
    Nov 15 '18 at 21:40














  • 6




    $begingroup$
    Not enough to warrant an answer but I'm surprised the very good answers we have don't mentioned the function names. Though the game is called greed, wouldn't calculate_score and roll_dice be way more explanatory as function names?
    $endgroup$
    – sbecker
    Nov 15 '18 at 8:23










  • $begingroup$
    @sbecker You're right, from now on I'll choose better function and variable names.
    $endgroup$
    – ChubakBidpaa
    Nov 15 '18 at 8:46






  • 2




    $begingroup$
    It's a bug or a feature? If the we got five 6, which score you expect? 0 or 600? Actually you get 0 but I think 600 seems more logic.
    $endgroup$
    – Calak
    Nov 15 '18 at 13:27










  • $begingroup$
    I know this game by another name but I cant recall what it is.
    $endgroup$
    – Matt
    Nov 15 '18 at 18:57










  • $begingroup$
    @Matt 10000, Greedy, zilch, Tora?
    $endgroup$
    – Calak
    Nov 15 '18 at 21:40








6




6




$begingroup$
Not enough to warrant an answer but I'm surprised the very good answers we have don't mentioned the function names. Though the game is called greed, wouldn't calculate_score and roll_dice be way more explanatory as function names?
$endgroup$
– sbecker
Nov 15 '18 at 8:23




$begingroup$
Not enough to warrant an answer but I'm surprised the very good answers we have don't mentioned the function names. Though the game is called greed, wouldn't calculate_score and roll_dice be way more explanatory as function names?
$endgroup$
– sbecker
Nov 15 '18 at 8:23












$begingroup$
@sbecker You're right, from now on I'll choose better function and variable names.
$endgroup$
– ChubakBidpaa
Nov 15 '18 at 8:46




$begingroup$
@sbecker You're right, from now on I'll choose better function and variable names.
$endgroup$
– ChubakBidpaa
Nov 15 '18 at 8:46




2




2




$begingroup$
It's a bug or a feature? If the we got five 6, which score you expect? 0 or 600? Actually you get 0 but I think 600 seems more logic.
$endgroup$
– Calak
Nov 15 '18 at 13:27




$begingroup$
It's a bug or a feature? If the we got five 6, which score you expect? 0 or 600? Actually you get 0 but I think 600 seems more logic.
$endgroup$
– Calak
Nov 15 '18 at 13:27












$begingroup$
I know this game by another name but I cant recall what it is.
$endgroup$
– Matt
Nov 15 '18 at 18:57




$begingroup$
I know this game by another name but I cant recall what it is.
$endgroup$
– Matt
Nov 15 '18 at 18:57












$begingroup$
@Matt 10000, Greedy, zilch, Tora?
$endgroup$
– Calak
Nov 15 '18 at 21:40




$begingroup$
@Matt 10000, Greedy, zilch, Tora?
$endgroup$
– Calak
Nov 15 '18 at 21:40










6 Answers
6






active

oldest

votes


















17












$begingroup$

Style

You're not being charged by the character; there's no need to abbreviate "count", or "ret" (which I would call "score" instead). Also, main has inconsistent brackets with the rest of the program. Other than that, it looks good.



Globals

The globals are only being used by greed_rand, and would be better created within main and passed as parameters to greed_rand.



Counting Logic

Currently you're checking each possible quantity of '1's (and other numbers) separately, and not in order either, which makes it harder to ensure you're not missing a case and will fail if you change the number of total dice. An improvement would be to check for the larger combinations first and just continue scoring as long as possible. For example:



for(; count[1] >= 3; count[1] -= 3) 
{
score += 1000;
}
for(; count[1] > 0; --count[1])
{
score += 100;
}


This also matches the score chart more clearly.



Output
You're outputting the end result of greed_rand, but not what list generated that result - which means you can't really tell if it was correct. It would probably be better to replace greed_rand with a method that makes a random list, and pass that to greed yourself, so you can also output it:



std::vector<int> roll_dice(int count, std::mt19937 &engine)


Use of Templates (Optional)

While not required for this usage, greed would be a good candidate for taking a pair of generic iterators instead of requiring a std::vector:



template<typename Iterator>
int greed(Iterator begin, Iterator end)
{
...
for (auto it = begin; it != end; ++it)
{
++cnt[*it];
}





share|improve this answer











$endgroup$













  • $begingroup$
    Thank you. I don't know what templates are yet, but once I learn about them (in a few hours) I will make use of them.
    $endgroup$
    – ChubakBidpaa
    Nov 15 '18 at 1:05






  • 1




    $begingroup$
    Note that your double loops for "counting logic" could probably be replaced by: score += 1000 * (count[1] / 3); score += 100 * (count[1] % 3);
    $endgroup$
    – scohe001
    Nov 15 '18 at 17:29








  • 1




    $begingroup$
    @scohe001 if the point bonus for 3 is broken up (100 each plus 700 in bonus points), you can just add 700 instead of 1000, which simplifies score += 100 * count[1] to get the individual rolls. The same logic applies to 5s as well.
    $endgroup$
    – TemporalWolf
    Nov 15 '18 at 23:08






  • 1




    $begingroup$
    regarding the globals: IMHO this is one of the few good circumstances to use statics within the function. Only one initialization on first call, no messy globals, no annoying extra parameters to pass
    $endgroup$
    – thegreatemu
    Nov 15 '18 at 23:16





















9












$begingroup$

Nice separation of functionality, well done!



There are some important details that the other review doesn’t address:



int greed(list_type die_rolls)


Here you are taking a std::vector by value. This means it will ve copied. It’s a small array, it probably doesn’t matter here at all, but you should get used to passing larger objects and objects that own stuff (a vector owns a piece of memory in the heap) by reference. Appropriate would be:



int greed(list_type const& die_rolls)


Next, you use a std::map<int, int> cnt to count die rolls. But you only index it using values 1-6. You should use a std::vector here. You’d waste the first element (index 0), but indexing into a vector is much faster than indexing into a map, and it takes less memory to boot. Or, since you know the size at compile time, you could use a std::array<int,7> instead. This is a fixed-sized array that lives entirely on the stack, it doesn’t allocate heap memory.



Next you do



for (int i = 1; i <= 6; ++i)
{
cnt[i] = 0;
}


Since we’re using a std::vector or std::array now, you can use std::fill:



std::fill(cnt.begin(), cnt.end(), 0);


std::array even has a fill member function:



cnt.fill(0);


However, it is even easier to rely on value initialization:



std::array<int,7> cnt{};


This value-initializes each element of cnt to int{}, meaning each element will have a value of 0.






share|improve this answer











$endgroup$













  • $begingroup$
    I didn't know about fill(). Thank you.
    $endgroup$
    – ChubakBidpaa
    Nov 15 '18 at 3:24










  • $begingroup$
    @CrisLuengo disagree. Why using std::fill (...) when you can just default construct? (std::array <int, 7> counts{};)
    $endgroup$
    – Calak
    Nov 15 '18 at 5:29










  • $begingroup$
    @Calak: Good point! Answer updated.
    $endgroup$
    – Cris Luengo
    Nov 15 '18 at 6:11



















6












$begingroup$

You can easily decouple the scoring-algorithm from the container used to score dice-rolls by using templates. Not that you actually need a container, if you have the right iterators.



Anyway, why aren't you prepared to deal with more dice being thrown?



Also, consider making it data-driven:



template <class InputIt, class Sentinel = InputIt>
auto score(InputIt first, Sentinel last) {
constexpr auto cap = 8;
constexpr unsigned goals[3] = {
{1, 3, 1000},
{6, 3, 600},
{5, 3, 500},
{4, 3, 400},
{3, 3, 300},
{2, 3, 200},
{1, 1, 100},
{5, 1, 50},
};
unsigned char dice[cap] = {};
for (; first != last; ++first)
++dice[(unsigned)*first % cap];
auto result = 0ULL;
for (auto [which, count, reward] : goals) {
auto& x = dice[which % cap];
result += x / count * reward;
x %= count;
}
return result;
}


Global mutable state is best avoided. Why is the random-generator and related things global?



For some reason, you have extra-newlines surrounding the return of your scoring-function. Maybe you should automate indentation?



main() is your only function where the opening brace doesn't have its own line. While there is Nothing wrong with that, try for consistency.



return 0; is implicit for main().






share|improve this answer











$endgroup$













  • $begingroup$
    What's the need for the cap here - it looks like you're taking an input that could be 16k + face, and provisioning for dice with faces up to 16? Perhaps I've just missed a detail.
    $endgroup$
    – cmh
    Nov 15 '18 at 20:04












  • $begingroup$
    @cmh I'm taking 16 because that's the nearest higher power of two.
    $endgroup$
    – Deduplicator
    Nov 15 '18 at 20:48










  • $begingroup$
    I surely missed something. What's the purpose of x %= count; or even... what are you doing with x after this operation? And, are you sure about your computation in result += x / count * reward;? Compare his result and yours if dice are "1, 1, 1, 1, 2". I don't understand, maybe too tired :)
    $endgroup$
    – Calak
    Nov 15 '18 at 20:50










  • $begingroup$
    @Calak Well, x / count is unsigned integer-arithmetic, so rounded down. * reward is obvious. And x %= count; removes the dice just used.
    $endgroup$
    – Deduplicator
    Nov 15 '18 at 20:56










  • $begingroup$
    @Deduplicator I suppose my confusion is that if I pass the sequence 5, 21, 37 it will (in my opinion) incorrectly give the score 500.
    $endgroup$
    – cmh
    Nov 15 '18 at 20:56



















5












$begingroup$

std::random_device seeder;
std::mt19937 engine(seeder());


While this is fine for small hobby projects or testing, you'll likely want to properly seed std::mt19937. Let's break this down into explicit steps to understand what is going on.



std::random_device rdev;     


std::random_device asks the OS for system-provided randomness. It can be, and sometimes is, implemented as a simple PRNG with a fixed seed, meaning you could produce the same sequence on every run. Arguably worse than using time(NULL) as a source for entropy.



auto random_seed{rdev()};


Invoking the random device object returns an unsigned int. This is normally 4 bytes, but it could be 2.



std::seed_seq seeder{random_seed};


A seed sequence is created using that one 2/4-byte value.



std::mt19937 engine(seeder);


You are now attempting to initialize the internal 624 32-bit integer state of the Mersenne Twister with that one value. This leads to issues related to




  • predictability - searching for the seed is simple as there are only 2^32 possibilities.

  • bias - values like 7 and 13 will never be generated. Two different seeds produce 0, 12 different seeds produce 1226181350.


If you are interested the perils/pitfalls of random bit generation and std::seed_seq, read through the comments here.





 typedef std::vector<int> list_type;


If you are expecting a fixed length container, consider using std::array over std::vector.





    std::map<int, int> cnt;


std::map is overkill for counting a contiguous range of values. std::unordered_map is better. An array-based counting sort would be best.



    for (int i = 1; i <= 6; ++i)
{
cnt[i] = 0;
}

for (auto &d : die_rolls)
{
++cnt[d];
}


std::map and std::unordered_map will default construct a value into the container if the key doesn't exist. You can skip the value initialization for these containers.





    if (cnt[1] == 3) { ret += 1000; }
/* ... */


You can do some strength reduction here by reorganizing the related comparisons and converting some of the branches into integer multiplications. Since you limit the number of dice to five, you can immediately stop checking the remaining triples once the first triple has been found.



    if (cnt[1] >= 3) { score += 1000; cnt[1] -= 3; }
else if (cnt[2] >= 3) { score += 200; }
else if (cnt[3] >= 3) { score += 300; }
else if (cnt[4] >= 3) { score += 400; }
else if (cnt[5] >= 3) { score += 500; cnt[5] -= 3; }
else if (cnt[6] >= 3) { score += 600; }

score += cnt[1] * 100 + cnt[5] * 50;





share|improve this answer











$endgroup$













  • $begingroup$
    Very interesting explanation about randomization!
    $endgroup$
    – Calak
    Nov 15 '18 at 12:00






  • 2




    $begingroup$
    You should modify == for >= because if we have more than 3 same values we still have at least 3 values so a combo ;)
    $endgroup$
    – Calak
    Nov 15 '18 at 13:22



















5












$begingroup$

Separation



Good attempt on separation of concerns, but I think you can go further.




  • You can wrap the logic to generate a random integer in a function generate_random_int (int min, int max); where the content might looks like the example provided by Bjarne Stroustrup (look at rand_int) and so, no more globals.

  • You can wrap the logic about filling a container with random numbers in a function template<class ForwardIt> void fill_random(ForwardIt begin, ForwardIt end) (which internally can rely on std::generate and your generate_random_int).


Be explicit



Try to be explicit about "how" and "what"





  • How: Use self-explanary methods and algorithms.


  • What: Uses names that make sense


Parameters type



Try to pass types that are not cheap to copy, by const& (unless you need "destructive" work on it).



Map initialization



You don't need to initialize the map with 0's because when you try accessing to a key that doesn't exist, it is default constructed (so with 0's here).



Choose right types



As stated in other answers since your map's keys is a range of integers, you should use a std::vector or even since you know it size at compile time, std::array that you automatically fill with 0's when you default construct: std::array <int, 7> counts{};. (If you don't want to waste space of the elem at index 0, you have to do some computation later).



Occurrences counting



When you write:



if (some_var == 3) { do_something(); }             //A
if (some_var == 2) { do_something_else(); } //B


If A is true, B can never be true. Instead of re-checking when it's useless, simply use else if :



if (some_var == 3) { do_something(); } 
else if (some_var == 3) { do_something_else(); }


But...



... Instead of testing several times for each dice side possible count, you can reduce branchements checking all "combo" first:



if (counts[1] >= 3) {
result += 1000;
counts[1] -= 3; // here we decrement to remove the wombo combo from the count
}
else if (counts[2] >= 3) {
//...
}
// ...
if (counts[1] > 1) {
result += 100 * counts[1];
}
// ...


Or even, automatically compute combo count



// superbonus
if (counts[1] >= 3) {
result += 1000;
counts [1] -= 3;
}
// combo bonus
else {
for (int index = 2; index < 7; ++index) {
if (counts[index] >= 3) {
result += index * 100;
counts[index] -= 3;
break;
}
}
}
// ...
if (counts[1] > 1) {
result += counts[1] * 100;
}
// ...


Or, maybe more explicit:



// combo bonus
for (int index = 1; index < 7; ++index) {
if (counts[index] >= 3) {
result += index * 100;
counts[index] -= 3;
break;
}
}
if (result == 100) {
result *= 10; // superbonus
}
if (counts[1] > 1) {
result += counts[1] * 100;
}
// ...


Or using the @Snowhawk method, more imperative (and surely more efficient) but less flexible if you want to change your algorithm later.



User-friendly output



Instead of just printing the output, add some information to the user.
It's look nicer if he get as output:




Roll the dice! We got 1, 1, 5, 1, 5, for total of 1100 points




What's next?



Maybe a good challenge is to try implementing the full game based on complete rules? (this or this)






share|improve this answer











$endgroup$





















    2












    $begingroup$

    In addition to a lot of other answers, it's worth noting the bonus logic can be simplified. We don't have to strictly adhere to the written version if we can find a logical equivalent:




    1000 pts for 3 ones



    100 pts for 1 one (not included in the upper rule)




    is equivalent to




    100 pts per one



    700 pts if 3 or more ones




    Which means



    if (cnt[1] == 3)
    {
    ret += 1000;
    }
    if (cnt[1] == 2)
    {
    ret += 200;
    }
    if (cnt[1] == 1)
    {
    ret += 100;
    }
    if (cnt[1] == 4)
    {
    ret += 1100;
    }
    if (cnt[1] == 5)
    {
    ret += 1200;
    }


    can be simplified to



    ret += 100 * cnt[1] + 700 * (cnt[1] / 3)  # Note this will not support more than 5 dice


    Fives can likewise be simplified and for the other numbers, using integer division we can reduce our summation logic from 56 lines to 6, and it should be clearer what is happening to boot:



    # sum score per number count - Note this will not support more than 5 dice
    ret += 700 * (cnt[1] / 3) + 100 * cnt[1]
    ret += 200 * (cnt[2] / 3)
    ret += 300 * (cnt[3] / 3)
    ret += 400 * (cnt[4] / 3)
    ret += 350 * (cnt[5] / 3) + 50 * cnt[5]
    ret += 600 * (cnt[6] / 3)


    Alternatively, we can recognize that the bonus we get is 100 * the die roll for every die except 1, and use that logic:



    ret += 900 * (cnt[1] / 3);  # Adjustment because 1s give 1000 pts, not 100

    # For each value, if 3+ rolled add 100 * value
    for (int i = 1; i <= 6; i++){
    if (cnt[i] / 3){
    ret += 100 * i;
    cnt[i] -= 3;
    }
    }

    # Add individual die score
    ret += cnt[1] * 100;
    ret += cnt[5] * 50;





    share|improve this answer











    $endgroup$













      Your Answer





      StackExchange.ifUsing("editor", function () {
      return StackExchange.using("mathjaxEditing", function () {
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
      });
      });
      }, "mathjax-editing");

      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "196"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207689%2fa-dice-game-called-greed%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      17












      $begingroup$

      Style

      You're not being charged by the character; there's no need to abbreviate "count", or "ret" (which I would call "score" instead). Also, main has inconsistent brackets with the rest of the program. Other than that, it looks good.



      Globals

      The globals are only being used by greed_rand, and would be better created within main and passed as parameters to greed_rand.



      Counting Logic

      Currently you're checking each possible quantity of '1's (and other numbers) separately, and not in order either, which makes it harder to ensure you're not missing a case and will fail if you change the number of total dice. An improvement would be to check for the larger combinations first and just continue scoring as long as possible. For example:



      for(; count[1] >= 3; count[1] -= 3) 
      {
      score += 1000;
      }
      for(; count[1] > 0; --count[1])
      {
      score += 100;
      }


      This also matches the score chart more clearly.



      Output
      You're outputting the end result of greed_rand, but not what list generated that result - which means you can't really tell if it was correct. It would probably be better to replace greed_rand with a method that makes a random list, and pass that to greed yourself, so you can also output it:



      std::vector<int> roll_dice(int count, std::mt19937 &engine)


      Use of Templates (Optional)

      While not required for this usage, greed would be a good candidate for taking a pair of generic iterators instead of requiring a std::vector:



      template<typename Iterator>
      int greed(Iterator begin, Iterator end)
      {
      ...
      for (auto it = begin; it != end; ++it)
      {
      ++cnt[*it];
      }





      share|improve this answer











      $endgroup$













      • $begingroup$
        Thank you. I don't know what templates are yet, but once I learn about them (in a few hours) I will make use of them.
        $endgroup$
        – ChubakBidpaa
        Nov 15 '18 at 1:05






      • 1




        $begingroup$
        Note that your double loops for "counting logic" could probably be replaced by: score += 1000 * (count[1] / 3); score += 100 * (count[1] % 3);
        $endgroup$
        – scohe001
        Nov 15 '18 at 17:29








      • 1




        $begingroup$
        @scohe001 if the point bonus for 3 is broken up (100 each plus 700 in bonus points), you can just add 700 instead of 1000, which simplifies score += 100 * count[1] to get the individual rolls. The same logic applies to 5s as well.
        $endgroup$
        – TemporalWolf
        Nov 15 '18 at 23:08






      • 1




        $begingroup$
        regarding the globals: IMHO this is one of the few good circumstances to use statics within the function. Only one initialization on first call, no messy globals, no annoying extra parameters to pass
        $endgroup$
        – thegreatemu
        Nov 15 '18 at 23:16


















      17












      $begingroup$

      Style

      You're not being charged by the character; there's no need to abbreviate "count", or "ret" (which I would call "score" instead). Also, main has inconsistent brackets with the rest of the program. Other than that, it looks good.



      Globals

      The globals are only being used by greed_rand, and would be better created within main and passed as parameters to greed_rand.



      Counting Logic

      Currently you're checking each possible quantity of '1's (and other numbers) separately, and not in order either, which makes it harder to ensure you're not missing a case and will fail if you change the number of total dice. An improvement would be to check for the larger combinations first and just continue scoring as long as possible. For example:



      for(; count[1] >= 3; count[1] -= 3) 
      {
      score += 1000;
      }
      for(; count[1] > 0; --count[1])
      {
      score += 100;
      }


      This also matches the score chart more clearly.



      Output
      You're outputting the end result of greed_rand, but not what list generated that result - which means you can't really tell if it was correct. It would probably be better to replace greed_rand with a method that makes a random list, and pass that to greed yourself, so you can also output it:



      std::vector<int> roll_dice(int count, std::mt19937 &engine)


      Use of Templates (Optional)

      While not required for this usage, greed would be a good candidate for taking a pair of generic iterators instead of requiring a std::vector:



      template<typename Iterator>
      int greed(Iterator begin, Iterator end)
      {
      ...
      for (auto it = begin; it != end; ++it)
      {
      ++cnt[*it];
      }





      share|improve this answer











      $endgroup$













      • $begingroup$
        Thank you. I don't know what templates are yet, but once I learn about them (in a few hours) I will make use of them.
        $endgroup$
        – ChubakBidpaa
        Nov 15 '18 at 1:05






      • 1




        $begingroup$
        Note that your double loops for "counting logic" could probably be replaced by: score += 1000 * (count[1] / 3); score += 100 * (count[1] % 3);
        $endgroup$
        – scohe001
        Nov 15 '18 at 17:29








      • 1




        $begingroup$
        @scohe001 if the point bonus for 3 is broken up (100 each plus 700 in bonus points), you can just add 700 instead of 1000, which simplifies score += 100 * count[1] to get the individual rolls. The same logic applies to 5s as well.
        $endgroup$
        – TemporalWolf
        Nov 15 '18 at 23:08






      • 1




        $begingroup$
        regarding the globals: IMHO this is one of the few good circumstances to use statics within the function. Only one initialization on first call, no messy globals, no annoying extra parameters to pass
        $endgroup$
        – thegreatemu
        Nov 15 '18 at 23:16
















      17












      17








      17





      $begingroup$

      Style

      You're not being charged by the character; there's no need to abbreviate "count", or "ret" (which I would call "score" instead). Also, main has inconsistent brackets with the rest of the program. Other than that, it looks good.



      Globals

      The globals are only being used by greed_rand, and would be better created within main and passed as parameters to greed_rand.



      Counting Logic

      Currently you're checking each possible quantity of '1's (and other numbers) separately, and not in order either, which makes it harder to ensure you're not missing a case and will fail if you change the number of total dice. An improvement would be to check for the larger combinations first and just continue scoring as long as possible. For example:



      for(; count[1] >= 3; count[1] -= 3) 
      {
      score += 1000;
      }
      for(; count[1] > 0; --count[1])
      {
      score += 100;
      }


      This also matches the score chart more clearly.



      Output
      You're outputting the end result of greed_rand, but not what list generated that result - which means you can't really tell if it was correct. It would probably be better to replace greed_rand with a method that makes a random list, and pass that to greed yourself, so you can also output it:



      std::vector<int> roll_dice(int count, std::mt19937 &engine)


      Use of Templates (Optional)

      While not required for this usage, greed would be a good candidate for taking a pair of generic iterators instead of requiring a std::vector:



      template<typename Iterator>
      int greed(Iterator begin, Iterator end)
      {
      ...
      for (auto it = begin; it != end; ++it)
      {
      ++cnt[*it];
      }





      share|improve this answer











      $endgroup$



      Style

      You're not being charged by the character; there's no need to abbreviate "count", or "ret" (which I would call "score" instead). Also, main has inconsistent brackets with the rest of the program. Other than that, it looks good.



      Globals

      The globals are only being used by greed_rand, and would be better created within main and passed as parameters to greed_rand.



      Counting Logic

      Currently you're checking each possible quantity of '1's (and other numbers) separately, and not in order either, which makes it harder to ensure you're not missing a case and will fail if you change the number of total dice. An improvement would be to check for the larger combinations first and just continue scoring as long as possible. For example:



      for(; count[1] >= 3; count[1] -= 3) 
      {
      score += 1000;
      }
      for(; count[1] > 0; --count[1])
      {
      score += 100;
      }


      This also matches the score chart more clearly.



      Output
      You're outputting the end result of greed_rand, but not what list generated that result - which means you can't really tell if it was correct. It would probably be better to replace greed_rand with a method that makes a random list, and pass that to greed yourself, so you can also output it:



      std::vector<int> roll_dice(int count, std::mt19937 &engine)


      Use of Templates (Optional)

      While not required for this usage, greed would be a good candidate for taking a pair of generic iterators instead of requiring a std::vector:



      template<typename Iterator>
      int greed(Iterator begin, Iterator end)
      {
      ...
      for (auto it = begin; it != end; ++it)
      {
      ++cnt[*it];
      }






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 1:07

























      answered Nov 15 '18 at 1:01









      ErrorsatzErrorsatz

      6837




      6837












      • $begingroup$
        Thank you. I don't know what templates are yet, but once I learn about them (in a few hours) I will make use of them.
        $endgroup$
        – ChubakBidpaa
        Nov 15 '18 at 1:05






      • 1




        $begingroup$
        Note that your double loops for "counting logic" could probably be replaced by: score += 1000 * (count[1] / 3); score += 100 * (count[1] % 3);
        $endgroup$
        – scohe001
        Nov 15 '18 at 17:29








      • 1




        $begingroup$
        @scohe001 if the point bonus for 3 is broken up (100 each plus 700 in bonus points), you can just add 700 instead of 1000, which simplifies score += 100 * count[1] to get the individual rolls. The same logic applies to 5s as well.
        $endgroup$
        – TemporalWolf
        Nov 15 '18 at 23:08






      • 1




        $begingroup$
        regarding the globals: IMHO this is one of the few good circumstances to use statics within the function. Only one initialization on first call, no messy globals, no annoying extra parameters to pass
        $endgroup$
        – thegreatemu
        Nov 15 '18 at 23:16




















      • $begingroup$
        Thank you. I don't know what templates are yet, but once I learn about them (in a few hours) I will make use of them.
        $endgroup$
        – ChubakBidpaa
        Nov 15 '18 at 1:05






      • 1




        $begingroup$
        Note that your double loops for "counting logic" could probably be replaced by: score += 1000 * (count[1] / 3); score += 100 * (count[1] % 3);
        $endgroup$
        – scohe001
        Nov 15 '18 at 17:29








      • 1




        $begingroup$
        @scohe001 if the point bonus for 3 is broken up (100 each plus 700 in bonus points), you can just add 700 instead of 1000, which simplifies score += 100 * count[1] to get the individual rolls. The same logic applies to 5s as well.
        $endgroup$
        – TemporalWolf
        Nov 15 '18 at 23:08






      • 1




        $begingroup$
        regarding the globals: IMHO this is one of the few good circumstances to use statics within the function. Only one initialization on first call, no messy globals, no annoying extra parameters to pass
        $endgroup$
        – thegreatemu
        Nov 15 '18 at 23:16


















      $begingroup$
      Thank you. I don't know what templates are yet, but once I learn about them (in a few hours) I will make use of them.
      $endgroup$
      – ChubakBidpaa
      Nov 15 '18 at 1:05




      $begingroup$
      Thank you. I don't know what templates are yet, but once I learn about them (in a few hours) I will make use of them.
      $endgroup$
      – ChubakBidpaa
      Nov 15 '18 at 1:05




      1




      1




      $begingroup$
      Note that your double loops for "counting logic" could probably be replaced by: score += 1000 * (count[1] / 3); score += 100 * (count[1] % 3);
      $endgroup$
      – scohe001
      Nov 15 '18 at 17:29






      $begingroup$
      Note that your double loops for "counting logic" could probably be replaced by: score += 1000 * (count[1] / 3); score += 100 * (count[1] % 3);
      $endgroup$
      – scohe001
      Nov 15 '18 at 17:29






      1




      1




      $begingroup$
      @scohe001 if the point bonus for 3 is broken up (100 each plus 700 in bonus points), you can just add 700 instead of 1000, which simplifies score += 100 * count[1] to get the individual rolls. The same logic applies to 5s as well.
      $endgroup$
      – TemporalWolf
      Nov 15 '18 at 23:08




      $begingroup$
      @scohe001 if the point bonus for 3 is broken up (100 each plus 700 in bonus points), you can just add 700 instead of 1000, which simplifies score += 100 * count[1] to get the individual rolls. The same logic applies to 5s as well.
      $endgroup$
      – TemporalWolf
      Nov 15 '18 at 23:08




      1




      1




      $begingroup$
      regarding the globals: IMHO this is one of the few good circumstances to use statics within the function. Only one initialization on first call, no messy globals, no annoying extra parameters to pass
      $endgroup$
      – thegreatemu
      Nov 15 '18 at 23:16






      $begingroup$
      regarding the globals: IMHO this is one of the few good circumstances to use statics within the function. Only one initialization on first call, no messy globals, no annoying extra parameters to pass
      $endgroup$
      – thegreatemu
      Nov 15 '18 at 23:16















      9












      $begingroup$

      Nice separation of functionality, well done!



      There are some important details that the other review doesn’t address:



      int greed(list_type die_rolls)


      Here you are taking a std::vector by value. This means it will ve copied. It’s a small array, it probably doesn’t matter here at all, but you should get used to passing larger objects and objects that own stuff (a vector owns a piece of memory in the heap) by reference. Appropriate would be:



      int greed(list_type const& die_rolls)


      Next, you use a std::map<int, int> cnt to count die rolls. But you only index it using values 1-6. You should use a std::vector here. You’d waste the first element (index 0), but indexing into a vector is much faster than indexing into a map, and it takes less memory to boot. Or, since you know the size at compile time, you could use a std::array<int,7> instead. This is a fixed-sized array that lives entirely on the stack, it doesn’t allocate heap memory.



      Next you do



      for (int i = 1; i <= 6; ++i)
      {
      cnt[i] = 0;
      }


      Since we’re using a std::vector or std::array now, you can use std::fill:



      std::fill(cnt.begin(), cnt.end(), 0);


      std::array even has a fill member function:



      cnt.fill(0);


      However, it is even easier to rely on value initialization:



      std::array<int,7> cnt{};


      This value-initializes each element of cnt to int{}, meaning each element will have a value of 0.






      share|improve this answer











      $endgroup$













      • $begingroup$
        I didn't know about fill(). Thank you.
        $endgroup$
        – ChubakBidpaa
        Nov 15 '18 at 3:24










      • $begingroup$
        @CrisLuengo disagree. Why using std::fill (...) when you can just default construct? (std::array <int, 7> counts{};)
        $endgroup$
        – Calak
        Nov 15 '18 at 5:29










      • $begingroup$
        @Calak: Good point! Answer updated.
        $endgroup$
        – Cris Luengo
        Nov 15 '18 at 6:11
















      9












      $begingroup$

      Nice separation of functionality, well done!



      There are some important details that the other review doesn’t address:



      int greed(list_type die_rolls)


      Here you are taking a std::vector by value. This means it will ve copied. It’s a small array, it probably doesn’t matter here at all, but you should get used to passing larger objects and objects that own stuff (a vector owns a piece of memory in the heap) by reference. Appropriate would be:



      int greed(list_type const& die_rolls)


      Next, you use a std::map<int, int> cnt to count die rolls. But you only index it using values 1-6. You should use a std::vector here. You’d waste the first element (index 0), but indexing into a vector is much faster than indexing into a map, and it takes less memory to boot. Or, since you know the size at compile time, you could use a std::array<int,7> instead. This is a fixed-sized array that lives entirely on the stack, it doesn’t allocate heap memory.



      Next you do



      for (int i = 1; i <= 6; ++i)
      {
      cnt[i] = 0;
      }


      Since we’re using a std::vector or std::array now, you can use std::fill:



      std::fill(cnt.begin(), cnt.end(), 0);


      std::array even has a fill member function:



      cnt.fill(0);


      However, it is even easier to rely on value initialization:



      std::array<int,7> cnt{};


      This value-initializes each element of cnt to int{}, meaning each element will have a value of 0.






      share|improve this answer











      $endgroup$













      • $begingroup$
        I didn't know about fill(). Thank you.
        $endgroup$
        – ChubakBidpaa
        Nov 15 '18 at 3:24










      • $begingroup$
        @CrisLuengo disagree. Why using std::fill (...) when you can just default construct? (std::array <int, 7> counts{};)
        $endgroup$
        – Calak
        Nov 15 '18 at 5:29










      • $begingroup$
        @Calak: Good point! Answer updated.
        $endgroup$
        – Cris Luengo
        Nov 15 '18 at 6:11














      9












      9








      9





      $begingroup$

      Nice separation of functionality, well done!



      There are some important details that the other review doesn’t address:



      int greed(list_type die_rolls)


      Here you are taking a std::vector by value. This means it will ve copied. It’s a small array, it probably doesn’t matter here at all, but you should get used to passing larger objects and objects that own stuff (a vector owns a piece of memory in the heap) by reference. Appropriate would be:



      int greed(list_type const& die_rolls)


      Next, you use a std::map<int, int> cnt to count die rolls. But you only index it using values 1-6. You should use a std::vector here. You’d waste the first element (index 0), but indexing into a vector is much faster than indexing into a map, and it takes less memory to boot. Or, since you know the size at compile time, you could use a std::array<int,7> instead. This is a fixed-sized array that lives entirely on the stack, it doesn’t allocate heap memory.



      Next you do



      for (int i = 1; i <= 6; ++i)
      {
      cnt[i] = 0;
      }


      Since we’re using a std::vector or std::array now, you can use std::fill:



      std::fill(cnt.begin(), cnt.end(), 0);


      std::array even has a fill member function:



      cnt.fill(0);


      However, it is even easier to rely on value initialization:



      std::array<int,7> cnt{};


      This value-initializes each element of cnt to int{}, meaning each element will have a value of 0.






      share|improve this answer











      $endgroup$



      Nice separation of functionality, well done!



      There are some important details that the other review doesn’t address:



      int greed(list_type die_rolls)


      Here you are taking a std::vector by value. This means it will ve copied. It’s a small array, it probably doesn’t matter here at all, but you should get used to passing larger objects and objects that own stuff (a vector owns a piece of memory in the heap) by reference. Appropriate would be:



      int greed(list_type const& die_rolls)


      Next, you use a std::map<int, int> cnt to count die rolls. But you only index it using values 1-6. You should use a std::vector here. You’d waste the first element (index 0), but indexing into a vector is much faster than indexing into a map, and it takes less memory to boot. Or, since you know the size at compile time, you could use a std::array<int,7> instead. This is a fixed-sized array that lives entirely on the stack, it doesn’t allocate heap memory.



      Next you do



      for (int i = 1; i <= 6; ++i)
      {
      cnt[i] = 0;
      }


      Since we’re using a std::vector or std::array now, you can use std::fill:



      std::fill(cnt.begin(), cnt.end(), 0);


      std::array even has a fill member function:



      cnt.fill(0);


      However, it is even easier to rely on value initialization:



      std::array<int,7> cnt{};


      This value-initializes each element of cnt to int{}, meaning each element will have a value of 0.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 18 '18 at 8:28









      Pezo

      1155




      1155










      answered Nov 15 '18 at 2:12









      Cris LuengoCris Luengo

      2,489319




      2,489319












      • $begingroup$
        I didn't know about fill(). Thank you.
        $endgroup$
        – ChubakBidpaa
        Nov 15 '18 at 3:24










      • $begingroup$
        @CrisLuengo disagree. Why using std::fill (...) when you can just default construct? (std::array <int, 7> counts{};)
        $endgroup$
        – Calak
        Nov 15 '18 at 5:29










      • $begingroup$
        @Calak: Good point! Answer updated.
        $endgroup$
        – Cris Luengo
        Nov 15 '18 at 6:11


















      • $begingroup$
        I didn't know about fill(). Thank you.
        $endgroup$
        – ChubakBidpaa
        Nov 15 '18 at 3:24










      • $begingroup$
        @CrisLuengo disagree. Why using std::fill (...) when you can just default construct? (std::array <int, 7> counts{};)
        $endgroup$
        – Calak
        Nov 15 '18 at 5:29










      • $begingroup$
        @Calak: Good point! Answer updated.
        $endgroup$
        – Cris Luengo
        Nov 15 '18 at 6:11
















      $begingroup$
      I didn't know about fill(). Thank you.
      $endgroup$
      – ChubakBidpaa
      Nov 15 '18 at 3:24




      $begingroup$
      I didn't know about fill(). Thank you.
      $endgroup$
      – ChubakBidpaa
      Nov 15 '18 at 3:24












      $begingroup$
      @CrisLuengo disagree. Why using std::fill (...) when you can just default construct? (std::array <int, 7> counts{};)
      $endgroup$
      – Calak
      Nov 15 '18 at 5:29




      $begingroup$
      @CrisLuengo disagree. Why using std::fill (...) when you can just default construct? (std::array <int, 7> counts{};)
      $endgroup$
      – Calak
      Nov 15 '18 at 5:29












      $begingroup$
      @Calak: Good point! Answer updated.
      $endgroup$
      – Cris Luengo
      Nov 15 '18 at 6:11




      $begingroup$
      @Calak: Good point! Answer updated.
      $endgroup$
      – Cris Luengo
      Nov 15 '18 at 6:11











      6












      $begingroup$

      You can easily decouple the scoring-algorithm from the container used to score dice-rolls by using templates. Not that you actually need a container, if you have the right iterators.



      Anyway, why aren't you prepared to deal with more dice being thrown?



      Also, consider making it data-driven:



      template <class InputIt, class Sentinel = InputIt>
      auto score(InputIt first, Sentinel last) {
      constexpr auto cap = 8;
      constexpr unsigned goals[3] = {
      {1, 3, 1000},
      {6, 3, 600},
      {5, 3, 500},
      {4, 3, 400},
      {3, 3, 300},
      {2, 3, 200},
      {1, 1, 100},
      {5, 1, 50},
      };
      unsigned char dice[cap] = {};
      for (; first != last; ++first)
      ++dice[(unsigned)*first % cap];
      auto result = 0ULL;
      for (auto [which, count, reward] : goals) {
      auto& x = dice[which % cap];
      result += x / count * reward;
      x %= count;
      }
      return result;
      }


      Global mutable state is best avoided. Why is the random-generator and related things global?



      For some reason, you have extra-newlines surrounding the return of your scoring-function. Maybe you should automate indentation?



      main() is your only function where the opening brace doesn't have its own line. While there is Nothing wrong with that, try for consistency.



      return 0; is implicit for main().






      share|improve this answer











      $endgroup$













      • $begingroup$
        What's the need for the cap here - it looks like you're taking an input that could be 16k + face, and provisioning for dice with faces up to 16? Perhaps I've just missed a detail.
        $endgroup$
        – cmh
        Nov 15 '18 at 20:04












      • $begingroup$
        @cmh I'm taking 16 because that's the nearest higher power of two.
        $endgroup$
        – Deduplicator
        Nov 15 '18 at 20:48










      • $begingroup$
        I surely missed something. What's the purpose of x %= count; or even... what are you doing with x after this operation? And, are you sure about your computation in result += x / count * reward;? Compare his result and yours if dice are "1, 1, 1, 1, 2". I don't understand, maybe too tired :)
        $endgroup$
        – Calak
        Nov 15 '18 at 20:50










      • $begingroup$
        @Calak Well, x / count is unsigned integer-arithmetic, so rounded down. * reward is obvious. And x %= count; removes the dice just used.
        $endgroup$
        – Deduplicator
        Nov 15 '18 at 20:56










      • $begingroup$
        @Deduplicator I suppose my confusion is that if I pass the sequence 5, 21, 37 it will (in my opinion) incorrectly give the score 500.
        $endgroup$
        – cmh
        Nov 15 '18 at 20:56
















      6












      $begingroup$

      You can easily decouple the scoring-algorithm from the container used to score dice-rolls by using templates. Not that you actually need a container, if you have the right iterators.



      Anyway, why aren't you prepared to deal with more dice being thrown?



      Also, consider making it data-driven:



      template <class InputIt, class Sentinel = InputIt>
      auto score(InputIt first, Sentinel last) {
      constexpr auto cap = 8;
      constexpr unsigned goals[3] = {
      {1, 3, 1000},
      {6, 3, 600},
      {5, 3, 500},
      {4, 3, 400},
      {3, 3, 300},
      {2, 3, 200},
      {1, 1, 100},
      {5, 1, 50},
      };
      unsigned char dice[cap] = {};
      for (; first != last; ++first)
      ++dice[(unsigned)*first % cap];
      auto result = 0ULL;
      for (auto [which, count, reward] : goals) {
      auto& x = dice[which % cap];
      result += x / count * reward;
      x %= count;
      }
      return result;
      }


      Global mutable state is best avoided. Why is the random-generator and related things global?



      For some reason, you have extra-newlines surrounding the return of your scoring-function. Maybe you should automate indentation?



      main() is your only function where the opening brace doesn't have its own line. While there is Nothing wrong with that, try for consistency.



      return 0; is implicit for main().






      share|improve this answer











      $endgroup$













      • $begingroup$
        What's the need for the cap here - it looks like you're taking an input that could be 16k + face, and provisioning for dice with faces up to 16? Perhaps I've just missed a detail.
        $endgroup$
        – cmh
        Nov 15 '18 at 20:04












      • $begingroup$
        @cmh I'm taking 16 because that's the nearest higher power of two.
        $endgroup$
        – Deduplicator
        Nov 15 '18 at 20:48










      • $begingroup$
        I surely missed something. What's the purpose of x %= count; or even... what are you doing with x after this operation? And, are you sure about your computation in result += x / count * reward;? Compare his result and yours if dice are "1, 1, 1, 1, 2". I don't understand, maybe too tired :)
        $endgroup$
        – Calak
        Nov 15 '18 at 20:50










      • $begingroup$
        @Calak Well, x / count is unsigned integer-arithmetic, so rounded down. * reward is obvious. And x %= count; removes the dice just used.
        $endgroup$
        – Deduplicator
        Nov 15 '18 at 20:56










      • $begingroup$
        @Deduplicator I suppose my confusion is that if I pass the sequence 5, 21, 37 it will (in my opinion) incorrectly give the score 500.
        $endgroup$
        – cmh
        Nov 15 '18 at 20:56














      6












      6








      6





      $begingroup$

      You can easily decouple the scoring-algorithm from the container used to score dice-rolls by using templates. Not that you actually need a container, if you have the right iterators.



      Anyway, why aren't you prepared to deal with more dice being thrown?



      Also, consider making it data-driven:



      template <class InputIt, class Sentinel = InputIt>
      auto score(InputIt first, Sentinel last) {
      constexpr auto cap = 8;
      constexpr unsigned goals[3] = {
      {1, 3, 1000},
      {6, 3, 600},
      {5, 3, 500},
      {4, 3, 400},
      {3, 3, 300},
      {2, 3, 200},
      {1, 1, 100},
      {5, 1, 50},
      };
      unsigned char dice[cap] = {};
      for (; first != last; ++first)
      ++dice[(unsigned)*first % cap];
      auto result = 0ULL;
      for (auto [which, count, reward] : goals) {
      auto& x = dice[which % cap];
      result += x / count * reward;
      x %= count;
      }
      return result;
      }


      Global mutable state is best avoided. Why is the random-generator and related things global?



      For some reason, you have extra-newlines surrounding the return of your scoring-function. Maybe you should automate indentation?



      main() is your only function where the opening brace doesn't have its own line. While there is Nothing wrong with that, try for consistency.



      return 0; is implicit for main().






      share|improve this answer











      $endgroup$



      You can easily decouple the scoring-algorithm from the container used to score dice-rolls by using templates. Not that you actually need a container, if you have the right iterators.



      Anyway, why aren't you prepared to deal with more dice being thrown?



      Also, consider making it data-driven:



      template <class InputIt, class Sentinel = InputIt>
      auto score(InputIt first, Sentinel last) {
      constexpr auto cap = 8;
      constexpr unsigned goals[3] = {
      {1, 3, 1000},
      {6, 3, 600},
      {5, 3, 500},
      {4, 3, 400},
      {3, 3, 300},
      {2, 3, 200},
      {1, 1, 100},
      {5, 1, 50},
      };
      unsigned char dice[cap] = {};
      for (; first != last; ++first)
      ++dice[(unsigned)*first % cap];
      auto result = 0ULL;
      for (auto [which, count, reward] : goals) {
      auto& x = dice[which % cap];
      result += x / count * reward;
      x %= count;
      }
      return result;
      }


      Global mutable state is best avoided. Why is the random-generator and related things global?



      For some reason, you have extra-newlines surrounding the return of your scoring-function. Maybe you should automate indentation?



      main() is your only function where the opening brace doesn't have its own line. While there is Nothing wrong with that, try for consistency.



      return 0; is implicit for main().







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 20:58

























      answered Nov 15 '18 at 17:23









      DeduplicatorDeduplicator

      11.2k1850




      11.2k1850












      • $begingroup$
        What's the need for the cap here - it looks like you're taking an input that could be 16k + face, and provisioning for dice with faces up to 16? Perhaps I've just missed a detail.
        $endgroup$
        – cmh
        Nov 15 '18 at 20:04












      • $begingroup$
        @cmh I'm taking 16 because that's the nearest higher power of two.
        $endgroup$
        – Deduplicator
        Nov 15 '18 at 20:48










      • $begingroup$
        I surely missed something. What's the purpose of x %= count; or even... what are you doing with x after this operation? And, are you sure about your computation in result += x / count * reward;? Compare his result and yours if dice are "1, 1, 1, 1, 2". I don't understand, maybe too tired :)
        $endgroup$
        – Calak
        Nov 15 '18 at 20:50










      • $begingroup$
        @Calak Well, x / count is unsigned integer-arithmetic, so rounded down. * reward is obvious. And x %= count; removes the dice just used.
        $endgroup$
        – Deduplicator
        Nov 15 '18 at 20:56










      • $begingroup$
        @Deduplicator I suppose my confusion is that if I pass the sequence 5, 21, 37 it will (in my opinion) incorrectly give the score 500.
        $endgroup$
        – cmh
        Nov 15 '18 at 20:56


















      • $begingroup$
        What's the need for the cap here - it looks like you're taking an input that could be 16k + face, and provisioning for dice with faces up to 16? Perhaps I've just missed a detail.
        $endgroup$
        – cmh
        Nov 15 '18 at 20:04












      • $begingroup$
        @cmh I'm taking 16 because that's the nearest higher power of two.
        $endgroup$
        – Deduplicator
        Nov 15 '18 at 20:48










      • $begingroup$
        I surely missed something. What's the purpose of x %= count; or even... what are you doing with x after this operation? And, are you sure about your computation in result += x / count * reward;? Compare his result and yours if dice are "1, 1, 1, 1, 2". I don't understand, maybe too tired :)
        $endgroup$
        – Calak
        Nov 15 '18 at 20:50










      • $begingroup$
        @Calak Well, x / count is unsigned integer-arithmetic, so rounded down. * reward is obvious. And x %= count; removes the dice just used.
        $endgroup$
        – Deduplicator
        Nov 15 '18 at 20:56










      • $begingroup$
        @Deduplicator I suppose my confusion is that if I pass the sequence 5, 21, 37 it will (in my opinion) incorrectly give the score 500.
        $endgroup$
        – cmh
        Nov 15 '18 at 20:56
















      $begingroup$
      What's the need for the cap here - it looks like you're taking an input that could be 16k + face, and provisioning for dice with faces up to 16? Perhaps I've just missed a detail.
      $endgroup$
      – cmh
      Nov 15 '18 at 20:04






      $begingroup$
      What's the need for the cap here - it looks like you're taking an input that could be 16k + face, and provisioning for dice with faces up to 16? Perhaps I've just missed a detail.
      $endgroup$
      – cmh
      Nov 15 '18 at 20:04














      $begingroup$
      @cmh I'm taking 16 because that's the nearest higher power of two.
      $endgroup$
      – Deduplicator
      Nov 15 '18 at 20:48




      $begingroup$
      @cmh I'm taking 16 because that's the nearest higher power of two.
      $endgroup$
      – Deduplicator
      Nov 15 '18 at 20:48












      $begingroup$
      I surely missed something. What's the purpose of x %= count; or even... what are you doing with x after this operation? And, are you sure about your computation in result += x / count * reward;? Compare his result and yours if dice are "1, 1, 1, 1, 2". I don't understand, maybe too tired :)
      $endgroup$
      – Calak
      Nov 15 '18 at 20:50




      $begingroup$
      I surely missed something. What's the purpose of x %= count; or even... what are you doing with x after this operation? And, are you sure about your computation in result += x / count * reward;? Compare his result and yours if dice are "1, 1, 1, 1, 2". I don't understand, maybe too tired :)
      $endgroup$
      – Calak
      Nov 15 '18 at 20:50












      $begingroup$
      @Calak Well, x / count is unsigned integer-arithmetic, so rounded down. * reward is obvious. And x %= count; removes the dice just used.
      $endgroup$
      – Deduplicator
      Nov 15 '18 at 20:56




      $begingroup$
      @Calak Well, x / count is unsigned integer-arithmetic, so rounded down. * reward is obvious. And x %= count; removes the dice just used.
      $endgroup$
      – Deduplicator
      Nov 15 '18 at 20:56












      $begingroup$
      @Deduplicator I suppose my confusion is that if I pass the sequence 5, 21, 37 it will (in my opinion) incorrectly give the score 500.
      $endgroup$
      – cmh
      Nov 15 '18 at 20:56




      $begingroup$
      @Deduplicator I suppose my confusion is that if I pass the sequence 5, 21, 37 it will (in my opinion) incorrectly give the score 500.
      $endgroup$
      – cmh
      Nov 15 '18 at 20:56











      5












      $begingroup$

      std::random_device seeder;
      std::mt19937 engine(seeder());


      While this is fine for small hobby projects or testing, you'll likely want to properly seed std::mt19937. Let's break this down into explicit steps to understand what is going on.



      std::random_device rdev;     


      std::random_device asks the OS for system-provided randomness. It can be, and sometimes is, implemented as a simple PRNG with a fixed seed, meaning you could produce the same sequence on every run. Arguably worse than using time(NULL) as a source for entropy.



      auto random_seed{rdev()};


      Invoking the random device object returns an unsigned int. This is normally 4 bytes, but it could be 2.



      std::seed_seq seeder{random_seed};


      A seed sequence is created using that one 2/4-byte value.



      std::mt19937 engine(seeder);


      You are now attempting to initialize the internal 624 32-bit integer state of the Mersenne Twister with that one value. This leads to issues related to




      • predictability - searching for the seed is simple as there are only 2^32 possibilities.

      • bias - values like 7 and 13 will never be generated. Two different seeds produce 0, 12 different seeds produce 1226181350.


      If you are interested the perils/pitfalls of random bit generation and std::seed_seq, read through the comments here.





       typedef std::vector<int> list_type;


      If you are expecting a fixed length container, consider using std::array over std::vector.





          std::map<int, int> cnt;


      std::map is overkill for counting a contiguous range of values. std::unordered_map is better. An array-based counting sort would be best.



          for (int i = 1; i <= 6; ++i)
      {
      cnt[i] = 0;
      }

      for (auto &d : die_rolls)
      {
      ++cnt[d];
      }


      std::map and std::unordered_map will default construct a value into the container if the key doesn't exist. You can skip the value initialization for these containers.





          if (cnt[1] == 3) { ret += 1000; }
      /* ... */


      You can do some strength reduction here by reorganizing the related comparisons and converting some of the branches into integer multiplications. Since you limit the number of dice to five, you can immediately stop checking the remaining triples once the first triple has been found.



          if (cnt[1] >= 3) { score += 1000; cnt[1] -= 3; }
      else if (cnt[2] >= 3) { score += 200; }
      else if (cnt[3] >= 3) { score += 300; }
      else if (cnt[4] >= 3) { score += 400; }
      else if (cnt[5] >= 3) { score += 500; cnt[5] -= 3; }
      else if (cnt[6] >= 3) { score += 600; }

      score += cnt[1] * 100 + cnt[5] * 50;





      share|improve this answer











      $endgroup$













      • $begingroup$
        Very interesting explanation about randomization!
        $endgroup$
        – Calak
        Nov 15 '18 at 12:00






      • 2




        $begingroup$
        You should modify == for >= because if we have more than 3 same values we still have at least 3 values so a combo ;)
        $endgroup$
        – Calak
        Nov 15 '18 at 13:22
















      5












      $begingroup$

      std::random_device seeder;
      std::mt19937 engine(seeder());


      While this is fine for small hobby projects or testing, you'll likely want to properly seed std::mt19937. Let's break this down into explicit steps to understand what is going on.



      std::random_device rdev;     


      std::random_device asks the OS for system-provided randomness. It can be, and sometimes is, implemented as a simple PRNG with a fixed seed, meaning you could produce the same sequence on every run. Arguably worse than using time(NULL) as a source for entropy.



      auto random_seed{rdev()};


      Invoking the random device object returns an unsigned int. This is normally 4 bytes, but it could be 2.



      std::seed_seq seeder{random_seed};


      A seed sequence is created using that one 2/4-byte value.



      std::mt19937 engine(seeder);


      You are now attempting to initialize the internal 624 32-bit integer state of the Mersenne Twister with that one value. This leads to issues related to




      • predictability - searching for the seed is simple as there are only 2^32 possibilities.

      • bias - values like 7 and 13 will never be generated. Two different seeds produce 0, 12 different seeds produce 1226181350.


      If you are interested the perils/pitfalls of random bit generation and std::seed_seq, read through the comments here.





       typedef std::vector<int> list_type;


      If you are expecting a fixed length container, consider using std::array over std::vector.





          std::map<int, int> cnt;


      std::map is overkill for counting a contiguous range of values. std::unordered_map is better. An array-based counting sort would be best.



          for (int i = 1; i <= 6; ++i)
      {
      cnt[i] = 0;
      }

      for (auto &d : die_rolls)
      {
      ++cnt[d];
      }


      std::map and std::unordered_map will default construct a value into the container if the key doesn't exist. You can skip the value initialization for these containers.





          if (cnt[1] == 3) { ret += 1000; }
      /* ... */


      You can do some strength reduction here by reorganizing the related comparisons and converting some of the branches into integer multiplications. Since you limit the number of dice to five, you can immediately stop checking the remaining triples once the first triple has been found.



          if (cnt[1] >= 3) { score += 1000; cnt[1] -= 3; }
      else if (cnt[2] >= 3) { score += 200; }
      else if (cnt[3] >= 3) { score += 300; }
      else if (cnt[4] >= 3) { score += 400; }
      else if (cnt[5] >= 3) { score += 500; cnt[5] -= 3; }
      else if (cnt[6] >= 3) { score += 600; }

      score += cnt[1] * 100 + cnt[5] * 50;





      share|improve this answer











      $endgroup$













      • $begingroup$
        Very interesting explanation about randomization!
        $endgroup$
        – Calak
        Nov 15 '18 at 12:00






      • 2




        $begingroup$
        You should modify == for >= because if we have more than 3 same values we still have at least 3 values so a combo ;)
        $endgroup$
        – Calak
        Nov 15 '18 at 13:22














      5












      5








      5





      $begingroup$

      std::random_device seeder;
      std::mt19937 engine(seeder());


      While this is fine for small hobby projects or testing, you'll likely want to properly seed std::mt19937. Let's break this down into explicit steps to understand what is going on.



      std::random_device rdev;     


      std::random_device asks the OS for system-provided randomness. It can be, and sometimes is, implemented as a simple PRNG with a fixed seed, meaning you could produce the same sequence on every run. Arguably worse than using time(NULL) as a source for entropy.



      auto random_seed{rdev()};


      Invoking the random device object returns an unsigned int. This is normally 4 bytes, but it could be 2.



      std::seed_seq seeder{random_seed};


      A seed sequence is created using that one 2/4-byte value.



      std::mt19937 engine(seeder);


      You are now attempting to initialize the internal 624 32-bit integer state of the Mersenne Twister with that one value. This leads to issues related to




      • predictability - searching for the seed is simple as there are only 2^32 possibilities.

      • bias - values like 7 and 13 will never be generated. Two different seeds produce 0, 12 different seeds produce 1226181350.


      If you are interested the perils/pitfalls of random bit generation and std::seed_seq, read through the comments here.





       typedef std::vector<int> list_type;


      If you are expecting a fixed length container, consider using std::array over std::vector.





          std::map<int, int> cnt;


      std::map is overkill for counting a contiguous range of values. std::unordered_map is better. An array-based counting sort would be best.



          for (int i = 1; i <= 6; ++i)
      {
      cnt[i] = 0;
      }

      for (auto &d : die_rolls)
      {
      ++cnt[d];
      }


      std::map and std::unordered_map will default construct a value into the container if the key doesn't exist. You can skip the value initialization for these containers.





          if (cnt[1] == 3) { ret += 1000; }
      /* ... */


      You can do some strength reduction here by reorganizing the related comparisons and converting some of the branches into integer multiplications. Since you limit the number of dice to five, you can immediately stop checking the remaining triples once the first triple has been found.



          if (cnt[1] >= 3) { score += 1000; cnt[1] -= 3; }
      else if (cnt[2] >= 3) { score += 200; }
      else if (cnt[3] >= 3) { score += 300; }
      else if (cnt[4] >= 3) { score += 400; }
      else if (cnt[5] >= 3) { score += 500; cnt[5] -= 3; }
      else if (cnt[6] >= 3) { score += 600; }

      score += cnt[1] * 100 + cnt[5] * 50;





      share|improve this answer











      $endgroup$



      std::random_device seeder;
      std::mt19937 engine(seeder());


      While this is fine for small hobby projects or testing, you'll likely want to properly seed std::mt19937. Let's break this down into explicit steps to understand what is going on.



      std::random_device rdev;     


      std::random_device asks the OS for system-provided randomness. It can be, and sometimes is, implemented as a simple PRNG with a fixed seed, meaning you could produce the same sequence on every run. Arguably worse than using time(NULL) as a source for entropy.



      auto random_seed{rdev()};


      Invoking the random device object returns an unsigned int. This is normally 4 bytes, but it could be 2.



      std::seed_seq seeder{random_seed};


      A seed sequence is created using that one 2/4-byte value.



      std::mt19937 engine(seeder);


      You are now attempting to initialize the internal 624 32-bit integer state of the Mersenne Twister with that one value. This leads to issues related to




      • predictability - searching for the seed is simple as there are only 2^32 possibilities.

      • bias - values like 7 and 13 will never be generated. Two different seeds produce 0, 12 different seeds produce 1226181350.


      If you are interested the perils/pitfalls of random bit generation and std::seed_seq, read through the comments here.





       typedef std::vector<int> list_type;


      If you are expecting a fixed length container, consider using std::array over std::vector.





          std::map<int, int> cnt;


      std::map is overkill for counting a contiguous range of values. std::unordered_map is better. An array-based counting sort would be best.



          for (int i = 1; i <= 6; ++i)
      {
      cnt[i] = 0;
      }

      for (auto &d : die_rolls)
      {
      ++cnt[d];
      }


      std::map and std::unordered_map will default construct a value into the container if the key doesn't exist. You can skip the value initialization for these containers.





          if (cnt[1] == 3) { ret += 1000; }
      /* ... */


      You can do some strength reduction here by reorganizing the related comparisons and converting some of the branches into integer multiplications. Since you limit the number of dice to five, you can immediately stop checking the remaining triples once the first triple has been found.



          if (cnt[1] >= 3) { score += 1000; cnt[1] -= 3; }
      else if (cnt[2] >= 3) { score += 200; }
      else if (cnt[3] >= 3) { score += 300; }
      else if (cnt[4] >= 3) { score += 400; }
      else if (cnt[5] >= 3) { score += 500; cnt[5] -= 3; }
      else if (cnt[6] >= 3) { score += 600; }

      score += cnt[1] * 100 + cnt[5] * 50;






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 15 '18 at 13:56









      Calak

      2,102318




      2,102318










      answered Nov 15 '18 at 10:04









      SnowhawkSnowhawk

      5,15911028




      5,15911028












      • $begingroup$
        Very interesting explanation about randomization!
        $endgroup$
        – Calak
        Nov 15 '18 at 12:00






      • 2




        $begingroup$
        You should modify == for >= because if we have more than 3 same values we still have at least 3 values so a combo ;)
        $endgroup$
        – Calak
        Nov 15 '18 at 13:22


















      • $begingroup$
        Very interesting explanation about randomization!
        $endgroup$
        – Calak
        Nov 15 '18 at 12:00






      • 2




        $begingroup$
        You should modify == for >= because if we have more than 3 same values we still have at least 3 values so a combo ;)
        $endgroup$
        – Calak
        Nov 15 '18 at 13:22
















      $begingroup$
      Very interesting explanation about randomization!
      $endgroup$
      – Calak
      Nov 15 '18 at 12:00




      $begingroup$
      Very interesting explanation about randomization!
      $endgroup$
      – Calak
      Nov 15 '18 at 12:00




      2




      2




      $begingroup$
      You should modify == for >= because if we have more than 3 same values we still have at least 3 values so a combo ;)
      $endgroup$
      – Calak
      Nov 15 '18 at 13:22




      $begingroup$
      You should modify == for >= because if we have more than 3 same values we still have at least 3 values so a combo ;)
      $endgroup$
      – Calak
      Nov 15 '18 at 13:22











      5












      $begingroup$

      Separation



      Good attempt on separation of concerns, but I think you can go further.




      • You can wrap the logic to generate a random integer in a function generate_random_int (int min, int max); where the content might looks like the example provided by Bjarne Stroustrup (look at rand_int) and so, no more globals.

      • You can wrap the logic about filling a container with random numbers in a function template<class ForwardIt> void fill_random(ForwardIt begin, ForwardIt end) (which internally can rely on std::generate and your generate_random_int).


      Be explicit



      Try to be explicit about "how" and "what"





      • How: Use self-explanary methods and algorithms.


      • What: Uses names that make sense


      Parameters type



      Try to pass types that are not cheap to copy, by const& (unless you need "destructive" work on it).



      Map initialization



      You don't need to initialize the map with 0's because when you try accessing to a key that doesn't exist, it is default constructed (so with 0's here).



      Choose right types



      As stated in other answers since your map's keys is a range of integers, you should use a std::vector or even since you know it size at compile time, std::array that you automatically fill with 0's when you default construct: std::array <int, 7> counts{};. (If you don't want to waste space of the elem at index 0, you have to do some computation later).



      Occurrences counting



      When you write:



      if (some_var == 3) { do_something(); }             //A
      if (some_var == 2) { do_something_else(); } //B


      If A is true, B can never be true. Instead of re-checking when it's useless, simply use else if :



      if (some_var == 3) { do_something(); } 
      else if (some_var == 3) { do_something_else(); }


      But...



      ... Instead of testing several times for each dice side possible count, you can reduce branchements checking all "combo" first:



      if (counts[1] >= 3) {
      result += 1000;
      counts[1] -= 3; // here we decrement to remove the wombo combo from the count
      }
      else if (counts[2] >= 3) {
      //...
      }
      // ...
      if (counts[1] > 1) {
      result += 100 * counts[1];
      }
      // ...


      Or even, automatically compute combo count



      // superbonus
      if (counts[1] >= 3) {
      result += 1000;
      counts [1] -= 3;
      }
      // combo bonus
      else {
      for (int index = 2; index < 7; ++index) {
      if (counts[index] >= 3) {
      result += index * 100;
      counts[index] -= 3;
      break;
      }
      }
      }
      // ...
      if (counts[1] > 1) {
      result += counts[1] * 100;
      }
      // ...


      Or, maybe more explicit:



      // combo bonus
      for (int index = 1; index < 7; ++index) {
      if (counts[index] >= 3) {
      result += index * 100;
      counts[index] -= 3;
      break;
      }
      }
      if (result == 100) {
      result *= 10; // superbonus
      }
      if (counts[1] > 1) {
      result += counts[1] * 100;
      }
      // ...


      Or using the @Snowhawk method, more imperative (and surely more efficient) but less flexible if you want to change your algorithm later.



      User-friendly output



      Instead of just printing the output, add some information to the user.
      It's look nicer if he get as output:




      Roll the dice! We got 1, 1, 5, 1, 5, for total of 1100 points




      What's next?



      Maybe a good challenge is to try implementing the full game based on complete rules? (this or this)






      share|improve this answer











      $endgroup$


















        5












        $begingroup$

        Separation



        Good attempt on separation of concerns, but I think you can go further.




        • You can wrap the logic to generate a random integer in a function generate_random_int (int min, int max); where the content might looks like the example provided by Bjarne Stroustrup (look at rand_int) and so, no more globals.

        • You can wrap the logic about filling a container with random numbers in a function template<class ForwardIt> void fill_random(ForwardIt begin, ForwardIt end) (which internally can rely on std::generate and your generate_random_int).


        Be explicit



        Try to be explicit about "how" and "what"





        • How: Use self-explanary methods and algorithms.


        • What: Uses names that make sense


        Parameters type



        Try to pass types that are not cheap to copy, by const& (unless you need "destructive" work on it).



        Map initialization



        You don't need to initialize the map with 0's because when you try accessing to a key that doesn't exist, it is default constructed (so with 0's here).



        Choose right types



        As stated in other answers since your map's keys is a range of integers, you should use a std::vector or even since you know it size at compile time, std::array that you automatically fill with 0's when you default construct: std::array <int, 7> counts{};. (If you don't want to waste space of the elem at index 0, you have to do some computation later).



        Occurrences counting



        When you write:



        if (some_var == 3) { do_something(); }             //A
        if (some_var == 2) { do_something_else(); } //B


        If A is true, B can never be true. Instead of re-checking when it's useless, simply use else if :



        if (some_var == 3) { do_something(); } 
        else if (some_var == 3) { do_something_else(); }


        But...



        ... Instead of testing several times for each dice side possible count, you can reduce branchements checking all "combo" first:



        if (counts[1] >= 3) {
        result += 1000;
        counts[1] -= 3; // here we decrement to remove the wombo combo from the count
        }
        else if (counts[2] >= 3) {
        //...
        }
        // ...
        if (counts[1] > 1) {
        result += 100 * counts[1];
        }
        // ...


        Or even, automatically compute combo count



        // superbonus
        if (counts[1] >= 3) {
        result += 1000;
        counts [1] -= 3;
        }
        // combo bonus
        else {
        for (int index = 2; index < 7; ++index) {
        if (counts[index] >= 3) {
        result += index * 100;
        counts[index] -= 3;
        break;
        }
        }
        }
        // ...
        if (counts[1] > 1) {
        result += counts[1] * 100;
        }
        // ...


        Or, maybe more explicit:



        // combo bonus
        for (int index = 1; index < 7; ++index) {
        if (counts[index] >= 3) {
        result += index * 100;
        counts[index] -= 3;
        break;
        }
        }
        if (result == 100) {
        result *= 10; // superbonus
        }
        if (counts[1] > 1) {
        result += counts[1] * 100;
        }
        // ...


        Or using the @Snowhawk method, more imperative (and surely more efficient) but less flexible if you want to change your algorithm later.



        User-friendly output



        Instead of just printing the output, add some information to the user.
        It's look nicer if he get as output:




        Roll the dice! We got 1, 1, 5, 1, 5, for total of 1100 points




        What's next?



        Maybe a good challenge is to try implementing the full game based on complete rules? (this or this)






        share|improve this answer











        $endgroup$
















          5












          5








          5





          $begingroup$

          Separation



          Good attempt on separation of concerns, but I think you can go further.




          • You can wrap the logic to generate a random integer in a function generate_random_int (int min, int max); where the content might looks like the example provided by Bjarne Stroustrup (look at rand_int) and so, no more globals.

          • You can wrap the logic about filling a container with random numbers in a function template<class ForwardIt> void fill_random(ForwardIt begin, ForwardIt end) (which internally can rely on std::generate and your generate_random_int).


          Be explicit



          Try to be explicit about "how" and "what"





          • How: Use self-explanary methods and algorithms.


          • What: Uses names that make sense


          Parameters type



          Try to pass types that are not cheap to copy, by const& (unless you need "destructive" work on it).



          Map initialization



          You don't need to initialize the map with 0's because when you try accessing to a key that doesn't exist, it is default constructed (so with 0's here).



          Choose right types



          As stated in other answers since your map's keys is a range of integers, you should use a std::vector or even since you know it size at compile time, std::array that you automatically fill with 0's when you default construct: std::array <int, 7> counts{};. (If you don't want to waste space of the elem at index 0, you have to do some computation later).



          Occurrences counting



          When you write:



          if (some_var == 3) { do_something(); }             //A
          if (some_var == 2) { do_something_else(); } //B


          If A is true, B can never be true. Instead of re-checking when it's useless, simply use else if :



          if (some_var == 3) { do_something(); } 
          else if (some_var == 3) { do_something_else(); }


          But...



          ... Instead of testing several times for each dice side possible count, you can reduce branchements checking all "combo" first:



          if (counts[1] >= 3) {
          result += 1000;
          counts[1] -= 3; // here we decrement to remove the wombo combo from the count
          }
          else if (counts[2] >= 3) {
          //...
          }
          // ...
          if (counts[1] > 1) {
          result += 100 * counts[1];
          }
          // ...


          Or even, automatically compute combo count



          // superbonus
          if (counts[1] >= 3) {
          result += 1000;
          counts [1] -= 3;
          }
          // combo bonus
          else {
          for (int index = 2; index < 7; ++index) {
          if (counts[index] >= 3) {
          result += index * 100;
          counts[index] -= 3;
          break;
          }
          }
          }
          // ...
          if (counts[1] > 1) {
          result += counts[1] * 100;
          }
          // ...


          Or, maybe more explicit:



          // combo bonus
          for (int index = 1; index < 7; ++index) {
          if (counts[index] >= 3) {
          result += index * 100;
          counts[index] -= 3;
          break;
          }
          }
          if (result == 100) {
          result *= 10; // superbonus
          }
          if (counts[1] > 1) {
          result += counts[1] * 100;
          }
          // ...


          Or using the @Snowhawk method, more imperative (and surely more efficient) but less flexible if you want to change your algorithm later.



          User-friendly output



          Instead of just printing the output, add some information to the user.
          It's look nicer if he get as output:




          Roll the dice! We got 1, 1, 5, 1, 5, for total of 1100 points




          What's next?



          Maybe a good challenge is to try implementing the full game based on complete rules? (this or this)






          share|improve this answer











          $endgroup$



          Separation



          Good attempt on separation of concerns, but I think you can go further.




          • You can wrap the logic to generate a random integer in a function generate_random_int (int min, int max); where the content might looks like the example provided by Bjarne Stroustrup (look at rand_int) and so, no more globals.

          • You can wrap the logic about filling a container with random numbers in a function template<class ForwardIt> void fill_random(ForwardIt begin, ForwardIt end) (which internally can rely on std::generate and your generate_random_int).


          Be explicit



          Try to be explicit about "how" and "what"





          • How: Use self-explanary methods and algorithms.


          • What: Uses names that make sense


          Parameters type



          Try to pass types that are not cheap to copy, by const& (unless you need "destructive" work on it).



          Map initialization



          You don't need to initialize the map with 0's because when you try accessing to a key that doesn't exist, it is default constructed (so with 0's here).



          Choose right types



          As stated in other answers since your map's keys is a range of integers, you should use a std::vector or even since you know it size at compile time, std::array that you automatically fill with 0's when you default construct: std::array <int, 7> counts{};. (If you don't want to waste space of the elem at index 0, you have to do some computation later).



          Occurrences counting



          When you write:



          if (some_var == 3) { do_something(); }             //A
          if (some_var == 2) { do_something_else(); } //B


          If A is true, B can never be true. Instead of re-checking when it's useless, simply use else if :



          if (some_var == 3) { do_something(); } 
          else if (some_var == 3) { do_something_else(); }


          But...



          ... Instead of testing several times for each dice side possible count, you can reduce branchements checking all "combo" first:



          if (counts[1] >= 3) {
          result += 1000;
          counts[1] -= 3; // here we decrement to remove the wombo combo from the count
          }
          else if (counts[2] >= 3) {
          //...
          }
          // ...
          if (counts[1] > 1) {
          result += 100 * counts[1];
          }
          // ...


          Or even, automatically compute combo count



          // superbonus
          if (counts[1] >= 3) {
          result += 1000;
          counts [1] -= 3;
          }
          // combo bonus
          else {
          for (int index = 2; index < 7; ++index) {
          if (counts[index] >= 3) {
          result += index * 100;
          counts[index] -= 3;
          break;
          }
          }
          }
          // ...
          if (counts[1] > 1) {
          result += counts[1] * 100;
          }
          // ...


          Or, maybe more explicit:



          // combo bonus
          for (int index = 1; index < 7; ++index) {
          if (counts[index] >= 3) {
          result += index * 100;
          counts[index] -= 3;
          break;
          }
          }
          if (result == 100) {
          result *= 10; // superbonus
          }
          if (counts[1] > 1) {
          result += counts[1] * 100;
          }
          // ...


          Or using the @Snowhawk method, more imperative (and surely more efficient) but less flexible if you want to change your algorithm later.



          User-friendly output



          Instead of just printing the output, add some information to the user.
          It's look nicer if he get as output:




          Roll the dice! We got 1, 1, 5, 1, 5, for total of 1100 points




          What's next?



          Maybe a good challenge is to try implementing the full game based on complete rules? (this or this)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 14:08

























          answered Nov 15 '18 at 11:45









          CalakCalak

          2,102318




          2,102318























              2












              $begingroup$

              In addition to a lot of other answers, it's worth noting the bonus logic can be simplified. We don't have to strictly adhere to the written version if we can find a logical equivalent:




              1000 pts for 3 ones



              100 pts for 1 one (not included in the upper rule)




              is equivalent to




              100 pts per one



              700 pts if 3 or more ones




              Which means



              if (cnt[1] == 3)
              {
              ret += 1000;
              }
              if (cnt[1] == 2)
              {
              ret += 200;
              }
              if (cnt[1] == 1)
              {
              ret += 100;
              }
              if (cnt[1] == 4)
              {
              ret += 1100;
              }
              if (cnt[1] == 5)
              {
              ret += 1200;
              }


              can be simplified to



              ret += 100 * cnt[1] + 700 * (cnt[1] / 3)  # Note this will not support more than 5 dice


              Fives can likewise be simplified and for the other numbers, using integer division we can reduce our summation logic from 56 lines to 6, and it should be clearer what is happening to boot:



              # sum score per number count - Note this will not support more than 5 dice
              ret += 700 * (cnt[1] / 3) + 100 * cnt[1]
              ret += 200 * (cnt[2] / 3)
              ret += 300 * (cnt[3] / 3)
              ret += 400 * (cnt[4] / 3)
              ret += 350 * (cnt[5] / 3) + 50 * cnt[5]
              ret += 600 * (cnt[6] / 3)


              Alternatively, we can recognize that the bonus we get is 100 * the die roll for every die except 1, and use that logic:



              ret += 900 * (cnt[1] / 3);  # Adjustment because 1s give 1000 pts, not 100

              # For each value, if 3+ rolled add 100 * value
              for (int i = 1; i <= 6; i++){
              if (cnt[i] / 3){
              ret += 100 * i;
              cnt[i] -= 3;
              }
              }

              # Add individual die score
              ret += cnt[1] * 100;
              ret += cnt[5] * 50;





              share|improve this answer











              $endgroup$


















                2












                $begingroup$

                In addition to a lot of other answers, it's worth noting the bonus logic can be simplified. We don't have to strictly adhere to the written version if we can find a logical equivalent:




                1000 pts for 3 ones



                100 pts for 1 one (not included in the upper rule)




                is equivalent to




                100 pts per one



                700 pts if 3 or more ones




                Which means



                if (cnt[1] == 3)
                {
                ret += 1000;
                }
                if (cnt[1] == 2)
                {
                ret += 200;
                }
                if (cnt[1] == 1)
                {
                ret += 100;
                }
                if (cnt[1] == 4)
                {
                ret += 1100;
                }
                if (cnt[1] == 5)
                {
                ret += 1200;
                }


                can be simplified to



                ret += 100 * cnt[1] + 700 * (cnt[1] / 3)  # Note this will not support more than 5 dice


                Fives can likewise be simplified and for the other numbers, using integer division we can reduce our summation logic from 56 lines to 6, and it should be clearer what is happening to boot:



                # sum score per number count - Note this will not support more than 5 dice
                ret += 700 * (cnt[1] / 3) + 100 * cnt[1]
                ret += 200 * (cnt[2] / 3)
                ret += 300 * (cnt[3] / 3)
                ret += 400 * (cnt[4] / 3)
                ret += 350 * (cnt[5] / 3) + 50 * cnt[5]
                ret += 600 * (cnt[6] / 3)


                Alternatively, we can recognize that the bonus we get is 100 * the die roll for every die except 1, and use that logic:



                ret += 900 * (cnt[1] / 3);  # Adjustment because 1s give 1000 pts, not 100

                # For each value, if 3+ rolled add 100 * value
                for (int i = 1; i <= 6; i++){
                if (cnt[i] / 3){
                ret += 100 * i;
                cnt[i] -= 3;
                }
                }

                # Add individual die score
                ret += cnt[1] * 100;
                ret += cnt[5] * 50;





                share|improve this answer











                $endgroup$
















                  2












                  2








                  2





                  $begingroup$

                  In addition to a lot of other answers, it's worth noting the bonus logic can be simplified. We don't have to strictly adhere to the written version if we can find a logical equivalent:




                  1000 pts for 3 ones



                  100 pts for 1 one (not included in the upper rule)




                  is equivalent to




                  100 pts per one



                  700 pts if 3 or more ones




                  Which means



                  if (cnt[1] == 3)
                  {
                  ret += 1000;
                  }
                  if (cnt[1] == 2)
                  {
                  ret += 200;
                  }
                  if (cnt[1] == 1)
                  {
                  ret += 100;
                  }
                  if (cnt[1] == 4)
                  {
                  ret += 1100;
                  }
                  if (cnt[1] == 5)
                  {
                  ret += 1200;
                  }


                  can be simplified to



                  ret += 100 * cnt[1] + 700 * (cnt[1] / 3)  # Note this will not support more than 5 dice


                  Fives can likewise be simplified and for the other numbers, using integer division we can reduce our summation logic from 56 lines to 6, and it should be clearer what is happening to boot:



                  # sum score per number count - Note this will not support more than 5 dice
                  ret += 700 * (cnt[1] / 3) + 100 * cnt[1]
                  ret += 200 * (cnt[2] / 3)
                  ret += 300 * (cnt[3] / 3)
                  ret += 400 * (cnt[4] / 3)
                  ret += 350 * (cnt[5] / 3) + 50 * cnt[5]
                  ret += 600 * (cnt[6] / 3)


                  Alternatively, we can recognize that the bonus we get is 100 * the die roll for every die except 1, and use that logic:



                  ret += 900 * (cnt[1] / 3);  # Adjustment because 1s give 1000 pts, not 100

                  # For each value, if 3+ rolled add 100 * value
                  for (int i = 1; i <= 6; i++){
                  if (cnt[i] / 3){
                  ret += 100 * i;
                  cnt[i] -= 3;
                  }
                  }

                  # Add individual die score
                  ret += cnt[1] * 100;
                  ret += cnt[5] * 50;





                  share|improve this answer











                  $endgroup$



                  In addition to a lot of other answers, it's worth noting the bonus logic can be simplified. We don't have to strictly adhere to the written version if we can find a logical equivalent:




                  1000 pts for 3 ones



                  100 pts for 1 one (not included in the upper rule)




                  is equivalent to




                  100 pts per one



                  700 pts if 3 or more ones




                  Which means



                  if (cnt[1] == 3)
                  {
                  ret += 1000;
                  }
                  if (cnt[1] == 2)
                  {
                  ret += 200;
                  }
                  if (cnt[1] == 1)
                  {
                  ret += 100;
                  }
                  if (cnt[1] == 4)
                  {
                  ret += 1100;
                  }
                  if (cnt[1] == 5)
                  {
                  ret += 1200;
                  }


                  can be simplified to



                  ret += 100 * cnt[1] + 700 * (cnt[1] / 3)  # Note this will not support more than 5 dice


                  Fives can likewise be simplified and for the other numbers, using integer division we can reduce our summation logic from 56 lines to 6, and it should be clearer what is happening to boot:



                  # sum score per number count - Note this will not support more than 5 dice
                  ret += 700 * (cnt[1] / 3) + 100 * cnt[1]
                  ret += 200 * (cnt[2] / 3)
                  ret += 300 * (cnt[3] / 3)
                  ret += 400 * (cnt[4] / 3)
                  ret += 350 * (cnt[5] / 3) + 50 * cnt[5]
                  ret += 600 * (cnt[6] / 3)


                  Alternatively, we can recognize that the bonus we get is 100 * the die roll for every die except 1, and use that logic:



                  ret += 900 * (cnt[1] / 3);  # Adjustment because 1s give 1000 pts, not 100

                  # For each value, if 3+ rolled add 100 * value
                  for (int i = 1; i <= 6; i++){
                  if (cnt[i] / 3){
                  ret += 100 * i;
                  cnt[i] -= 3;
                  }
                  }

                  # Add individual die score
                  ret += cnt[1] * 100;
                  ret += cnt[5] * 50;






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 23:54

























                  answered Nov 15 '18 at 23:31









                  TemporalWolfTemporalWolf

                  25427




                  25427






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Code Review Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      Use MathJax to format equations. MathJax reference.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207689%2fa-dice-game-called-greed%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()