0

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.

  • 2
    See http://stackoverflow.com/questions/4044726/how-to-set-a-timer-in-java – Danstahr Oct 06 '15 at 14:29
  • I would calculate time eslapsed to check that, as said here: http://stackoverflow.com/questions/1770010/how-do-i-measure-time-elapsed-in-java, take a look on Pascal Thivent answer. – spekdrum Oct 06 '15 at 14:32
  • All right, thanks. Already viewing it now. I'll update if it didn't solve the problem. – IneptGamer02 Oct 06 '15 at 14:32
  • @Danstahr , may be this will fix the problem, however, the only remaining problem is how would I implement this to our existing code. – IneptGamer02 Oct 06 '15 at 14:48

1 Answers1

1

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.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • I just want to terminate the current level once the timer is done and maybe print out a message that the timer is finished. Keeping in mind that the player can still answer WHILE the timer isn't finished yet. Can you provide me with an example code on how to do this? – IneptGamer02 Oct 06 '15 at 14:39
  • You need to create a second thread (a timer) that block other operations setting a variable as explained before. It is not simple to handle multithreading (it is considered an advanced topic) and if you don't know how threads work it is better to leave this road before creating some difficult to manage problem related to multithreading. – Davide Lorenzo MARINO Oct 06 '15 at 14:41
  • Would you mind to help me? We need to get this done before the 8th of October, like I said, we're almost finished with the whole thing, the only missing is that timer. I have thought of creating two threads, then just running them the same time, would that work? And how would I even run them the same time? – IneptGamer02 Oct 06 '15 at 14:44
  • Would it work if I create another Thread that sleeps for 500 seconds, then after it's done sleeping, just set the variable _tries_ to 31? – IneptGamer02 Oct 06 '15 at 14:53
  • Yes if you accept the last answer, otherwise it will be accepted also over time. And remember that tries access must be synchronized – Davide Lorenzo MARINO Oct 06 '15 at 14:57
  • What do you mean if I accept the last answer and that tries access must be synchronized? Please elaborate. – IneptGamer02 Oct 06 '15 at 14:59
  • If two threads try to change the value of tries at the same time as it is possible in your situation you can have an invalid value for the variable tries. For example if tries has value 1 and the timer exceed it change it to 31 but if the main thread at the same time change it to the value 2 you lost the information that the timer exceeded. – Davide Lorenzo MARINO Oct 06 '15 at 15:02
  • Ah, I understand, however if they didn't do it at the same time, would it still overwrite the value that the second thread changed? – IneptGamer02 Oct 06 '15 at 15:12
  • Yes the problem of multithreading often are related to when a particular value changes. As additional note use the volatile keyword for the variable tries or you will possible have another problem related to cached copies of tries at thread level – Davide Lorenzo MARINO Oct 06 '15 at 15:13
  • Ah, I see, I'll try my best. On a side note, how would I mark my question as answered if it has been solved? – IneptGamer02 Oct 06 '15 at 15:17
  • You can upvote the question with the up symbol and use the green check to flag it as resolved. It is possible that to mark it as resolved you need to wait some minutes (i think that now you should check it). – Davide Lorenzo MARINO Oct 06 '15 at 15:19
  • Thanks a lot, Davide! Not to sound stupid or anything, but, where's the plus symbol? All I see are arrow heads pointing upwards and downwards. – IneptGamer02 Oct 06 '15 at 15:19