Writing a small battleship game server in Python


Michael Goettsche

Hi there,

for a project in our computer science lessons at school we decided to write a
client/server based battleship like game [1].
I know this game could be written without a server, but the whole project is
for educational purposes.

Being the initiator of this project, I thought I would write a skeleton for it
before we actually start with it. The client thing won't be too hard, what
I'm having problems with is the server, maybe because I have problems
understanding what's the best way to accept connections and handle existing

So, here is my problem:

The server should accept connections from new players and be able to handle
multiple games concurrently. The main purpose of the server will be to make
sure the clients follow the rules(only shoot once, shoot only existing
fields, ...) and inform clients about game end etc..

What would be a good, but still easy way to write such a server?
In a socket tutorial [2] I read the usual way to write a server is to create a
server socket and then listen for connections in an infinite loop. I
understand that part, but how should I handle connections then?
Start a thread for each game and put every two new players in it?
Or does Python offer a better solution to do this?
Twisted can be a solution too, but this server should run on standard python
installations and Twisted might be a bit too heavy for our project.

Thank you.
Best Regards,
Michael Goettsche.

[1]: For those who don't know the game:
It's played by two players. Each player has a game board where he places a
predefined number of ships. After placing the ship, the players try to shoot
the enemy's ships (but they don't know where they are). The game is
round-based, so player A may shoot once, then it's player B's turn and then A
again and so on until one player has shot all ships of the enemy.

[2]: http://www.amk.ca/python/howto/sockets/

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Latest member

Latest Threads