-
Notifications
You must be signed in to change notification settings - Fork 0
/
Die.cpp
43 lines (35 loc) · 754 Bytes
/
Die.cpp
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
#include "Die.h"
#include "util.h"
Die::Die()
{
quantity = 0;
maxRoll = 0;
}
Die::Die(std::string fromString)
{
setFromString(fromString);
}
void Die::setFromString(std::string fromString)
{
std::vector<std::string> splitInput = util::split(fromString, 'd');
quantity = std::stoi(splitInput.at(0));
maxRoll = std::stoi(splitInput.at(1));
}
int Die::roll(int bonus)
{
int totalRoll = 0;
for (int n = 0; n < quantity; n++) {
totalRoll += rand() % maxRoll + 1;
}
totalRoll += bonus;
return totalRoll;
}
int Die::rollToHit(int bonus)
{
return rand() % 20 + 1 + bonus;
}
std::string Die::stringRep()
{
std::string rep = std::to_string(quantity) + "d" + std::to_string(maxRoll);
return rep;
}