die.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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;
  31. int against;
  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. };
  39. /**
  40. * @brief A generator function to turn a string representation of a dice roll into a C++ object
  41. * @param strdice A string representation of a dice roll, represented as one of the
  42. * following expression classes:
  43. * Die = [1-9]?\d*d[1-9]\d*
  44. * SingleRoll: ($Die|\d+)((+|-)($Die|\d+))*
  45. * RepeatRoll: [1-9]\d*\{$SingleRoll\}
  46. * @return a dice object representing the roll
  47. * @throws dice::unexpected_token if a parse failure occurs
  48. */
  49. dice from_string(std::string const & strdice);
  50. std::ostream & operator<<(std::ostream & out, dice const & d);
  51. std::istream & operator>>(std::istream & out, dice & d);
  52. }