Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .Jules/palette.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
**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.

## 2024-04-24 - Avoiding Trailing Artifacts in CLI Applications
**Learning:** When dynamically updating terminal lines using carriage return (`\r`), relying on hardcoded padding spaces to overwrite old text is brittle and leads to trailing text artifacts when new lines are shorter than previous ones.
**Action:** Always use the ANSI escape sequence `\033[K` (Erase in Line) immediately after the carriage return (`\r`) to cleanly clear the line before writing new content, rather than manually managing padding spaces.
## 2026-05-02 - Thousands Separators for Numeric Readability
**Learning:** In applications where numeric values (like scores) can grow large, readability suffers without thousands separators. Adding a simple comma-formatting helper improves the user's ability to quickly parse large numbers at a glance.
**Action:** Use a thousands-separator formatter for all significant numeric displays in CLI and GUI applications to enhance readability as values escalate.
28 changes: 23 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@
#define CLR_HARD "\033[1;31m"
#define CLR_NORM "\033[1;32m"
#define CLR_CTRL "\033[1;33m"
#define CLR_EOL "\033[K"
#define CLR_RESET "\033[0m"
#define ERASE_LINE "\033[K"

struct termios oldt;

std::string formatWithCommas(long long value) {
std::string s = std::to_string(value);
int n = s.length();
int insertPosition = n - 3;
int limit = (value < 0) ? 1 : 0;
while (insertPosition > limit) {
s.insert(insertPosition, ",");
insertPosition -= 3;
}
return s;
}

void restore_terminal(int signum) {
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
// Use write() and _exit() because they are async-signal-safe
Expand Down Expand Up @@ -78,7 +91,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 "
Expand All @@ -95,7 +108,7 @@ int main() {
}

for (int i = 3; i > 0; --i) {
std::cout << "\r" ERASE_LINE "Starting in " << CLR_CTRL << i << CLR_RESET << "... " << std::flush;
std::cout << "\rStarting in " << CLR_CTRL << i << CLR_RESET << "... " << CLR_EOL << std::flush;
auto start_wait = std::chrono::steady_clock::now();
while (std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_wait).count() < 1000) {
int elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_wait).count();
Expand All @@ -109,7 +122,7 @@ int main() {
}
}
}
std::cout << "\r" ERASE_LINE << CLR_NORM << "GO!" << CLR_RESET << "\n" << std::flush;
std::cout << "\r" << CLR_NORM << "GO! " << CLR_RESET << CLR_EOL << "\n" << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
tcflush(STDIN_FILENO, TCIFLUSH);

Expand Down Expand Up @@ -142,6 +155,11 @@ int main() {
}

if (updateUI) {
std::cout << "\r" << CLR_SCORE << "Score: " << formatWithCommas(score) << CLR_RESET
<< " | High: " << CLR_SCORE << formatWithCommas(highscore) << CLR_RESET << " "
<< (hardMode ? CLR_HARD "[HARD MODE]" : CLR_NORM "[NORMAL MODE]")
<< (score > initialHighscore ? " NEW BEST! 🥳" : "")
<< CLR_EOL << std::flush;
std::cout << "\r" ERASE_LINE << CLR_SCORE << "Score: " << score << CLR_RESET << " | High: " << highscore << " "
<< (hardMode ? CLR_HARD "[HARD MODE]" : CLR_NORM "[NORMAL MODE]")
<< (score > initialHighscore ? " NEW BEST! 🥳" : "")
Expand All @@ -155,9 +173,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 record: " << formatWithCommas(initialHighscore) << ")\n";
}
std::cout << "Thanks for playing!\n";
std::cout << "\033[?25h" << std::flush;
Expand Down
Loading