| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- //
- // die.hpp
- // dice-roll
- //
- // Created by Sam Jaffe on 12/1/18.
- // Copyright © 2018 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <iosfwd>
- #include <vector>
- namespace dice {
- enum 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);
- std::string str(sign);
- struct die {
- sign sgn;
- int num, sides;
- };
- struct mod {
- operator int() const;
- sign sign;
- int value;
- };
- // Default value: 1{+0}
- struct dice {
- int num{1};
- std::vector<die> of{};
- std::vector<mod> modifier{+0};
- };
-
- /**
- * @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, dice const & d);
- std::istream & operator>>(std::istream & out, dice & d);
- }
|