die.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // die.hpp
  3. // dice-roll
  4. //
  5. // Created by Sam Jaffe on 12/1/18.
  6. // Copyright © 2018 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <iosfwd>
  10. #include <vector>
  11. namespace dice {
  12. enum class sign { PLUS = 1, MINUS = -1, ZERO = 0 };
  13. template <typename T> static sign sgn(T val) {
  14. return sign((T(0) < val) - (val < T(0)));
  15. }
  16. int sgn(sign);
  17. std::string str(sign);
  18. struct die {
  19. sign sgn;
  20. int num, sides;
  21. };
  22. struct mod {
  23. operator int() const;
  24. sign sign;
  25. int value;
  26. };
  27. struct difficulty_class {
  28. enum class test { None, Less, LessOrEqual, Greater, GreaterOrEqual };
  29. bool operator()(int value) const;
  30. test comp{test::None};
  31. int against{0};
  32. };
  33. // Default value: 1{+0}
  34. struct dice {
  35. int num{1};
  36. std::vector<die> of{};
  37. std::vector<mod> modifier{+0};
  38. difficulty_class dc{};
  39. };
  40. /**
  41. * @brief A generator function to turn a string representation of a dice roll into a C++ object
  42. * @param strdice A string representation of a dice roll, represented as one of the
  43. * following expression classes:
  44. * Die = [1-9]?\d*d[1-9]\d*
  45. * SingleRoll: ($Die|\d+)((+|-)($Die|\d+))*
  46. * RepeatRoll: [1-9]\d*\{$SingleRoll\}
  47. * @return a dice object representing the roll
  48. * @throws dice::unexpected_token if a parse failure occurs
  49. */
  50. dice from_string(std::string const & strdice);
  51. std::ostream & operator<<(std::ostream & out, dice const & d);
  52. std::istream & operator>>(std::istream & out, dice & d);
  53. }