Skip to content

Add username support for high scores with persistent storage#68

Merged
Project516 merged 4 commits into
masterfrom
copilot/add-usernames-to-high-scores
Oct 27, 2025
Merged

Add username support for high scores with persistent storage#68
Project516 merged 4 commits into
masterfrom
copilot/add-usernames-to-high-scores

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Oct 22, 2025

Overview

This PR implements a high score system that tracks usernames and scores (number of guesses to win) with persistent storage across game sessions. The feature works seamlessly in both GUI and console modes.

Implementation

Core Classes

HighScore - Immutable data class representing a single high score entry:

  • Stores username and number of guesses
  • Implements Comparable for sorting by fewest guesses
  • Validates username and guess count on construction

HighScoreManager - Manages high score persistence and retrieval:

  • Saves high scores to ~/.numberguessinggame/highscores.properties
  • Maintains top 10 scores across game sessions
  • Provides thread-safe loading/saving with graceful error handling
  • Supports custom config directory for testing

Username - Utility class for username validation:

  • Validates usernames (1-20 characters)
  • Prompts users for username input in console mode
  • Handles Scanner buffer clearing to prevent input issues

GUI Mode Integration

  • Added username prompt dialog after winning the game
  • Created new "View" menu with "High Scores" option to view scores anytime
  • Displays formatted high score table after saving a score
  • Gracefully handles high score system failures

Console Mode Integration

  • Automatically prompts for username after winning
  • Displays top 5 high scores after each game
  • Fixed Scanner buffer issue where leftover newlines from nextInt() caused empty reads

Testing

  • Added 17 comprehensive unit tests across 3 new test files
  • All 31 tests pass (100% success rate)
  • Manual testing verified in console mode
  • CodeQL security scan: 0 alerts

Storage Format

High scores are stored in Java properties format at ~/.numberguessinggame/highscores.properties:

#Number Guessing Game High Scores
score.0=Alice:5
score.1=Bob:7
score.2=Charlie:9

Example Usage

Console Mode:

Guess a number: 42
Took you 5 guesses!
Enter your username: Alice
Score saved!

=== Top High Scores ===
1. Alice: 5 guesses
2. Bob: 7 guesses
3. Charlie: 9 guesses
=======================

GUI Mode:

  • Win the game → Username prompt appears
  • Enter username → High scores dialog displays
  • View → High Scores menu option to view anytime

Code Quality

  • Follows existing Google Java Format AOSP style
  • Comprehensive Javadoc documentation
  • Minimal changes to existing code
  • Backward compatible (Java 8+ target)
  • No new external dependencies

Files Changed

  • 5 new files: HighScore.java, HighScoreManager.java, and 3 test files
  • 4 modified files: Username.java, GameLogic.java, GUI.java, README.md
  • Total: +651 lines, -12 lines

Closes #[issue_number]

Original prompt

This section details on the original issue you should resolve

<issue_title>Add usernames for high scores</issue_title>
<issue_description></issue_description>

<agent_instructions>Keep the code modification to a minimum, document all changes, and try to make the storage perminate and work for both gui and console. Make sure to be consistant with the code style</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #23


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits October 22, 2025 14:43
…modes

Co-authored-by: Project516 <138796702+Project516@users.noreply.github.com>
Co-authored-by: Project516 <138796702+Project516@users.noreply.github.com>
Co-authored-by: Project516 <138796702+Project516@users.noreply.github.com>
Copilot AI changed the title [WIP] Add usernames for high scores functionality Add username support for high scores with persistent storage Oct 22, 2025
Copilot AI requested a review from Project516 October 22, 2025 14:56
@Project516 Project516 marked this pull request as ready for review October 27, 2025 17:47
@Project516 Project516 merged commit 5ec0b0c into master Oct 27, 2025
5 checks passed
@Project516 Project516 deleted the copilot/add-usernames-to-high-scores branch October 27, 2025 18:17
Project516 added a commit that referenced this pull request Feb 23, 2026
…scores

Add username support for high scores with persistent storage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add usernames for high scores

2 participants