Louis B
Louis B

Reputation: 342

trying to program a timer so that a user can only input words for a certain amount of time

I am trying to make a game where you can only enter words for 10 seconds. I tried to create a multithread solution but it doesn't work properly.

class timer extends Thread{//thread
public void run(){
    for(int i=10;i>=0;i--){
        System.out.print(i+" ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}

Main method:

            timer t=new timer();
            t.start();
            while () {//not sure what to put in my while statement
                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! ");
            }

essentially, after the thread goes for 10 seconds and terminates,I want it to return a break statement so the program leaves the while loop. Any suggestions?

Upvotes: 1

Views: 2475

Answers (3)

hmatar
hmatar

Reputation: 2419

You could have a shared boolean where your thread and main share in a synchronized fashion.

The timer could be as follows;

class timer extends Thread{//thread
private Object lock = new Object(); // a lock used by both your thread and main to access stop boolean
private boolean stop = false;
public void setStop() // your thread calls this method in order to set stop
{
   synchronized(lock) {
   stop = true;
   }
}

public boolean getStop() // main calls this to see if thread told main to stop.
{
   boolean _stop;
   synchronized(lock) {
   _stop = stop;
   }
   return _stop;
}
public void run(){
    for(int i=10;i>=0;i--){
        System.out.print(i+" ");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        setStop();
    }
}
}

Your main could be as follows:

timer t=new timer();
            t.start();
            while (!t.getStop()) {// calls getStop to see if other thread told main to stop
                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! ");
            }
            t.join(); // to make sure main terminates after the termination of other thread

Upvotes: 0

Vishal K
Vishal K

Reputation: 13066

Here is a simple Demo that would let you to know how to use java.util.Timer .

import java.util.Timer;
import java.util.TimerTask;
import java.util.Scanner;
class Tester 
{
    static long i = 0;
    public static void main(String[] args) throws Exception
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("You have only 10 seconds to find the result");
        System.out.println("What is the value of : 111111 X 111111 ");
        Timer timer = new Timer("Timer");
        timer.schedule(new TimerTask()
        {
            public void run()
            {
                if (i == 12345654321L)
                {
                    System.out.println("Congrats!! you guessed the write answer :)");
                }
                else
                {
                    System.out.println("Sorry Time is over. You couldn't guess the correct answer.");
                }
                System.exit(0);
            }
        },10 * 1000 , 1);
        while (true)
        {
            i = scanner.nextLong(); 
            if ( i == 12345654321L)
            {
                System.out.println("Congrats!! you guessed the write answer :)");
                System.exit(0);
            }
            else
            {
                System.out.println("Try next  guess :");
            }
        }
    }
}

EDIT

Since I don't have your all code so I am posting here the solution for your answer on my basic assumption. Don't use Thread. Instead use java.util.Timer. Your code would look as follows:

static String input=" ";//created a static variable input to take input 
public static void main(String st[])
{
    Timer timer = new Timer("Timer");
    timer.schedule(new TimerTask()
    {
        public void run()
        {
            if (test.CheckGame(input))
            {
                System.out.println("Congrats!! you guessed the write answer :)");
            }
            else
            {
                System.out.println("Sorry Time is over. You couldn't guess the correct answer.");
            }
            System.exit(0);
        }
    },10 * 1000 , 1);//waits for 10 seconds
    while (true) 
    {
        System.out.print("Guess a word on the board! ");
        input = scan.next();
        if(test.CheckGame(input))
        {
            System.out.print("Good job! ");
            System.exit(0);
        }
        else
        {
            System.out.println("Bad Guess. Try again ");
        }
    }
}

Upvotes: 1

ctlevi
ctlevi

Reputation: 1427

Change your code to this

timer t=new timer();
t.start();
while (t.isAlive()) {//not sure what to put in my while statement
    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! ");
}

Once the run function exits, t.isAlive will be false. You may also need to pass the timer object around and check the isAlive() of the object, depending on how CheckGame works. This is so that the input cannot be put in after the 10 seconds for an indefinite period of time.

Upvotes: 1

Related Questions