0

I'm making a java swing app using RMI and I would like to know when the client called a method of the server so that other clients get updated. Let me be more specific, i'm making a tic tac toe game in java swing, I have the server and 2 clients, I want that when a client makes a move the other client in its board get the new move. Here's what happens when a client press a button to make a move on one client.

private void btn11ActionPerformed(java.awt.event.ActionEvent evt) {                                      
    try{
        int turno = stub.getTurno();
        int resultado = stub.tirar(1, 1);
        hacerTablero(stub.getPosiciones());
        if (resultado != 3){
            if (turno == 1){
                lblJugador.setText("Turn of player 2");
            }else{
                lblJugador.setText("Turn of player 1");
            }
            if(resultado == 1 || resultado == 2){
                if (resultado == 1){
                    if (turno == 1)
                        JOptionPane.showMessageDialog(this, "Player 1 wins");
                    else
                        JOptionPane.showMessageDialog(this, "Player 2 wins");
                } else
                    JOptionPane.showMessageDialog(this, "It's a tie");
                InterfazGato nuevo=new InterfazGato();
                nuevo.setVisible(true);
                this.dispose();
            }
        } else {
            JOptionPane.showMessageDialog(this, "This place is taken");
        }
    }catch(RemoteException | HeadlessException e){
        System.out.println(Exception from the remote method: " + e.getMessage());
    }
}

In the line

int resultado = stub.tirar(1, 1);

is where I call the method on the server side on the remote object, and here is this method

public int tirar(int x, int y) throws RemoteException{
    if (gato[x-1][y-1] == 0){
        if (turno == 1){
            gato[x-1][y-1] = 1;
            if (ganar()== 1){
                reiniciar();
                return 1;
            } else if (ganar() == 2){
                reiniciar();
                return 2;
            }
            turno = 2;
        }
        else{
            gato[x-1][y-1] = 2;
            if (ganar()== 1){
                reiniciar();
                return 1;
            } else if (ganar() == 2){
                reiniciar();
                return 2;
            }
            turno = 1;
        }
    } else
        return 3;
    return 0;
}

Everytime the method tirar(int, int) is called, it checks the matrix on the remote object with the moves and positions the players have made. The method ganar() is to know when the game is finished, it returns 0 if it hasnt fininshed, 1 if somebody won, 2 if its a tie.

On the other hand I still don't know how many clients are connected and I would like to be 2, if theres one that the app wait until theres 2 and if a third cient tries to make a connection refuse it.

Any help would be greately appreciated, I've been looking for an answer for some time now and I haven't found anything.. This is my last option..

P.S. I hope I make myself clear, english is not my first language

4

1 回答 1

0

我不知道这是否是正确的方法,但为了解决我的问题,我在两个客户端上都使用了一个计时器,所以每次客户端都会检查服务器上的更新,所以如果有任何更新,客户端就会更新并给出外观他们知道其他客户何时采取行动。这对我来说是个窍门。

于 2014-09-07T14:59:57.050 回答