首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果程序只通过doClick()交互,则不会呈现doClick();

如果程序只通过doClick()交互,则不会呈现doClick();
EN

Stack Overflow用户
提问于 2021-03-04 17:00:06
回答 1查看 27关注 0票数 2

我正在使用Swing编写Othello游戏,下面是一般代码:(片段不包括游戏结束/同一玩家再次运行的情况,这只是基本的游戏循环)。

视图/主计长:

代码语言:javascript
复制
public class OthelloGUI extends JFrame {
    JSquare[][] board;
    final int BOARD_SIZE = 8;
    OthelloModel model;
    
    public OthelloGUI(OthelloModel m) {
        model = m;
        
        /* Window size, etc... */
        setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));

        //add 8x8 buttons to GUI
        ActionListener ml = new MoveListener();
        board = new JSquare[BOARD_SIZE][BOARD_SIZE];

        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                getContentPane.add(board[i][j] = new JSquare(new Point(i, j)));
                board[i][j].addActionListener(ml);
            }
        }
        update();
    }

    void update() {

        //update GUI to reflect last move
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                //changes the JButton's text based on who owns it on the model's board
                board[i][j].setText(model.getOwner(i, j).view());
            }
        }
        
        //best place (I think) to have AI move is after the last move is processed
        if (model.getCurrentPlayerType() == PlayerType.AI) {

            //Computer player finds its next move, and clicks that button
            int[] bestMove = GreedyAI.findMove(model);
            board[bestMove[0]][bestMove[1]].doCLick();
        }
    }

    static class JSquare extends JButton {
        java.awt.Point p;
        JSquare (Point p) {this.p = p;}
    }

    class MoveListener implements ActionListener {
        
        @Override
        public void actionPerformed(ActionEvent e) {
        
            JSquare sq = (JSquare) e.getSource();

            //places the piece on the model's "board" and flips all applicable pieces
            int result = model.placeDisk(s.p, model.getCurrentColor());
            
            if (result == 0) {//move executes successfully
                update();
            }
            //otherwise... (illegal move, game end, same player again, etc...) 
        }
    }
}

enum Owner {
    NONE, BLACK, WHITE;

    public String view() {
        return this == BLACK ? "B" : this == WHITE ? "W" : ""
    }
}

如果启动Human v HumanHuman v AI游戏,此代码执行得很好,但是,如果启动AI v AI游戏,GUI就不会呈现。我得到了弹出框的游戏设置,弹出的游戏结束和谁赢了,但主ContentPane与董事会从来没有呈现,所以我不知道什么移动人工智能作出。

有什么想法/建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-03-04 17:55:46

人工智能是自动化的。人类是..。不。

  • 当人类在扮演人类时,代码必须等待人类做出每一个回合。它必须是事件驱动的。
  • 当人类玩人工智能时,人工智能是自动的,但是代码仍然必须等待来自人类的事件。
  • 但是当AI播放AI时,整个过程由代码处理,这将导致EDT被阻塞。

不要阻塞EDT (事件调度线程)。当这种情况发生时,GUI将“冻结”。有关详细信息和修复,请参见在Swing中并发

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66479593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档