JoBattleShips

 

JoBattleShips is a game based on a threaded TCP server . At this moment the game don’t show too impressive because the propose of the project was to create a TCP Client-Server application.The server spawns a thread for each client and can, in theory, accept as many connections as you want (although in practice this is limited because you can only spawn so many threads before Windows will get upset).The game is a PVP (1 vs 1) so you can compete agains your friend or agains another gamer.

How does the game works ?

Basically once you are connected to a server you can create a room or you can join into one.Once you are in a room your status is set to „InWaiting” and anytime another player can connect with you and start game. After the game start , you are supposed to pick 3 fields and prepare yourself. The other player do same thing. Once the fight field is set, the battle can begin. The battle consist in hitting the opponent fields and when you hit all 3 fields that he pick the game is over and you win. 

Select fieldsBattle Preview

As you can see in picture one, players must select 3 fields and be ready. In picture two the fight begin, and it’s seems that test will gonna win the game (of course, if he will hit the last field).

How does the server works ?

As I said above when the TCPListener is fired up the server spawns a thread for each client and it’s ready to listen his commands. The commands are abstractized and have certain features:

  • Each command starts with „#”;
  • Each commnad are fit in „< >”;
  • Each command have a name thats can be found in a dictionary of command names;
  • Each command can have an argument and a message.

A typical command looks like #<command:argument> message;

Before the command reach the server , the command is passed through a command parser then send to server. Once server retrieve the command he looks for it in a dictionary of command names and if the command is found the server formulate a response and send it to a response parser and write it out on the client side.

The response also have a certain features:

  • Each response starts with „@”;
  • Each response are fit in „< >”;
  • Each response have a name thats can be found in a dictionary of response names;
  • Each command can have an argument and a message.

A typical response looks like @<response:argument> message;

In the picture below you can see a scheme that explain how does the server works.

JoBattleShips -> TCPServer-Client Scheme


List of commands

  • "login"
  • "loginsuccessful"
  • "register"
  • "createroom"
  • "searchroom"
  • "selectfield"
  • "exitgame"
  • "hitfield"
  • "surrender"

Last edited Jan 30, 2013 at 11:40 AM by Meseriasul, version 43