Could someone give me a hint, an algorithm or a source code if you like.... for this program...

I need to display and store the highest scorers in my Game.... How is that?

Dani AI

Generated

asked for an algorithm to store and display high scores; rightly asked whether you want a data file or a database; linked an unrelated thread.

Two practical, dependable approaches asked for an algorithm to store and display high scores; rightly asked whether you want a data file or a database; linked an unrelated thread.

Two practical, dependable approaches:

  • Keep a small in-memory sorted list (best for local/single-player games): maintain an array/vector of the top N entries sorted by score (highest first). When a new score arrives, if the list has fewer than N entries insert it in the correct position; otherwise compare with the Nth entry and insert only if higher, dropping the last. For small N this is O(N) per insert and trivial to implement.

  • Use persistent storage: for a simple file, use a plain CSV or JSON file and perform an atomic update (write to a temp file, then rename) and simple file locking to avoid corruption. For multi-user or online games prefer a small embedded DB (SQLite) to get ACID behavior, concurrency, and indexed queries without a separate server.

Minimal pseudocode (conceptual):

onNewScore(name, score):
  if list.size < N or score > list[N-1].score:
    insert into list at correct sorted position
    if list.size > N: remove last
    persistList()

Notes and cautions:

  • Include unique tie breakers (timestamp or ID) so identical scores sort deterministically.
  • Limit name length and sanitize input to avoid file/DB injection.
  • Use atomic file replace or a DB transaction to avoid corrupting the high-score store if the app crashes during write.
  • If scores come from clients, validate server-side to prevent cheating; do not trust client-submitted scores for competitive leaderboards.

This gives a reliable, simple design you can implement in C or any language; choose file vs SQLite based on concurrency needs.

Recommended Answers

All 2 Replies

Greetings.
Would you like to store them in a data file or a database?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.