简体   繁体   中英

Computer Vs Computer Loop Java Applet

I am having issues setting up a computer vs computer loop in my java game applet. I have been trying for 3 days now to effectively add a one second delay between the two computer-player turns, while also repainting the board. I have tried try/catch/thread.sleep and wait and a few other tricks, however none have been successful. In the program's current state, when a computer vs computer game is initiated, the program freezes for the duration of the game (with one second delays between moves) and then displays the final board when the game has finished. How can I make it so the program repaints/delays after each move? From all of the reading I have done, I am aware that the following implementation will not work but my issue is I cannot figure out how to do it any other way. Thanks in advance!

The following code is inside my actionPerformed listener method

if (event.getSource() == startAIvAI)
    {
        drawing.clear();

        while (drawing.hasWon() == -1 && !drawing.isFull())
        {

            go1();
            repaint();

            try {
                Thread.sleep(1000);
            } catch (Exception e) {}

            go2();
            repaint();
            try {
                Thread.sleep(1000);
            } catch (Exception e) {}

        }


    }

When you invoke Thread.sleep(1000); which Thread do you think you're pausing? Probable the one handling event given your code start with if (event.getSource() == startAIvAI) .

You should read this to understand what to do: Sleep method locks my GUI

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM