nmu
nmu

Reputation: 1519

Can a while loop wait for an event to continue java

I have a hangman engine that uses a JOptionPane to get the users guess, but now that I am implementing a GUI with a text field, I need a way to stop the engine and get the users guess from the text field - I'm trying to use MVC so at the moment the data from the text field gets sent to the main/controller class however after the data is loaded into a variable in the main class - I start having trouble integrating that data into the engine

I don't know if adding a wait to the engine would be the best thing but that's all I can really think of.

Here is my engine class:

public class engine {
    char [] charda = new char[20];
    char [] charwo = new char[20];
    Highscore words = new Highscore();
    main mn = new main();
    int guesses =7;
    char guess;

    public engine() {
    }

    public void enginer(){

        int count = 0;
        String word = words.getWord();

        for (int i = 0; i<word.length(); i++)
        {
            //instantiates two arrays one of dahses and one with the word
            charwo[count] = word.charAt(i);
            charda[count]= '_';
            count++;
        }

        for (int l=0; l<count; l++)
        {
            System.out.print(" "+charda[l]);
        }

        while (guesses != 0 && !Arrays.equals(charwo, charda))
        {
            System.out.println("");
            guess = JOptionPane.showInputDialog("enter a Guess").charAt(0);

            if (word.toUpperCase().contains(String.valueOf(guess).toUpperCase()))
            {
                for (int k = 0; k<word.length(); k++)
                {
                    if (String.valueOf(guess)
                            .toUpperCase()
                            .equals( String.valueOf(charwo[k]).toUpperCase() ))
                    {
                        charda[k]=charwo[k];

                        for(int l=0; l<count; l++)
                        { // prints dashes here to avoid a letter being 
                          // chopped off by the program stopping in the middle
                            System.out.print(" "+charda[l]);

                        }
                    }
                }
            }
            else
            {
                guesses = guesses-1;
                System.out.println("guesses left "+guesses);
                //
                //Re-displays dashes 
                for (int l=0; l<count; l++)
                {
                    System.out.print(" "+charda[l]);
                }
            }

            if (Arrays.equals(charwo, charda))
            {
                System.out.println("");
                System.out.println("You are Winner");

            }
        }
    }
}

Here is the part of my main class that handles the button click

public void buttonClicked ()
{
    gameplay gp = new gameplay(); 
    engine en  = new engine();

    en.guess = gp.getInput; // this is where I try send the input from the text field to the engine
    en.enginer(); 

    System.out.println("button clicked"); 
}

I'm really lost at the moment so even a nod in the right direction would be really helpful :)

Upvotes: 1

Views: 1620

Answers (1)

AlexR
AlexR

Reputation: 115338

Generally waiting can be implemented using wait/notify built-in java mechanism.

Although you can find a bunch of tutorials that explain this issue here is a very brief explanation.

Each class in java automatically extends java.lang.Object that defines methods wait() and notify(). Wait suspends current thread until notify() is called on the same object. The object that is used to invoke wait() and notify() is called monitor. Following rules of java thread management both method must be invoked from synchronized block, i.e.

synchronized(obj) {
    obj.wait();
}

Now the answer to your question is simple. Define monitor object that is visible from both points (I mean code that should wait and code that should trigger the first thread to continue).

Implement waiting as:

synchronized(obj) {
    obj.wait();
}

When use clicks button (so you want the wait to exit) you should invoke code:

synchronized(obj) {
    obj.notify();
}

That's it. Pay attention that there is version of wait with timeout and there is notifyAll() too. And IMHO take some time to learn java threads, synchronization etc. It is fun. Good luck.

Upvotes: 4

Related Questions