How to return when finished/keep record of the remaining time (Timer/Schedulel)?

Asked

Viewed 78 times

1

So, I’m developing a project for a game that uses RMI (client/server) and needed to manage the timeout of players' plays. For example, if a player takes more than a minute to make his or her move, he or she loses.

The plays run through a loop, so I’d need to check each interaction to see if time is up. I was currently trying to do with the Timer/Schedulel classes, but I can’t find a way to "save" time and check when it’s over.

My doubt: How can I save/return the time currently running? Are there any tricks or classes for this? I’m trying to achieve something like, for example:

Timer timer;
int tempoAtual;            // Variável para guardar tempo
boolean isAcabou = false;  // Variável para verificar termino
public Temporizacao(int seconds) {
    timer = new Timer();                        // Cria um novo Timer
    timer.schedule(new Teste(), seconds*1000);  // set tarefa...
}

class Teste extends TimerTask {
    public void run() {
        // tempoAtual  -> atualiza tempo atual (?)
        timer.cancel();
        isAcabou = true;  
    }
}

public static void main(String args[]) {
    new Temporizacao(5);        // set tarefa de 5seg

    if(tempoAtual != isAcabou){ // Variável matem o tempo salvo para fazer validações
    System.out.println("Acabou tempo.");
   }
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.