Question

I'm writing a console Boggle game and the gameLoop logic isn't quite what I think it should be.

My basic game loop method is:

    public void gameStart() {
        timer.schedule(new BoggleTimerTask(1),0);
        gameActive = true;
        System.out.println(boggle.getBoggleTrayAsString());
        while (gameActive) {
            String word = scanner.next();
            boggle.addGuess(word);
        }
        scanner.close();
        timer.cancel();
        postGame();
    }

My timer task class run method looks like this:

    public void run() {
        if (minutesRemaining == 4 || minutesRemaining == 3 || 
                minutesRemaining == 2)
            minutesRemaining--;
        else if (minutesRemaining == 1 || minutesRemaining == .5)
            minutesRemaining -= .5;
        if (minutesRemaining == 3) {
            System.out.println("3 minutes remaining.");
            timer.schedule(new BoggleTimerTask(minutesRemaining),60000);
        }
        else if (minutesRemaining == 2) {
            System.out.println("2 minutes remaining.");
            timer.schedule(new BoggleTimerTask(minutesRemaining),60000);
        }
        else if (minutesRemaining == 1) {
            System.out.println("1 minute remaining.");
            timer.schedule(new BoggleTimerTask(minutesRemaining),
                    30000);
        }
        else if (minutesRemaining == .5) {
            System.out.println("30 seconds remaining.");
            timer.schedule(new BoggleTimerTask(minutesRemaining),
                    30000);
        }
        else {
            System.out.println("Time's Up!");
            gameActive = false;
        }

The timer counts down exactly as I think it should. "Time's Up!" prints out as well, however postGame() is never called. I initially thought that the issue was the main thread waiting on scanner.next() so I found a solution using the Robot class and calling a key press and key release with VK_ENTER and that did not work.

I then tried adding a check on word in the gameLoop. I added the check if (word.compareTo("quit") == 0) gameActive = false. That does call postGame() both when I personally enter quit as a word or when I spell out quit and enter with the Robot.

So I've surmised this much: the gameLoop does terminate and send the program to postGame() when gameActive = false and "Time's Up!" is printing so the last else block is catching appropriately in the timer. Why those two don't work in conjunction to execute properly (or at least how I think it SHOULD execute) I don't know.

Any help someone could provide would be greatly appreciated. Thanks in advance.

Was it helpful?

Solution

I think you need to have two threads running to do something like this (better than robot).

public static void main(String[] args) {
    new Thread() {
        public void run() {
            while(true) {
                String input = new Scanner(System.in).nextLine();
                doSomethingWithInput(input);
            }
        }
    }.start();
    new Thread() {
        public void run() {
            int time = 4;
            while(time > 0) {
                System.out.println(time + " seconds left.");
                printGameBoard();
                time--;
                Thread.sleep(1000);                    
            }
            printScore();
            System.exit(0);
        }
    }.start();
}

Just define the functions and you have your game.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top