views:

111

answers:

1
+1  Q: 

C CLI game concept

So I just wanted to get some opinions on the overall structure of a game I have to build for a programming class.

Essentially - I'm building two programs - a client and a server for a battleships game. I've already written the actual program which plays the battleships game. The program I've written is where a map and rules file is read in and the user tries to guess the location of the ships until they sink all of the ships.

For this new program - the server has to allow groups of two people to vs each other using the battleships game. Clients connect to the game using a game name, and once the game receives two clients, it can begin. We're required to use TCP on the local machine to communicate between server and clients, as well as threads in at least the server.

Here's what I was thinking..

Once a client joins - check which game they want to connect to. If it is a new game - create a new thread for that game and give the socket descriptor for the first client. Otherwise, if there is already one person in the game, alert the game thread of the new participant and it can start the game.

When a game is started - the game thread would have to spawn two more threads.. one instance of the battleship game for each player. The instance for each player will be where their ships are placed. The thread will then listen for input from each client in turn and pass that information onto the respective game.

Is this interpretation correct? That I'll need 3 threads per running game? And also, is it possible to use exec() in a thread to run a new battleships instance and then pass information to the stdin of that instance using threads (and get the stdout)?

Please let me know if I've been hazy on anything and I'll clarify. Thanks a lot in advance for any help!

+1  A: 

I would use as many threads as connected clients and use them only for handling connections. Games themselves are just data/objects and need no specific thread.

mouviciel
But how would I actually run the game from the program. I'd need to use one of the exec() functions somehow to begin the battleship program, no?
Gary
Think MVC pattern. The connections are the views, the games are the models. You need a controller for connecting views and models but it can run from the main thread and control many games and connections.
mouviciel
@mouviciel I have to disagree. I tried this before and it failed. What happens when you have 1000 connections? This means oppening 1000 threads and thus it will slow down everything. But that's just my oppinion
Sanctus2099
@Sanctus2099: My suggestion aims at replacing 3 threads by only two for each game. What solution did you find for your performance issue? This might be helpful for the OP.
mouviciel