| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- //
- // die.hpp
- // dice-roll
- //
- // Created by Sam Jaffe on 12/1/18.
- // Copyright © 2018 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <iosfwd>
- #include <string>
- #include <vector>
- namespace dice {
- enum class sign { PLUS = 1, MINUS = -1, ZERO = 0 };
- template <typename T> static sign sgn(T val) {
- return sign((T(0) < val) - (val < T(0)));
- }
- int sgn(sign);
- struct die {
- sign sgn;
- int num, sides;
- };
- struct mod {
- operator int() const;
- sign sign;
- int value;
- };
- /**
- * In some cases, the roller is not interested in the actual value of the dice
- * rolled, but only if they pass some metric.
- * This object allows us to perform that check, and then conveniently obfuscate
- * the numbers rolled so that the player cannot intuit their odds of success in
- * the short term.
- */
- struct difficulty_class {
- enum class test { None, Less, LessOrEqual, Greater, GreaterOrEqual };
- bool operator()(int value) const;
- test comp{test::None};
- int against{0};
- };
- // Default value: 1{+0}
- struct dice {
- int num{1};
- std::vector<die> of{};
- std::vector<mod> modifier{+0};
- difficulty_class dc{};
- };
- /**
- * @brief A generator function to turn a string representation of a dice roll
- * into a C++ object
- * @param strdice A string representation of a dice roll, represented as one of
- * the following expression classes: Die = [1-9]?\d*d[1-9]\d* SingleRoll:
- * ($Die|\d+)((+|-)($Die|\d+))* RepeatRoll: [1-9]\d*\{$SingleRoll\}
- * @return a dice object representing the roll
- * @throws dice::unexpected_token if a parse failure occurs
- */
- dice from_string(std::string const & strdice);
- std::ostream & operator<<(std::ostream & out, sign s);
- std::ostream & operator<<(std::ostream & out, dice const & d);
- std::istream & operator>>(std::istream & out, dice & d);
- }
|