I am working on a snake game. I have posted about it before. It is going well, and now I would like to implement a high score option, that saves the score online, so that everyone can see it. I am planning to make a menu, that will have a high score button. It would then open a new frame displaying the high scores (most likely top 10 or 15). How would I go about doing this. I know about the HighScore method in Java, but that saves it to the computer. I have also read this forum post: http://www.daniweb.com/software-development/java/threads/104026 for some information, but this user was also saving it to the computer.

Thanks for any help.

DO NOT post on the listed thread. It is an old thread.

Recommended Answers

All 18 Replies

You'd need to create some online service to store and retrieve the scores.
A simple REST web service might be ideal for this, with just 2 commands: "GET" command to get a list of high scores (or optionally with a parameter to get the score for a specific player) and a "POST" to store a score.

Of course that's vulnerable to people reporting unrealistic scores or overriding other peoples' scores. To prevent that you'd have to start adding security and sanity checks (and to prevent incorrect reporting you'd also have to do the entire score generation on the server, making the entire game run serverside with the part on the user machines just being a user interface, the way it works with mmos).

would it be possible to open a URL connection to a class file located on a server and the send the info to that class file (score, version of game, name, etc.) and then have that class file add the score to a highscore file (with the .highscore ending). and then to retrieve it use another class in charge with retrieving the scores? Or would there be errors because of the security limitations of the internet (the same as applets)?

If you're not using an applet (ie using a standard desktop app) then the applet restrictions don't apply. What you describe is straightforward. The hardest bit is how the clients get the server's IP address (unless you're happy to hard-code something). jwenting's security issues are still very real.

I recommend using a simple MySQL database. You can find MySQL libs online for free and either rewrite one or find one with a lenient license. You just need a online MySQL database, this can be solved with a web host, or install MySQL on a local comp thats on 24/7, port forward, and either get a static IP (may not be possible) or use a Dynamic DNS host such as DynDNS or NoIP or some other service, Google is thy friend!

What I was suggesting with my previous post is building an applet that runs on the server (that is called when updating scores) and then rewriting the .highscores file. And then making another applet that can read the highscores and then return the values.

Terminology alert: Applets (by definition) run in a browser. You want an ordinary Java application on the server. You can use a single application for updating and for returning the values. It's easier that way. (no problems with sharing the file, no need to re-read the file to return current values)

So the file restrictions don't apply when I have my application on a server rewriting a file? Also how would I communicate between these classes? Would I use a URL connection? If so how would I call the class using that URL?

Your server application can do whatever it likes with its file(s) - subject to any restrictions imposed via the operating system.
Your server program opens a ServerSocket on a known port and waits for incoming connections. The client makes a Socket connection to the server's IP address and listening port.
Once the connection is made you open IO streams both ways and the client and server can then send whatever they like to each other.
You'll find lots of sample code on the web, but once you've got the basic code right it's all pretty easy.

A MySQL database would be the easiest solution guys...... you would only then need to write or obtain a MySQL lib (There are many out there) and only write the client..... no need to write a whole server app. Also obtaining a server capable of running an executable is harder and more expensive than obtaining a simple web server with a MySQL database.

Hi Arrorn. Yes, you certainly could do this app with a MySQL server, and the database forum would be the right place to discuss that. This is the Java forum, so we're discussing a Java solution here.

obtaining a server capable of running an executable is harder and more expensive than ...

A Java server application using ServerSocket will run an any PC that supports Java and has TCP/IP. Your comment doesn't seem to make any sense.

A MySQL database would be the easiest solution guys...... you would only then need to write or obtain a MySQL lib (There are many out there) and only write the client..... no need to write a whole server app. Also obtaining a server capable of running an executable is harder and more expensive than obtaining a simple web server with a MySQL database.

not necessarilly. What he wants doesn't require the resources of a relational database, it's far too simple for that.
A servlet writing to a text file will do the trick, or a php script, or whatever he can run at his chosen host.

Relational databases are far too heavy for such small solutions (and for many larger ones that they're pushed into, tbh).

@jamescherill: He'd still need to program access to the MySQL database in Java so it is a Java solution so it is an appropriate comment for this forum...

@jwenting: I have made several relational databases and they are extremely simple. create database, add user, make table done. As I said before finding a MySQL lib and using it to store and retrieve high scores would be the least coding involved.
Also, as I said before finding a server capable of running an executable for a java server is far more expensive and harder than a simple web hosting that includes a MySQL database.
As for the PHP script, that would also be an easy thing to use. In PHP you could wright a hard file (.txt, so on) or use its built in database libraries. The problem is that again you would need to find a web host that allows the upload of files and the executing of a php script. Most of those already come with a MySQL database.

To summarize available options from my perspective:

Java Server Application:
    Pros
        Java purist solution
        Simple as programs go - Only needs to read/write and send/receive scores
    Cons
        Needs server with access to internet running 24/7 with ability to run executables - expensive if renting, simple if have access to one

Webbased Script
    Pros
        Again fairly simple program wise - read & print/write or using built in database libraries
        Java Program only needs to access a URL and get a text response
    Cons
        Need to know another programming language - PHP, ASP, Etc. and possibly relational databases
        Need a server that allows writing to disc - Don't know if free web hosting services allow this. Most paid web hosting services allow this and are fairly cheap compared to a service that allows running of an executable. OR needs a server with webhosting as well as a relational database such as MySQL. Not many free ones of these though, most paid webhosting services come with a MySQL database or other database and the ability to run server side scripts.

Relational database such as MySQL
    Pros
        Again easy to program if can find a free Library with lenient license
        Only need a MySQL database or relational Database not a full web hosting service
    Cons
        Need to know how relational databases work to set one up (Get someone to do it for you)
        Need a MySQL or other relational database server or hosting.

If I missed something comment and I'll add it.

Hi Arraon
You make good points and a good contribution to the discussion.
I don't think the OP is planning a commercial service, I assume it's "just" a learning exercise, so maybe the criteria are simpler...

It is just for learning. I have a free download to my snake game, and I just want to learn a way to make an online high scores list. I have decided to go with a free account from 000webhost (supports MySQL). My though with the application one would be to have each version have its own lets say "Serial code" which it would send, then the application would sort which high score list to look at. That way I could operate multiple high score lists with one application, while each of them would still be separate.

If I were to make a high score list using MySQL (because I know my server Supports it) where would I start?

As stated by a few other posters above I'd go with MySQL as well for this. It is fairly straightforward and you dont have to develop a whole server application that has to be very careful with it's resources (probably reading/writing the highscores to a file). Two clients trying to read and write to the highscore file at the same time can, if you are unlucky and dont protect yourself against it, end up with very weird results. It's just too much work for the task you want to, unless you see it as something interesting that you want to learn of course then by all means go ahead.

I wouldn't mind spending time with it, but it would be better to start with the easier option and then progress to the application. I have decided to go with the MySQL. Again I will ask, where would I start if I wanted to make a high score list using MySQL. Could you please link me to tutorials?

Thanks

Here is a good starting point: http://download.oracle.com/javase/tutorial/jdbc/index.html

Edit: Maybe this is a bit extensive if you already know how to work with MySQL and just dont know how to use it with Java. However, you will find useful info there.

Start with the MySQL documentation on using their JDBC driver. They have several examples to get you started.

Edit: Cross-posted with Slimmy. Be sure to check the link he posted as well for a more general tutorial on JDBC.

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.