Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
Dice.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_DICE_H
22 #define GF_DICE_H
23 
24 #include "Portability.h"
25 #include "Random.h"
26 #include "StringRef.h"
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
40  class GF_API Dice {
41  public:
52  constexpr explicit Dice(int faces, int count = 1, int modifier = 0)
53  : m_faces(faces)
54  , m_count(count)
55  , m_modifier(modifier)
56  {
57 
58  }
59 
65  constexpr Dice(StringRef str)
66  : m_faces(0)
67  , m_count(0)
68  , m_modifier(0)
69  {
70  std::size_t i = 0;
71  std::size_t sz = str.getSize();
72 
73  while (i < sz && '0' <= str[i] && str[i] <= '9') {
74  m_count = m_count * 10 + (str[i] - '0');
75  ++i;
76  }
77 
78  if (i == sz) {
79  return;
80  }
81 
82  if (str[i] != 'd' && str[i] != 'D') {
83  return;
84  }
85 
86  if (m_count == 0) {
87  m_count = 1;
88  }
89 
90  ++i; // eat 'd'
91 
92  while (i < sz && '0' <= str[i] && str[i] <= '9') {
93  m_faces = m_faces * 10 + (str[i] - '0');
94  ++i;
95  }
96 
97  if (i == sz) {
98  return;
99  }
100 
101  if (str[i] != '+') {
102  return;
103  }
104 
105  ++i; // eat '+'
106 
107  while (i < sz && '0' <= str[i] && str[i] <= '9') {
108  m_modifier = m_modifier * 10 + (str[i] - '0');
109  ++i;
110  }
111  }
112 
118  constexpr int getFaces() const {
119  return m_faces;
120  }
121 
127  constexpr int getCount() const {
128  return m_count;
129  }
130 
136  constexpr int getModifier() const {
137  return m_modifier;
138  }
139 
145  int roll(gf::Random& random) const;
146 
147  private:
148  int m_faces;
149  int m_count;
150  int m_modifier;
151  };
152 
153 
154  namespace literals {
155 
166  constexpr gf::Dice operator"" _dice(const char *str, std::size_t sz) {
167  return gf::Dice(StringRef(str, sz));
168  }
169 
176  constexpr gf::Dice operator"" _d4(unsigned long long int count) {
177  return gf::Dice(4, count);
178  }
179 
186  constexpr gf::Dice operator"" _d6(unsigned long long int count) {
187  return gf::Dice(6, count);
188  }
189 
196  constexpr gf::Dice operator"" _d8(unsigned long long int count) {
197  return gf::Dice(8, count);
198  }
199 
206  constexpr gf::Dice operator"" _d10(unsigned long long int count) {
207  return gf::Dice(10, count);
208  }
209 
216  constexpr gf::Dice operator"" _d12(unsigned long long int count) {
217  return gf::Dice(12, count);
218  }
219 
226  constexpr gf::Dice operator"" _d20(unsigned long long int count) {
227  return gf::Dice(20, count);
228  }
229 
236  constexpr gf::Dice operator"" _d100(unsigned long long int count) {
237  return gf::Dice(100, count);
238  }
239  }
240 
241 #ifndef DOXYGEN_SHOULD_SKIP_THIS
242 }
243 #endif
244 }
245 
246 #endif // GF_DICE_H
A random engine.
Definition: Random.h:45
constexpr int getCount() const
Get the count of dice.
Definition: Dice.h:127
constexpr int getModifier() const
Get the modifier.
Definition: Dice.h:136
constexpr Dice(int faces, int count=1, int modifier=0)
Constructor.
Definition: Dice.h:52
constexpr int getFaces() const
Get the number of faces.
Definition: Dice.h:118
The namespace for gf classes.
Definition: Action.h:35
constexpr Dice(StringRef str)
Constructor.
Definition: Dice.h:65
A set of dice that can be rolled.
Definition: Dice.h:40
A constant reference to a string and its size.
Definition: StringRef.h:41
constexpr std::size_t getSize() const noexcept
Get the size of the string.
Definition: StringRef.h:120