// // die.hpp // dice-roll // // Created by Sam Jaffe on 12/1/18. // Copyright © 2018 Sam Jaffe. All rights reserved. // #pragma once #include #include namespace dice { enum class sign { PLUS = 1, MINUS = -1, ZERO = 0 }; template 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; }; 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 of{}; std::vector 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, dice const & d); std::istream & operator>>(std::istream & out, dice & d); }