Menu
CoddyTech

Recap - Score Tracker

Part of the Object Oriented Programming section of Coddy's Lua journey. Lesson 69 of 70.

challenge icon

Challenge

Easy

Let'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 a playerName and a score. Include a :getInfo() method that returns a string in the format "{playerName}: {score} points". The key feature here is the __lt metamethod: a < b should be true when a's score is lower than b's, the same way < works on numbers.
  • 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 using table.sort. On its own, table.sort puts the smallest first, so pass it a comparison that reverses the order: function(a, b) return b < a end. That comparison still uses your __lt.
    • :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.

Comparing two entries with < calls your __lt metamethod, so table.sort can order ScoreEntry objects. Keep __lt meaning "less than" and reverse the order in the sort call: if __lt meant "greater than", every < on two entries anywhere in the program would read backwards.

You will receive six inputs representing three players:

  1. First player's name
  2. First player's score (a number)
  3. Second player's name
  4. Second player's score (a number)
  5. Third player's name
  6. 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 points

If the inputs are Zara, 500, Max, 750, Luna, and 600, the output should be:

Max: 750 points
Luna: 600 points
Zara: 500 points

Try 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

Practice on your own: Online Lua compiler