Writing a tetris game -- How to implement the network?

Hey,

This is a really general question, because I don’t know enough to ask a pointed one.

I’m working on writing a tetris that’s playable over a network. I’ve written up a really nice single player program, but I’m having trouble coming up with a design scheme for network play.

I really just don’t know what tools to use. I’ve got O’Rielly’s “Learning Java”, and I’m playing with RMI, but I really don’t know where to start.

Depending on what the user is doing, I’m rendering the screen every 12-26ms.

How would I go about getting two game running together over the network?

http://joshman.ath.cx:81/bestris.png

My goal is to have it so the local-player controls the game on the left side in that image while the remote-player controls the right side of that screen. Idealy there would be no difference in gameplay if the two players are playing from the same computer (controlling it from the same keyboard) or over the network. I would like to render the block falling just as I would if it was all local.

I’m sorry if this is really vague, but I just don’t know much about network programming.

Is this a LAN game or an Internet game? The techniques will vary substantially dependign on which it is.

If it is a LAN game then you have effectively 0 net latency and you cna do it lock-step. This means that both computers run both right and left games and you just send the controls across to the other player.

This will never work for an internet game howevre, the latency will cause the foreign keystrokles to arrive much too late.

Instead, in thsi case, you want the “foreign” game to really be a s.lightly time-delayed view. You show the results of the foreign game player’s actions rather then actually trying to run the remote game locally.

first of all, what is gameplay interaction between two boards? or it’s just than one player can see the progress of second one?
In O’Reilly’s Java NIO the author is converting multiplayer tetris from regular IO stryle to NIO :slight_smile: There when you complete a row(s) opponent gets row(s) of random garbage.

Anyway don’t know much about networking, just started, my initial idea, p2p or client-server:
You connect 2 pcs, server sends start msg and his inital falling objects (first two, first one to start falling and second one that is next to fall) and client responds with what will his first two falling objects be.
Game engine starts to process these, each in own seperate board, on both sides, but localy for opponents board dosen’t calculate any points or whatever, just the visual process of falling and so on.
When local player changes rotation of falling object or speeds it down you send a message what happened to other player. Other player receives it and his game engine process it.
When falling object falls localy you send a message of status of your board (what squares are filled). Other player receives it, his game engine process it. Now your opponent board and his local board should be the same. If object made filled a row game engine should process it exactly on your and opponents board, since you have info how the objects fell exactly.
You create new random falling object and send message to other player what it is, and other player renders it… and process starts again.

Note this is just my opinion, I neved created a single network game but I’m working on one right now :wink:
Also in this case it’s very important that message that signals status of board gets received… if you lose a message about rotation, no biggie, you’ll just won’t see how opponent rotated object when falling, but if you lose status message, boards won’t be syncronized until next status message comes (when opponent places next falling object).

I hope it’s sounds clear what I said.

Internet.

Should I be using RMI or just some protocol I made up over sockets?

I’ve been told RMI can do the job and has high performance and I tried looking into it myself but I found it too hard too get a grip of… stubs, remote objects…

As far as I understand RMI is limited to TCP (rather than UDP or a combo of both) and it may be a bit too high-level, not giving you the fine-grained control that you’d want to have in a game. The danger of coding it in RMI would be that its stuck in RMI. I’d recommend sending your own byte packets using your own sockets so you can change between UDP and TCP as you see fit.

There’s a good, active project called JGN (http://www.java-gaming.org/forums/index.php?topic=12384.0) that has that network communication ready-made, among other libraries.

I’ve also developed a Serialization-like way of doing a network game (http://www.java-gaming.org/forums/index.php?topic=11531.0) but there’s no tutorial yet and my website’s down so you won’t be able to get the code (but I’m working on it)…

Keith

I would not use RMI. Lots of over-head and very little gain for this sort of thing.

Id just use ByteBuffers and build your own packets.

It does sound clear, thanks. This was the general scheme I had in mind. I’m happy that it’s something that O’Rielly thinks would work.

on second thought… maybe it’s not from O’Reilly… I read many tutorials lately so got things messed up. If you’re interested exactly about it message me and I’ll look it up.
And you got it wrong, the networking plan is all from me (so it’s filled with holes probably :slight_smile: ), what I picked up from that tutorial is just that when you complete a row you put row of random garbage to your opponent.

Awesome. Either way, knowing someone else came up w/ the same general idea I did gives me alot more confidence that it’s a good idea. :slight_smile:

I would like the name of that book/tutorial too, if you can find it.

Thanks!

click here, then go to part 3