Recap - Score Tracker
Part of the Object Oriented Programming section of Coddy's Lua journey. Lesson 69 of 70.
Challenge
EasyLet's build a score tracking system for a gaming leaderboard! You'll create two classes that work together: one representing individual score entries, and another managing the leaderboard with sorting capabilities powered by operator overloading.
You'll organize your code across three files:
ScoreEntry.lua: Create a ScoreEntry class that represents a single player's score. Each entry has aplayerNameand ascore. Include a:getInfo()method that returns a string in the format"{playerName}: {score} points". The key feature here is implementing the__ltmetamethod, but remember, for descending order (highest scores first), your comparison should returntruewhen the first entry's score is greater than the second's.Leaderboard.lua: Build a Leaderboard class that manages a collection of score entries. Your leaderboard needs::addEntry(entry): adds a ScoreEntry to the internal list:sort(): sorts entries from highest to lowest score usingtable.sort:display(): prints the info for each entry, one per line
main.lua: Bring everything together! Create a Leaderboard, add score entries based on the inputs you receive, sort the leaderboard, and display the results.
The elegant part of this design is that table.sort automatically uses your __lt metamethod to compare ScoreEntry objects. By defining how entries compare to each other, Lua's built-in sorting just works with your custom objects!
You will receive six inputs representing three players:
- First player's name
- First player's score (a number)
- Second player's name
- Second player's score (a number)
- Third player's name
- Third player's score (a number)
In your main file, create a leaderboard, add all three entries, call :sort(), then call :display() to show the sorted results.
For example, if the inputs are Alice, 1500, Bob, 2300, Charlie, and 1800, the output should be:
Bob: 2300 points
Charlie: 1800 points
Alice: 1500 pointsIf the inputs are Zara, 500, Max, 750, Luna, and 600, the output should be:
Max: 750 points
Luna: 600 points
Zara: 500 pointsTry it yourself
-- Main file: Bring everything together
local ScoreEntry = require('ScoreEntry')
local Leaderboard = require('Leaderboard')
-- Read inputs for three players
local name1 = io.read()
local score1 = tonumber(io.read())
local name2 = io.read()
local score2 = tonumber(io.read())
local name3 = io.read()
local score3 = tonumber(io.read())
-- TODO: Create a new Leaderboard
-- TODO: Create ScoreEntry objects for each player
-- TODO: Add all three entries to the leaderboard
-- TODO: Sort the leaderboard
-- TODO: Display the sorted results
All lessons in Object Oriented Programming
1The 'Self' Concept
Tables with FunctionsExplicit 'self'The Colon SyntaxDot vs ColonRecap - Moving Point4Project: Digital Bank
Project SetupDeposit Method7Polymorphism & Overriding
Overriding MethodsCalling Parent MethodsDuck TypingCommon InterfaceChecking TypeRecap - Employee Roles10Project: Shape Manager
Project SetupRectangle ClassCircle ClassPerimeter MethodShape CollectionTotal AreaFilter Shapes2Class Prototype Pattern
The Prototype ConceptLinking with __indexThe :new() ConstructorInitializing AttributesIndependent InstancesRecap - Car Factory5Operator Overloading in OOP
Adding ObjectsSubtracting ObjectsConcatenating ObjectsComparing Objects (<, >)Recap - Wallet Math8Encapsulation
Naming ConventionsClosures for PrivacyAccess via ClosuresRead-Only TablesValidation LogicRecap - Secure Vault11Design Patterns (Lite)
Factory FunctionsSingleton TableIterator PatternObserver (Listener)Recap - Logger Factory3Object State and Behavior
Instance VariablesGetter MethodsSetter MethodsCalculated PropertiesFormatting StringsEquality ChecksRecap - Student Grade6Inheritance Basics
The Inheritance SetupInheriting MethodsExtending the ConstructorAdding Child MethodsShared vs UniqueRecap - Shape HierarchyPractice on your own: Online Lua compiler