DEV Community

Sri Mahalakshmi
Sri Mahalakshmi

Posted on

Building a Leaderboard for a Number Guessing Game using Selection Sort

Introduction

What is a Number Guessing Game?

A number guessing game is a simple game where:

The system generates a random number
The player tries to guess that number
The system gives hints like:
"Too high"
"Too low"
The game continues until the correct number is guessed

How Performance is Measured

The number of attempts taken by a player is recorded
The difficulty level (Easy, Medium, Hard) may also be chosen

In a number guessing game, just finding the correct number is not enough.
We also need to compare players and rank them based on their performance.

This can be done using a leaderboard system, where players are sorted based on:

  • Difficulty level
  • Number of attempts

Problem Statement

The task is to:

  1. Show the leaderboard details
  2. Sort the leaderboard based on:
  • Difficulty (Easy → Medium → Hard)
  • Attempts (less attempts = better rank)

Example Input

Alice   Hard    7
Bob     Easy    3
Charlie Medium  5
David   Easy    2
Enter fullscreen mode Exit fullscreen mode

Expected Output

David   Easy    2
Bob     Easy    3
Charlie Medium  5
Alice   Hard    7
Enter fullscreen mode Exit fullscreen mode

Concept Used: Selection Sort

Selection Sort works in a simple way:

  1. Find the smallest element
  2. Place it in the correct position
  3. Repeat for the remaining elements

In this problem:

  • “Smallest” means:

    • Easier difficulty first
    • If same difficulty → fewer attempts

Step 1: Store Leaderboard Data

leaderboard = [
    {"name": "Alice", "difficulty": "Hard", "attempts": 7},
    {"name": "Bob", "difficulty": "Easy", "attempts": 3},
    {"name": "Charlie", "difficulty": "Medium", "attempts": 5},
    {"name": "David", "difficulty": "Easy", "attempts": 2}
]
Enter fullscreen mode Exit fullscreen mode

Explanation

  • leaderboard is a list
  • Each player is stored as a dictionary
  • Keys:

    • name → player name
    • difficulty → level
    • attempts → number of tries

Step 2: Define Difficulty Order

order = {"Easy": 1, "Medium": 2, "Hard": 3}
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Difficulty is text, so we convert it into numbers
  • Easy = 1, Medium = 2, Hard = 3
  • This helps in comparison during sorting

Step 3: Selection Sort Code

n = len(leaderboard)

for i in range(n):
    min_index = i

    for j in range(i + 1, n):
        if (order[leaderboard[j]["difficulty"]], leaderboard[j]["attempts"]) < \
           (order[leaderboard[min_index]["difficulty"]], leaderboard[min_index]["attempts"]):
            min_index = j

    leaderboard[i], leaderboard[min_index] = leaderboard[min_index], leaderboard[i]
Enter fullscreen mode Exit fullscreen mode

Line-by-Line Explanation

Line 1

n = len(leaderboard)
Enter fullscreen mode Exit fullscreen mode
  • Finds the total number of players

Line 2

for i in range(n):
Enter fullscreen mode Exit fullscreen mode
  • Outer loop
  • Moves from first position to last

Line 3

min_index = i
Enter fullscreen mode Exit fullscreen mode
  • Assumes current position has the smallest element

Line 4

for j in range(i + 1, n):
Enter fullscreen mode Exit fullscreen mode
  • Inner loop
  • Checks remaining elements

Line 5 (Important)

if (order[leaderboard[j]["difficulty"]], leaderboard[j]["attempts"]) < (order[leaderboard[min_index]["difficulty"]], leaderboard[min_index]["attempts"]):
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Compares two players
  • First compares difficulty
  • Then compares attempts

So:

  • Easy comes before Medium
  • If same → fewer attempts wins

Line 6

min_index = j
Enter fullscreen mode Exit fullscreen mode
  • Updates the index of the smallest player

Line 7

leaderboard[i], leaderboard[min_index] = leaderboard[min_index], leaderboard[i]
Enter fullscreen mode Exit fullscreen mode
  • Swaps the players
  • Places the smallest element in correct position

Step 4: Display Sorted Leaderboard

for player in leaderboard:
    print(player)
Enter fullscreen mode Exit fullscreen mode

Explanation

  • Loops through sorted list
  • Prints each player

Output

{'name': 'David', 'difficulty': 'Easy', 'attempts': 2}
{'name': 'Bob', 'difficulty': 'Easy', 'attempts': 3}
{'name': 'Charlie', 'difficulty': 'Medium', 'attempts': 5}
{'name': 'Alice', 'difficulty': 'Hard', 'attempts': 7}
Enter fullscreen mode Exit fullscreen mode

Conclusion

This project uses:

  • Basic data structures (list and dictionary)
  • Selection Sort for ordering

Selection Sort is simple to understand and works well for small datasets like this leaderboard.


Top comments (0)