- Ships may be placed beside each other, but may NOT overlap
- Each player has five ships (2, 3, 3, 4, 5)
- Each player takes one shot per turn (no streaks)
After arranging their ships, player 1 sends their targeted square (e.g.: A4, C6, F2) encoded in bytes to the opponent and waits to recieve a response. Player 2 recieves their targeted sqaure and sends back a boolean of whether a ship is on the targeted square encoded in bytes. The same steps happen again, but the order swaps and the player 2 sends their targeted square to player 1. Use Convert.java to convert from boolean and (row, column) to bytes.
Value | Bytes |
---|---|
false | 0 |
true | 1 |
(0, 0) | {0, 0} |
DatagramSocket socket = new DatagramSocket(PORT);
byte[] buffer = new byte[512];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
System.out.println(new String(buffer, 0, buffer.length));
DatagramSocket socket = new DatagramSocket();
String str = "hello world";
byte[] buffer = str.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ADDRESS, PORT);
socket.send(packet);