-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDice.cpp
More file actions
99 lines (77 loc) · 2.08 KB
/
Dice.cpp
File metadata and controls
99 lines (77 loc) · 2.08 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Dice.h"
std::uniform_int_distribution<int> RandomDice::die{1,6};
std::default_random_engine RandomDice::generator{ static_cast<unsigned int>(time(NULL)) };
//std::default_random_engine RandomDice::generator{static_cast<unsigned long>(time(NULL))};
Dice::Dice(Color _color) : d_color(_color)
{
roll();
}
int Dice::roll()
{
return d_face = RandomDice::die(RandomDice::generator);
}
std::ostream& operator<<(std::ostream& _out, const Dice& _dice)
{
_out << "\t\t\t\t";
switch(_dice.d_color)
{
case Color::RED:
_out << "Red ";
break;
case Color::YELLOW:
_out << "Yellow";
break;
case Color::BLUE:
_out << "Blue";
break;
case Color::GREEN:
_out << "Green";
break;
case Color::WHITE_1:
_out << "White-1";
break;
case Color::WHITE_2:
_out << "White-2";
break;
case Color::INVALID:
break;
}
_out << "\t|" << _dice.d_face << "|" << std::endl;
_out << "\t\t\t\t";
_out << "------------";
return _out;
}
RollOfDice RollOfDice::roll()
{
for(auto &die : *this)
{
die.roll();
}
return *this;
}
//revise if inheritance is not permited
RollOfDice RollOfDice::pair(const int _index1, const int _index2) const
{
RollOfDice pairedRoll;
pairedRoll.push_back((*this)[_index1]);
pairedRoll.push_back((*this)[_index2]);
return pairedRoll;
}
RollOfDice::operator int() const
{
int total = 0;
for(auto die : *this)
total += die.d_face;
return total;
}
std::ostream& operator<<(std::ostream& _out, const RollOfDice& _roll)
{
_out << "Roll: " << std::endl;
_out << "\t\t\t\t";
_out << "------------" << std::endl;
for(auto die : _roll)
_out << die << std::endl;
_out << "\t\t\t\t" << "Total: " << static_cast<int>(_roll) << std::endl;
_out << std::endl;
return _out;
}