Browse Source

refactor: strikethrough on kh/kl prints

Sam Jaffe 1 year ago
parent
commit
612eb7f813
3 changed files with 13 additions and 4 deletions
  1. 7 1
      curses_dice/main.cxx
  2. 1 0
      include/dice-roll/roll.h
  3. 5 3
      src/roll.cxx

+ 7 - 1
curses_dice/main.cxx

@@ -46,7 +46,13 @@ curses::Window & operator<<(curses::Window & window, outcome o) {
 
 curses::Window & operator<<(curses::Window & window, die_outcome const & r) {
   if (auto scope = window.with(color(r.roll == r.sides, r.roll == 1))) {
-    window.printf("%d", r.roll);
+    auto roll = std::to_string(r.roll);
+    std::string strikethrough = "\u0336";
+    for (size_t i = 1; i <= roll.size() && r.dropped;
+         i += 1 + strikethrough.length()) {
+      roll.insert(i, strikethrough);
+    }
+    window.printf("%s", roll.c_str());
   }
   return window;
 }

+ 1 - 0
include/dice-roll/roll.h

@@ -24,6 +24,7 @@ struct die_outcome {
 
   int roll;
   int sides;
+  bool dropped{false};
 };
 
 // Describe the actual result of rolling (+/-)NdM

+ 5 - 3
src/roll.cxx

@@ -13,7 +13,9 @@
 #include "dice-roll/random.h"
 
 namespace dice {
-int add_outcome(int accum, die_outcome const & die) { return accum + die.roll; }
+int add_outcome(int accum, die_outcome const & die) {
+  return accum + (!die.dropped ? die.roll : 0);
+}
 
 die_roll::operator int() const {
   return sgn(sign) *
@@ -47,12 +49,12 @@ die_roll roll_impl(die const & d, engine::random & gen) {
   switch (d.keep.method) {
   case keep::Highest:
     for (int i = 0; i < d.num - d.keep.amount; ++i) {
-      hits.erase(std::min_element(hits.begin(), hits.end()));
+      std::min_element(hits.begin(), hits.end())->dropped = true;
     }
     break;
   case keep::Lowest:
     for (int i = 0; i < d.num - d.keep.amount; ++i) {
-      hits.erase(std::max_element(hits.begin(), hits.end()));
+      std::max_element(hits.begin(), hits.end())->dropped = true;
     }
     break;
   default: