-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDice.cpp
60 lines (51 loc) · 1.2 KB
/
Dice.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "Dice.h"
#include <sstream>
#include <boost/regex.hpp>
Dice::Dice()
{
this->base = 0;
this->number = 0;
this->sides = 0;
this->assigned = false;
}
Dice::Dice(const std::string &str)
{
boost::regex regex("^([0-9]+)\\+([0-9]+)d([0-9]+)$");
boost::cmatch matches;
boost::regex_match(str.c_str(), matches, regex);
std::istringstream(matches[1]) >> this->base;
std::istringstream(matches[2]) >> this->number;
std::istringstream(matches[3]) >> this->sides;
this->assigned = true;
}
Dice::Dice(int base, int number, int sides)
{
this->base = base;
this->number = number;
this->sides = sides;
}
Dice::~Dice()
{
}
Dice &Dice::operator=(const Dice &other)
{
this->base = other.base;
this->number = other.number;
this->sides = other.sides;
this->assigned = other.assigned;
return *this;
}
unsigned int Dice::Roll()
{
unsigned int result = base, i;
if(sides == 0) return result;
for(i = 0; i < number; i++)
{
result += (random() % sides) + 1;//somehow, this works even when I don't include <random>
}
return result;
}
std::ostream &operator<<(std::ostream &o, const Dice &d)
{
return o << d.base << "+" << d.number << "d" << d.sides;
}