I am attempting to make a four-player chess game over the internet. So far, I have these classes:
MultiPlayerServer
MultiPlayerServerThread
Client
The MultiPlayerServer
makes a port to listen and whenever a Client
starts listening, it gives the responsibility to the MultiPlayerServerThread
to handle the request, but here comes my question: I wish to have a single class handle the state of the game, do I make a single static class (if so, where might it go?). Beyond this, the client will need a lot of work because the server will be giving the client data as where to draw the pawns and the rest of the implementation.
TL;DR If I was to make a multiplayer online 4 player chess game with a server class, server thread class, and client class, where might be a good place to make a single copy of the game, so that if multiple people join, they all join the same game and not create games for themselves.
The MultiPlayerServer should "own" an instance of the game state -- this is common to all players. The MultiPlayerServerThread class (which abstracts a connection to a client) contains a reference to this common object.
When a client makes a move, this common game state is accessed and, if the move is legal, mutated.
Since each client runs in a separate thread, you generally need synchronization to make sure that two clients don't modify the state at exactly the same time. With chess this is less of a problem since only one player is allowed by the rules to modify the game state at a time -- but be careful to introduce this if you extend the "game state" to include something like a chat log.
Note that you may want to design this keeping in mind that you may later want the MultiPlayerServer to keep track of several GameState objects. This would allow your chess server to keep multiple games of chess running at the same time. You'd need additional logic to keep track of which players are playing in which games.