How can I add networking to the game I made in GridWorld?

264 views Asked by At

(GridWorld as in Collegeboard's AP exam case study thing)

So, I've been working on this game during my free time for a few years now, and it's coming along pretty nicely. I have a nice lil' stick character that runs around in a grid and picks up items and fights monsters and stuff. Now, I think I'm ready to take it to the next level (for as much of a choice as I have anyway, since I kind of need to make a project for my networks class, hah...), and I'm a bit stumped on how to progress.

I've made a few programs in the past using ServerSocket, where I have one Server running and several Clients can connect to it and receive messages, but they have all been simple text programs that don't exchange more than a few Strings. Sockets are what I was thinking of using for this, but I'm not sure how to set it up...

Now, when GridWorld is run, it automagically pops up what looks like a JFrame, which contains the grid and all actors in it. In the main source file that contains all the code for the, uhh, "engine", if you will, there are basically 2 giant methods: step(), which controls what happens per every unit of time, and keyPressed(), which controls what happens when the user pushes a key. Now, I was thinking that step() for sure needs to be on the server side, and keyPressed() should be on the client side, along with the window (of course—otherwise how will the user be able to see what they're playing?) The idea is to have the main game that governs all the NPCs' actions and game calculations to be running only once, on the server, and for multiple clients to be able to connect to it and each have their own character in the game (basically like a mmorpg).

But like, how exactly would I split it up? (Or if that's even the right way to go?) I'm not even sure on how to pass data back and forth between the server and client(s)—for my simple text programs that I've mentioned, I've always used PrintWriter and BufferedReader. I found that PrintWriter does have a print() method that accepts any Object, but then how would the BufferedReader receive it? It doesn't have any method that will read an Object.

So basically, I want to know all the specifics of exactly what the server should do, what the client should do, which libraries I should use, and where to put certain functionality.

2

There are 2 answers

0
Pojo On BEST ANSWER

All right, I've settled on using Sockets and doing the Serialization stuff myself with ObjectOutputStreams and stuff. I've figured out where to put everything, and it's basically working fine, though I still have the inevitable glitches in the game itself that I'll need to hammer out. Thanks, Tim :)

0
Tim On

Based on the design you currently have, it looks like you probably want to use something like RMI for the communication between your client and server. That will allow you to focus on how to call methods between the client and server without having to worry too much about managing network communications.

RMI might not be what you'd want to use long term, but it's probably the right next step for getting your client up and running.


As for streams - PrintWriter.print(Object) just writes out the String representation - i.e. it calls toString() - of the object, so there is no way to turn it back into an object.

For that, you want ObjectOutputStream and Java seralization. But RMI will do this for you without you needing to get too far into the details.