简体   繁体   English

计算机与计算机循环Java小程序

[英]Computer Vs Computer Loop Java Applet

I am having issues setting up a computer vs computer loop in my java game applet. 我在Java游戏小程序中设置计算机与计算机循环时遇到问题。 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. 我已经尝试了3天,以有效地在两个计算机玩家回合之间增加一秒钟的延迟,同时还要重新粉刷木板。 I have tried try/catch/thread.sleep and wait and a few other tricks, however none have been successful. 我尝试过try / catch / thread.sleep和wait和其他一些技巧,但是都没有成功。 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 以下代码在我的actionPerformed侦听器方法中

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); 当您调用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) . 给定代码以if (event.getSource() == startAIvAI)开头的情况下,可能是一个处理事件。

You should read this to understand what to do: Sleep method locks my GUI 您应该阅读以下内容以了解操作: 睡眠方法会锁定我的GUI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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