Java Client/Server turn based help

谁说我不能喝 提交于 2019-12-09 04:35:32

In my opinion the best strategy to approach a turn based game such as this, is to decide on some basic architectural approaches. Diagram out the componenents and some basic game flow diagrams.

You should put the bulk of the game engine logic in the server component. The clients should be kept as thin as possible, focusing primarily on

  1. Communication with the game engine
  2. Accepting user inputs
  3. Interpreting game engine responses
  4. Drawing the screens

Your server/game engine should be relatively stateless, yet maintain a list of current game sessions in play. Stateful SOAP web services or even HTTP Servlets would be a good choice because they maintain session for you by placing and reading session cookies in the request.

Everything web works on request response so it is by nature stateless, but certain technologies like Java servlets will help you maintain sessions so that you don't have to. No need for physically creating seperate threads, each request causes the application server to spawn a new thread of execution, while the session by nature is volatile.

On the server side I would persist all data for a particular active game in the session. In this way, your game engine will maintain the orderly communication between the two players.

  1. Player 1 sends end of turn request with all of the game state change information.
  2. Game engine interprets request, makes necessary changes to the game state.
  3. Player 2 sends frequent requests to check and see if it is Player 2s turn yet.
  4. Game engine acknowledges Player 2 request for its turn and sends the new game state in response.
  5. Player 2 receives the response, updates its copy of the game state making note of the changes since its last turn.
  6. Rinse and repeat.

You just use the server as a middle-man.

  1. Client A sends (writes) message to Server with attribute denoting Client B as destination
  2. Server receives (reads) the message and forwards (writes) the message to Client B
  3. Client B receives (reads) the message.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!