Class Structure for Game

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
I am working on a game.

I currently have a multi-threaded socket server that can be connected to and communicated with.

I am now getting ready to implement the game logic, but am a little tripped up by how I should compose and arrange my classes.

My main classes are :

Server
->Serves Connections
ServerConnection extends Thread
->receives and sends messages to clients connected to server
MessageBuilder
-> builds messages to be sent back over the ServerConnection to a player
based on game data
GameController
-> controls flow of game


I am wondering if I should change this to make the GameController extend thread instead of the ServerConnection and have ServerConnection be a member of the GameController.

I need 1 instance of the Game Controller per player who connects to the Server. This is not a multi-player game, but a server based single player game (for the time being).

I don't think this idea will work though, because the run method of my ServerConnection object that extends thread is where the messages are received from the client...

Any guidance would be greatly appreciated.
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Server connection should create a separate game engine class and set up internal knowledge/link of the client to the game class.

Recommend that you set up a thread for each instance of the class.
Server gets pinged
Server knows which client pinged him and know which game class should get the message.
Server sets up data for proper Game Class and activates thread controlling the game class
Game class pulls needed data, processed and puts data back for server to send to client
Server detects thread completed (does not need to be destroyed) and grabs data to send back to user.

Unsure what language you are using.
 

Schmide

Diamond Member
Mar 7, 2002
5,798
1,125
126
Type of game would be necessary to determine to what level you have to abstract communication.

In general, a game should have multiple levels of internal communication to prevent network and input lag. Rendering should be decoupled from Logic which should further be decoupled from Communication and Input. You should build buffers between each level and run logic at a fixed rate to ensure both fairness and consistency.
 

LumbergTech

Diamond Member
Sep 15, 2005
3,622
1
0
I am using Java for this program

The type of games are casino style games. Cards are dealt or Diced are "rolled", outcomes are and choices are communicated between the server and client.

Blackjack is one of the games, so for instance

Player places bets... (server is notified)
Timer counts down... when reaches 0 (server is notified)
Cards are dealt... (player is notified)
Player makes a decision...(server is notified)...
outcome phase where server reports to player the outcome
...repeat

Hopefully I will have multiple games available through this server..so that is definitely something to keep in mind...but I can do some refactoring later to support that just to test the basic idea.
 
Last edited:

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Reommend that instead of a timer countdown; you have a "GO" button that is labeled deal or roll.