Up Previous Next

When a client is created it should have a position (x, y) as in (column, row) on the board.

  int x, y;
  public Client(int columns, int rows, int x, int y) {
    this.x = x * cellWidth; 
    this.y = y * cellHeight; 
    this.setTitle("iceblox: client frame"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    this.setSize(columns * cellWidth + cellWidth / 2, (1 + rows) * cellHeight + cellHeight / 2); 
    getContentPane().add(new Surface(columns, rows), "Center"); 
    addKeyListener(this); 
    this.setVisible(true); 
  }
See how the main in Client needs to accomodate this change, a bit later, below.

Each client should keep track of where the other clients are:

Penguin[] players = new Penguin[100]; 

public void update(int player, String action) {
  System.out.println("Player " + this.id + " updated by player " + player + " with action: " + action); 
  players[player].perform(action); 
}
So there is a class, Penguin:
import java.awt.*; 

public class Penguin {
  public Penguin(int x, int y, int width, int height, String owner) {
    
  } 
} 
The question then is, how does a player first gets placed in the frame (as a Penguin):

  Penguin[] players = new Penguin[100]; 

  public void update(int player, String action) {
    System.out.println("Player " + this.id + " updated by player " + player + " with action: " + action); 
    if (players[player] == null) {
      players[player] = new Penguin(x, y, 30, 30, "(" + this.id + ", " + player + ")"); 
      players[player].placeIn(this);
    } else { 
      players[player].perform(action); 
    } 
  }
This means Penguin must keep up:
import java.awt.*; 

public class Penguin {
  public Penguin(int x, int y, int width, int height, String owner) {
    
  } 
  public void placeIn(Client location) {

  }
  public void perform(String action) {

  } 
} 
The main in Client initiates the sequence of initializations:
  public static void main(String[] args) {
    try {
        ServerInterface far = (ServerInterface)Naming.lookup("rmi://" + args[0] +  ":" + args[1] + "/Dirac"); 
        int cols = 10, rows = 10; 
        Client here = new Client(cols, rows, (int)(Math.random() * cols), (int)(Math.random() * rows)); 
        UnicastRemoteObject.exportObject(here); 
        here.registerWith(far); 
        far.broadcast(here.id, here.x, here.y, "happy to be here"); 
    } catch (Exception e) {
        System.out.println("Error in client..." + e); 
        e.printStackTrace();
    }
Notice that the Server's broadcast method changes a bit:
  public void broadcast(int id, int x, int y, String action) throws RemoteException {
    for (int i = 0; i <= size; i++) {
      clients[i].update(id, action); 
    } 
  } 
This change needs to propagate to the server's business card:
import java.rmi.*; 

public interface ServerInterface extends Remote {
  public int register(ClientInterface client) throws RemoteException;
  public void broadcast(int name, int x, int y, String action) throws RemoteException; 
} 
And, eventually, the use of broadcast in the client needs to be updated:
  public void keyPressed(KeyEvent e) {
    int val = e.getKeyCode(); 
    try {
	if      (val == 38) server.broadcast(id, x, y, "up"); 
	else if (val == 39) server.broadcast(id, x, y, "right"); 
	else if (val == 40) server.broadcast(id, x, y, "down"); 
	else if (val == 37) server.broadcast(id, x, y, "left"); 
    } catch (Exception exception) {
      System.out.println("Glitch: " + exception); 
    }  
  } 
The last change we need to make:
import java.rmi.*; 

class Setup {
  public static void main(String[] args) throws RemoteException{

    Server server = new Server(); 

    int cols = 10, rows = 10; 

    Client a = new Client(cols, rows, 0, 1); 
    a.registerWith(server); 
    server.broadcast(a.id, a.x, a.y, "happy to be here");

    Client b = new Client(cols, rows, 2, 3); 
    b.registerWith(server); 
    server.broadcast(b.id, b.x, b.y, "happy to be here"); 

    Client c = new Client(cols, rows, 4, 5); 
    c.registerWith(server); 
    server.broadcast(c.id, c.x, c.y, "happy to be here"); 

  } 
}
At this point we can compile:
javac *.java
The changes are all consistent, compilation proceeds without error or warning.

What do we do next? (There's more than one thing we can do now, so which do we choose first?)


Up Previous Next