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
4 changes: 4 additions & 0 deletions .Jules/palette.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-05-24 - Preventing Visual Artifacts in CLI
**Learning:** Using carriage returns (`\r`) for in-place terminal updates can leave "ghost characters" if the new line is shorter than the previous one. The ANSI 'Erase in Line' sequence (`\033[K`), or `CLR_EOL`, is a more robust solution than space-padding as it guarantees a clean line regardless of terminal width or previous content.
**Action:** Use `CLR_EOL` at the end of every `\r` update line to ensure a clean visual transition, especially during state changes like countdowns or score magnitude shifts.
31 changes: 24 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#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"

struct termios oldt;
Expand Down Expand Up @@ -50,6 +51,17 @@ void save_highscore(long long score) {
}
}

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

int main() {
struct termios newt;
if (tcgetattr(STDIN_FILENO, &oldt) == -1) {
Expand Down Expand Up @@ -77,7 +89,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 @@ -94,7 +106,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 << "... " << 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 @@ -108,7 +120,7 @@ int main() {
}
}
}
std::cout << "\r" << 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 @@ -141,10 +153,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 << " | " << CLR_SCORE << "High: " << formatWithCommas(highscore) << CLR_RESET << " "
<< (hardMode ? CLR_HARD "[HARD MODE]" : CLR_NORM "[NORMAL MODE]")
<< (score > initialHighscore ? " NEW BEST! 🥳" : "")
<< " " << std::flush;
<< CLR_EOL << std::flush;
updateUI = false;
}
}
Expand All @@ -154,9 +167,13 @@ 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 << CLR_NORM << "Congratulations! A new personal best!" << CLR_RESET;
if (initialHighscore > 0) {
std::cout << " (Previous: " << CLR_SCORE << formatWithCommas(initialHighscore) << CLR_RESET << ")";
}
std::cout << "\n";
}
std::cout << "Thanks for playing!\n";
std::cout << "\033[?25h" << std::flush;
Expand Down
Loading