Message Handling Help in Battleship game

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

Message Handling Help in Battleship game

Postby ridha121 » Fri Jan 02, 2009 2:07 am

Hi Guys
I am in the process of creating a simple battleship game to be played between two devices. At the moment the devices are connect and send message by connecting on a particular socket and through the use of inputstreams and outputstreams they exchange messages. I have the following classes

Battleship (main class), Sender (carries out sending) Board (Paints simple board class), Client (uses sender class and board class for the grid and is generated by battleship) and Server (acts as server carries out same actions as client.

The way I have implemented the battleship is through the use of hashmaps. I am finding it difficult to actually exchange the messages between the server/client. The run() method is in both my client and server below and as you can see here is where the streams are initialised and the handlings of data is carried out. My problem is that I am using hashmaps for each of my commands to fire at ships etc and its constructer requires two variables when sending data. 1) a String value and 2) an Integer hence representing my co-ordinates for the board. The fire() method is below.
My question is how can I integrate the hashtable requests to work with my run() method below.

Thank you very much, you help is greatly appreciated


Code: Select all
public void run() {
try
{
sc = (SocketConnection)
Connector.open("socket://localhost:5000");
si.setText("Connected to server");
is = sc.openInputStream();
os = sc.openOutputStream();
// Start the thread for sending messages - see Sender's main

// Create thread to do sending of messages
            sender = new Sender(os);
// Loop forever, receiving data - sending of data
while (true)
{
StringBuffer sb = new StringBuffer();
int c = 0;
while (((c = is.read()) != '\n') && (c != -1))
{
sb.append((char) c);
}
if (c == -1)
{
break;
}
// Display message to user
si.setText("Message received - " + sb.toString());
}
stop();
si.setText("Connection closed");
f.removeCommand(sendCommand);
} catch (ConnectionNotFoundException cnfe)
{
Alert a = new Alert("Client", "Please run Server MIDlet first",
null, AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
} catch (IOException ioe)
{
if (!stop)
{
ioe.printStackTrace();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}


Code: Select all
public void Fire() {
      Hashtable request = new Hashtable();
      request.put("own", own.getString());
      request.put("opponent", opponent.getString());
      request.put("form", "turn");
      request.put("fire", "");
      connect("turn.faces", request);
   }
ridha121
 
Posts: 3
Joined: Fri Jan 02, 2009 2:03 am

Re: Message Handling Help in Battleship game

Postby DevelopmentTeam » Fri Jan 02, 2009 5:31 am

Bit confused about why you really need a hash table for this game. Usually we encode the data in a simple XML format or sometimes our own simple protocols in binary format to send and receive information from and to the server.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Message Handling Help in Battleship game

Postby ridha121 » Fri Jan 02, 2009 12:00 pm

^ This is because Im not actually implementing on an actual server. I will only be running as simulation between two mobile phones and the application will run between the two phones. This is writen in J2ME.

Thank you
ridha121
 
Posts: 3
Joined: Fri Jan 02, 2009 2:03 am

Re: Message Handling Help in Battleship game

Postby ridha121 » Mon Jan 05, 2009 11:53 am

hi

I would greatly appreciate any help please :)

Kind Regards
ridha121
 
Posts: 3
Joined: Fri Jan 02, 2009 2:03 am


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 1 guest

cron