0

I have written code that solves a boggle board and now I want to convert it into my own game. I have everything I need except I cannot figure out how to set a time limit so the person can only enter words in for a 2 minutes. This is what I have so far, but it doesn't do what i want it to do, which is to stop after 2 mins.

            long startTime = System.nanoTime();
            long endTime1=0;
            long maxDurationInMilliseconds = 2 * 60 * 1000;
            while (2 * 60 * 1000> endTime1-startTime) {
                System.out.println(System.currentTimeMillis());
                System.out.print("Guess a word on the board! ");
                if(test.CheckGame(scan.next())==true){
                    System.out.print("Good job! ");
                }
                else    
                    System.out.print("Guess again! ");
                endTime1 = System.nanoTime();
            }           
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Louis B
  • 342
  • 1
  • 5
  • 21

1 Answers1

3

You are comparing milliseconds with nanoseconds. That seems to be the problem here.

You should probably use System.currentTimeMillis to calculate both startTime and endTime1.

Or if you need nanosecond resolution (though not guaranteed with System.nanoTime) you should represent max duration in nanoseconds.

  • thanks that actually works! however now my problem is is that it only is able to check the time after it goes through the loop so is there anyway to have a running timer? and is there any way to have the timer print out the clock? – Louis B Mar 29 '13 at 00:15