-1

I am designing a simple chess game in java and want to add network(client and server model) to
to it. One player is on server side and the other on client side. Right now I hava a server.java client.java and game.java.

game.java is included in both server side and client side(each player has a game instance). My problem is that I don't know when to send data between two sides. I add listener to the game class but the server client seems cannot get the click event inside the game class. One way is that I use busy waiting to continuously check that whether there is an update. But is there a way to check update without using busy waiting?

Cherish
  • 187
  • 1
  • 2
  • 13
  • Think of it like a ping-pong match. You send the data (ping) to the server when you want to send information, they send data back (pong) when they want to send you information. – Makoto Feb 13 '13 at 05:22
  • The standard SO assisted way to solve something like this is to first use google, write some code, then when stumped, post the code at SO... Btw, consider using some ready-made lib such as some XMLRPC implementation instead of rolling your own, if this is not a network programming learning exercise. – hyde Feb 13 '13 at 05:26

2 Answers2

1

The standard way to design this game would be to have have two instances of your client class (one for each player) talking to a central server which coordinates the game. Each client has its own representation of the board in memory, but its up to the server to make sure that the moves are relayed from one client to another, that the selected move is valid, to keep score, to determine when the game is over, etc. Basically, the server is the controller/coordinator/referee.

Cody A. Ray
  • 5,869
  • 1
  • 37
  • 31
0

Basically, you need to set up some kind of "token" that dictates which player can move. The player with the token is the only player that can make a valid move and will send that data back to the other player, along with the token. The first player would then be put into a "wait" state...

The player without the token would be waiting for a response from the other player. This can easily be established by simply reading from the socket's input stream.

When the active player makes a move, that move (and thus the token) is send to the waiting player. The active player then enters a wait state, reading the there socket's input stream. The player that has just become active would be notified of the the update and would be allow to make a move. And around it goes...

I would have a call back mechanism from your "network engine" that would notify the "game engine" of network events, such as "move received", "network failure" etc.

I would have a simple method in the "network engine" that would allow me to send data of the move back to the other player. If coded correctly, the "network engine" could block requests from the "game engine" until it was the players turn to move. Equally, you could have checks to determine the players current state as well.

Just some random thoughts :P

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Reaaaaally appreciate for ur nice reply. But as I stated, how do I know when to send the token? How do the server class know that there is a move in the game class? Is there a listener mechanism? – Cherish Feb 13 '13 at 06:12
  • No. Basically, as I said. One side would be waiting for something to be written to the socket. While it's waiting, you would consider that player to be inactive. When the active player writes something to the socket, then the inactive player would receive. If I get some time, I'll do a simple example... – MadProgrammer Feb 13 '13 at 06:28