开发者

Timer Help:Working on a project for my AP Computer Science class

In our class we are making a game. The user has to Guess words and stuff. I don't think info about the game is needed to answer my question/problem. Ok so what I am trying to do is to give the user a time limit in which t开发者_JS百科hey have to guess the word. Something like 15 seconds. If the user does not guess the word in 15 seconds they lose a turn. Problems:

  1. We didn't learn how to use timers. I experiment with timers and stuff. I can get a timer to count down from 15.
  2. I can't check the current time while waiting for the user to input a guess.
  3. I don't know how to bypass Stdin.readString() and make the program check the time.

Thanks.


Well, you can use the Scanner class to gather input from the user.

You may want to avoid timers if you don't know what threading is yet, but if you do want to try, you might be interested looking into the TimerTask & Timer classes.

While you may already know, you can get time from the System class, like currentTimeMillis


You have a few options. As you said, your program is waiting for input and hence that thread is busy. What you can do is create a separate thread, pass your timer to that thread and have it check the timer. Perhaps something like the following:

public class TimerChecker implements Runnable {
    private Timer timer;
    public TimerChecker(Timer timer) { this.timer = timer; }

    @Override
    public void run() {
      // implement logic here
    }
}

Which you can have invoked in a new thread using:

 Timer timer = ...
 new Thread(new TimerChecker(timer)).start();
 // Now you are free to perform your blocking operation in the current thread
 Stdin.readString();


One way to do this is, well to a run a separate thread object for the timer... this thread shall handle the updating of the time and would then trigger a certain event when the time of the player runs out...

or more like, implementing a counter in a separate thread whose increments are triggered by time-step, in this case, in seconds, you can do this by calling sleep()..

the timer thread object shall maintain a variable which keeps track of the current time..

on the main method of your program, you shall continue to check the value of this variable, as a pre-condition of your main loop perhaps,

the idea is there i think, just a thought!

pseudocode

class Timer extends Thread{
    int current_time = 0;

    public void run(){
     sleep(1000);
     current_time += 1;
    }

    public void synchronized getCurTime(){
     return current_time;
    }
}



class Game{

  public Game(){

   Timer timer = new Timer();
    timer.start();

   while (timer.getCurTime() <16){
      //get the guess of the user
      //checks if it's correct
      // if it is correct, output you win and break!
   }

  //time runs out

  }

}


How about an Event driven architecture with Java eventing library?

Example of events with conditions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜