-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiceRoll.hpp
57 lines (43 loc) · 1.31 KB
/
DiceRoll.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef SRC_DICEROLL_HPP_
#define SRC_DICEROLL_HPP_
#include <iostream>
#include <random>
#include <type_traits>
struct DiceQty {
int value;
explicit DiceQty(int value)
: value(value) {};
};
struct DiceSides {
int value;
explicit DiceSides(int value)
: value(value) {};
};
struct RollResultProxy {
DiceQty diceQty;
DiceSides diceSides;
int result;
RollResultProxy(DiceQty diceQty, DiceSides diceSides, int result)
: diceQty(diceQty),
diceSides(diceSides),
result(result) {}
explicit operator int() const {
return result;
}
};
template<int diceQty, int diceSides> RollResultProxy roll() {
static_assert(diceQty > 0, "Dice quantity must be greater than 0");
static_assert(diceSides > 0, "Dice sides must be greater than 0");
std::random_device rDev; // initiates a random device
// set the distribution bounds to the total number of die sides
std::uniform_int_distribution<int> dist(1, diceSides);
int results = 0;
// sum the total number of die rolls
for (int i = 0; i < diceQty; ++i) {
results += dist(rDev);
}
return { DiceQty(diceQty), DiceSides(diceSides), results };
}
int roll(DiceQty diceQty, DiceSides diceSides);
std::ostream& operator<<(std::ostream& out, const RollResultProxy& roll);
#endif // SRC_DICEROLL_HPP_