简体   繁体   中英

How can I run commands under a timer? A more detailed explanation below

Please forgive me if I write anything that is irrelevant or wrong regarding my question, since this is actually my first time asking a question here in this website, also, I've only just recently started in Java programming , so please spare me for terminologies and concepts that I can't fully grasp.

So we were tasked to create a basic Text Twist game, one of our objectives was to create a timer that will terminate the current level once it is finished, however, we also need to ask the player for answers while the timer is running.

So me and my group decided to limit the player's tries to 30 and that we'll allow the player to answer for 300 seconds (five minutes).

If one of the above requisites has been fulfilled, then the current level will terminate.

So we used the FOR loop for this case, our code is down below. Please note that all the variables that we used below have been properly initialized in the beginning of the program and that the code below is just a small part of our lengthy code.

By the way, the code below is just a shortened (VERY SHORTENED) version of the loop statement. I just pasted it down there to give you guys a preview on how we did the program.


for (tries=1; tries<=30; tries++)
{
    System.out.println();
    System.out.println("====================");
    System.out.println("Again, the twist is: " + twistlaptop);
    System.out.println();
    System.out.println("You have found " + guesslaptop + " words.");
    System.out.println("Try #" + tries + ".");
    System.out.println();
    System.out.println("Current score: " + disp.format(score));
    System.out.println();   
    System.out.print("Enter your answer: ");
    guess=input.next();
    guess=guess.toLowerCase();
    System.out.println();
}

We used the word "LAPTOP," for the source word in the first level. So all that is left for us to do, is just the timer. Keep in mind that the code above needs to be running at the same time as the timer, and when one of them (30 tries are done or 300 seconds have passed) finishes, level one terminates and level two starts.

I am very willing to paste the whole code here, however the code contains of 2,277 lines including unnecessary comments in the program, I'll have to delete irrelevant comments if you guys ask me for it.

Again, I'll put emphasis on the part where the player can answer at the same time as the timer is counting down.

There are two different possible answers depending on how you like to treat overtime:

  • Simply wait for the answer and than show that the answer is not accepted because it arrives too late
  • Inform the user when the maximum time has exceeded

For the first approach simply do something like the following:

long now = System.currentTimeMillis(); 
// Your code to post the question
long after = System.currentTimeMillis();
if (after - long > MAX_WAIT_MILLISECONDS) {
   System.out.println("Response not accepted time exceeded");
} else {
   // Accept the answer
}

The second approach is more complicated because you need to start a second thread (for example in a timer) that after MAX_WAIT_MILLISECONDS show a message and change a shared variable (you need to use synchronization or at least volatile, search informations on multithreading to know how to do that) that is checked when the user try to answer.

Possible alternative for second approach:

First thread

Second thread (timer)

// Run method of TimerTask
public void run() {
    if (!answered) {
        System.out.println("Time exceeded");
        timeExceed = true;
    }
}


// Main thread

// Print the question
startTimer(); // Use Timer and TimerTask

guess = input.next();
if (timeExceed) {
    System.out.println("Time exceeded your answer is not accepted");
} else {
    answered = true;
    // Handle answer
}

Note: timeExceed and answered are shared variables between two threads. You need to synchronize the access to those variables. This is only a pseudo code and you need add the right visibility or getter and setter for those variables that probably will be present in another class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM