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)
.
greed
, wouldn'tcalculate_score
androll_dice
be way more explanatory as function names? \$\endgroup\$