diff --git a/.Jules/palette.md b/.Jules/palette.md index 853ef9c..1139435 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-31 - Professionalizing CLI UI with Formatting and Line Clearing +**Learning:** Large numbers in CLI games become hard to read at a glance; implementing thousands separators (like `formatWithCommas`) significantly improves immediate comprehension. Additionally, using the ANSI escape sequence `\033[K` (Erase in Line) is a more robust way to handle volatile line updates than manual space padding, as it avoids "ghost" characters regardless of terminal width or previous string length. +**Action:** Always format large numeric outputs for readability and use semantic ANSI clearing sequences for in-place terminal updates. diff --git a/src/main.cpp b/src/main.cpp index af692c8..d85531a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,6 +24,16 @@ struct termios oldt; +std::string formatWithCommas(long long value) { + std::string s = std::to_string(value); + int n = s.length() - 3; + while (n > 0) { + s.insert(n, ","); + n -= 3; + } + return s; +} + void restore_terminal(int signum) { tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // Use write() and _exit() because they are async-signal-safe @@ -77,13 +87,13 @@ 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 " << CLR_CTRL << "[q]" << CLR_RESET << " Quit Game\n " << CLR_CTRL << "[Any key]" << CLR_RESET << " Click!\n\n"; - std::cout << "Press any key to start... " << std::flush; + std::cout << "Press " << CLR_CTRL << "any key" << CLR_RESET << " to start... " << std::flush; struct pollfd start_fds[1] = {{STDIN_FILENO, POLLIN, 0}}; if (poll(start_fds, 1, -1) > 0) { if (read(STDIN_FILENO, &input, 1) > 0 && input == 'q') { @@ -94,7 +104,7 @@ int main() { } for (int i = 3; i > 0; --i) { - std::cout << "\rStarting in " << CLR_CTRL << i << CLR_RESET << "... " << std::flush; + std::cout << "\rStarting in " << CLR_CTRL << i << CLR_RESET << "... \033[K" << std::flush; auto start_wait = std::chrono::steady_clock::now(); while (std::chrono::duration_cast(std::chrono::steady_clock::now() - start_wait).count() < 1000) { int elapsed = std::chrono::duration_cast(std::chrono::steady_clock::now() - start_wait).count(); @@ -108,7 +118,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 +151,11 @@ 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 +165,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;