Game High Score System
Introduction
This system manages high scores for a game. It consists of two main components:
1. GameEntry
- Represents an individual player's name and score.
2. ScoreBoard
- Maintains a list of top scores, sorted in descending order.
Concept & Logic
1️⃣ GameEntry Class
- Stores the player's name and score.
- Uses private (
__name
) and protected (_score
) attributes for encapsulation. - Provides getter methods for controlled access.
2️⃣ ScoreBoard Class
- Stores a fixed number of top scores.
- Automatically inserts new scores in the correct position.
- Removes the lowest score if the list is full.
- Uses a preallocated list for efficiency.
Time Complexity Analysis
Method | Time Complexity | Explanation |
---|---|---|
get_name() |
O(1) | Direct access to private attribute |
get_score() |
O(1) | Direct access to protected attribute |
__repr__() |
O(n) | Iterates through all stored entries |
__getitem__() |
O(1) | Direct index access |
add(entry) |
O(n) | Shifts elements for insertion |
Code Implementation
class GameEntry:
"""
Represents a player's score entry.
"""
def __init__(self, name, score):
self.__name = name # Private attribute
self._score = score # Protected attribute
def get_name(self):
"""Returns the player's name. O(1)"""
return self.__name
def get_score(self):
"""Returns the player's score. O(1)"""
return self._score
def __repr__(self):
"""String representation of the entry. O(1)"""
return f'GameEntry(Name: {self.__name}, Score: {self._score})'
class ScoreBoard:
"""
Stores the top scores of the game.
"""
def __init__(self, capacity=10):
"""Initializes a scoreboard with a fixed capacity."""
self.__board = [None] * capacity # Preallocated list
self.__n = 0 # Number of stored scores
def __repr__(self):
"""Returns formatted scoreboard. O(n)"""
return '\n'.join(str(self.__board[j]) for j in range(self.__n))
def __getitem__(self, index):
"""Returns the score at the given index. O(1)"""
return self.__board[index]
def add(self, entry: GameEntry):
"""
Adds a new GameEntry, keeping scores in descending order.
Removes lowest score if necessary.
O(n) due to shifting elements.
"""
score = entry.get_score()
# Check if we can insert the new score
if self.__n < len(self.__board) or score > self.__board[self.__n - 1].get_score():
if self.__n < len(self.__board):
self.__n += 1 # Increase count if space is available
# Find the position to insert the new score
j = self.__n - 1
while j > 0 and self.__board[j - 1].get_score() < score:
self.__board[j] = self.__board[j - 1]
j -= 1
# Insert the new score
self.__board[j] = entry
# Example Usage
game1 = GameEntry("Alice", 100)
game2 = GameEntry("Bob", 200)
game3 = GameEntry("Charlie", 150)
scoreboard = ScoreBoard()
scoreboard.add(game1)
scoreboard.add(game2)
scoreboard.add(game3)
print(scoreboard)
Key Takeaways
✅ Encapsulation: Uses private and protected attributes for data security.
✅ Efficiency: Preallocates memory to avoid frequent resizing.
✅ Sorting Mechanism: Keeps scores in descending order dynamically.
✅ Fixed Capacity: Ensures only the top scores are stored.
🎯 How It Works?
- Create a
GameEntry
: Store player name and score. - Add to
ScoreBoard
: Finds the correct position and inserts the new score. - Maintains Sorted Order: Shifts elements to keep highest scores at the top.
Example Output
GameEntry(Name: Bob, Score: 200)
GameEntry(Name: Charlie, Score: 150)
GameEntry(Name: Alice, Score: 100)
Conclusion
This system efficiently stores and manages game high scores, ensuring that only the highest scores remain. 🚀