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?
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?
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:
This gives a reliable, simple design you can implement in C or any language; choose file vs SQLite based on concurrency needs.
Jump to Post— red_evolve 1Greetings.
Would you like to store them in a data file or a database?
Greetings.
Would you like to store them in a data file or a database?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.