diff --git a/.Jules/palette.md b/.Jules/palette.md index 853ef9c..f197a0e 100644 --- a/.Jules/palette.md +++ b/.Jules/palette.md @@ -29,3 +29,7 @@ ## 2025-01-24 - Real-time Achievement Feedback in CLI **Learning:** In terminal-based games, displaying achievement progress (like a live high score) in real-time provides immediate tactile reward and engagement. Furthermore, inclusive UX means ensuring first-time players also receive "New Best" feedback, even when their initial record is zero. **Action:** Update session-high-score variables immediately upon record-breaking and display them in the live HUD. Ensure achievement conditions (`score > highscore`) don't exclude the first-time user experience. + +## 2026-03-29 - Robust CLI Line Clearing & Score Readability +**Learning:** Manual space padding in CLI HUDs is brittle and often leaves "ghost" characters when strings shorten. Using the ANSI "Erase in Line" sequence (`\033[K`) ensures a clean UI regardless of string length. Additionally, providing thousands separators in clicker games significantly reduces cognitive load as scores escalate. +**Action:** Always prefer `\033[K` over manual space padding for in-place line updates, and implement a `formatWithCommas` helper for all numeric score displays. diff --git a/src/main.cpp b/src/main.cpp index af692c8..e1dc667 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,6 @@ #include // Color and formatting macros for terminal output -#define RESET "\033[0m" #define RED "\033[31m" #define GREEN "\033[32m" #define YELLOW "\033[33m" @@ -50,6 +49,15 @@ void save_highscore(long long score) { } } +std::string formatWithCommas(long long value) { + std::string s = std::to_string(value); + int n = s.length(); + for (int i = n - 3; i > 0; i -= 3) { + s.insert(i, ","); + } + return s; +} + int main() { struct termios newt; if (tcgetattr(STDIN_FILENO, &oldt) == -1) { @@ -77,7 +85,7 @@ int main() { std::cout << CLR_CTRL << "==========================\n SPEED CLICKER\n==========================\n" << CLR_RESET; if (highscore > 0) { - std::cout << " Personal Best: " << CLR_SCORE << highscore << CLR_RESET << "\n\n"; + std::cout << " Personal Best: " << CLR_SCORE << formatWithCommas(highscore) << CLR_RESET << "\n\n"; } std::cout << "Controls:\n " << CLR_CTRL << "[h]" << CLR_RESET << " Toggle Hard Mode (10x Speed!)\n " @@ -108,7 +116,7 @@ int main() { } } } - std::cout << "\r" << CLR_NORM << "GO! " << CLR_RESET << "\n" << std::flush; + std::cout << "\r" << CLR_NORM << "GO!\033[K" << CLR_RESET << "\n" << std::flush; std::this_thread::sleep_for(std::chrono::milliseconds(200)); tcflush(STDIN_FILENO, TCIFLUSH); @@ -141,10 +149,10 @@ int main() { } if (updateUI) { - std::cout << "\r" << CLR_SCORE << "Score: " << score << CLR_RESET << " | High: " << highscore << " " + std::cout << "\r" << CLR_SCORE << "Score: " << formatWithCommas(score) << CLR_RESET << " | High: " << formatWithCommas(highscore) << " " << (hardMode ? CLR_HARD "[HARD MODE]" : CLR_NORM "[NORMAL MODE]") << (score > initialHighscore ? " NEW BEST! 🥳" : "") - << " " << std::flush; + << "\033[K" << std::flush; updateUI = false; } } @@ -154,9 +162,9 @@ int main() { } tcsetattr(STDIN_FILENO, TCSANOW, &oldt); - std::cout << "\n\n" << CLR_SCORE << "Final Score: " << score << CLR_RESET << "\n"; + std::cout << "\n\n" << CLR_SCORE << "Final Score: " << formatWithCommas(score) << CLR_RESET << "\n"; if (score > initialHighscore) { - std::cout << "Congratulations! A new personal best!\n"; + std::cout << "Congratulations! A new personal best! (Previous: " << formatWithCommas(initialHighscore) << ")\n"; } std::cout << "Thanks for playing!\n"; std::cout << "\033[?25h" << std::flush;