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);
}
