Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何让Minimax Alpha-Beta算法发挥作用?_Java_Artificial Intelligence_Chess_Minimax_Alpha Beta Pruning - Fatal编程技术网

Java 如何让Minimax Alpha-Beta算法发挥作用?

Java 如何让Minimax Alpha-Beta算法发挥作用?,java,artificial-intelligence,chess,minimax,alpha-beta-pruning,Java,Artificial Intelligence,Chess,Minimax,Alpha Beta Pruning,我已经开发了一个使用minimax和alpha-beta的象棋引擎,但是我想通过玩自己的游戏来测试我的算法,电脑对电脑。我什么都试过了,都没有用。只是想知道我会怎么做 public class Main { static JTextArea textField; public static void main(String[] args) { while ( 'K' != ABChess.board[ABChess.kingLocationUU/8][ABCh

我已经开发了一个使用minimax和alpha-beta的象棋引擎,但是我想通过玩自己的游戏来测试我的算法,电脑对电脑。我什么都试过了,都没有用。只是想知道我会怎么做

public class Main {  

static JTextArea textField;

    public static void main(String[] args) {


        while ( 'K' != ABChess.board[ABChess.kingLocationUU/8][ABChess.kingLocationUU%8]) {ABChess.kingLocationUU++;}
        while ( 'k' != ABChess.board[ABChess.kingLocationLL/8][ABChess.kingLocationLL%8]) {ABChess.kingLocationLL++;}

        Asset.init("/images/ChessPiecess.png");

        ABChess.updateKingLocations();
        //print();  

        JPanel depthPanel = depthPanel();
        JPanel optionPanel = optionPanel();
        JPanel logPanel = logPanel();

        JPanel menuPanel = new JPanel();
        menuPanel.setPreferredSize(new Dimension(140, 100));
        menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
        menuPanel.add(depthPanel);
        menuPanel.add(optionPanel);
        menuPanel.add(logPanel);


        GUInterface gui = new GUInterface();

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(gui);
        panel.add(menuPanel, BorderLayout.EAST);

        JFrame frame = new JFrame(ABChess.title);

        frame.setSize(ABChess.width, ABChess.height);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);        

        System.out.println(ABChess.possibleMoves());
        ABChess.playerChoice = JOptionPane.showOptionDialog(null, "Who wants to make the first move?", "Who moves first?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, ABChess.options, ABChess.options[0]);

        if (ABChess.playerChoice == 0){
            ABChess.flipBoard();
            long startTime=System.currentTimeMillis();
            Move autoMove = AlphaBeta.alphaBeta(ABChess.gameDepth, 1000000, -1000000, new Move(), 0);
            long endTime=System.currentTimeMillis();
            ABChess.makeMove(autoMove);
            ABChess.flipBoard();
            System.out.println("COMPUTER'S MOVE TOOK "+((endTime-startTime)/1000.0)+" SECONDS");                        
            ABChess.printBoard();
            frame.repaint();
            displayMessage("Took "+((endTime-startTime)/1000.0)+" seconds");
        }

    }
这是运行文件时对算法的初始调用

public void mousePressed(MouseEvent event) {
        if ( event.getX() < 8*sizeOfSquare && event.getY() < 8*sizeOfSquare) {
            mouseX = event.getX();
            mouseY = event.getY();
            repaint();
        }
    }


    public void mouseReleased(MouseEvent event) {
        if  (event.getX() < 8*sizeOfSquare && event.getY() < 8*sizeOfSquare) {
            newMouseX = event.getX();
            newMouseY = event.getY();
            if (event.getButton() == MouseEvent.BUTTON1) {
                // Regular move
                Move legalMovesMove = new Move(mouseY/sizeOfSquare, mouseX/sizeOfSquare, newMouseY/sizeOfSquare, newMouseX/sizeOfSquare, Test6.board[newMouseY/sizeOfSquare][newMouseX/sizeOfSquare]);

                java.util.List<Move> legalMoves = ABChess.possibleMoves();
                for(Move m : legalMoves) {
                    if (m.equals(legalMovesMove)) {
                        ABChess.makeMove(legalMovesMove);
                        ABChess.flipBoard();
                        long startTime=System.currentTimeMillis();
                        Move autoMove = AlphaBeta.alphaBeta(ABChess.gameDepth, 1000000, -1000000, new Move(), 0);
                        long endTime=System.currentTimeMillis();
                        ABChess.makeMove(autoMove);
                        ABChess.flipBoard();
                        System.out.println("COMPUTER'S MOVE TOOK "+((endTime-startTime)/1000.0)+" SECONDS");                        
                        ABChess.printBoard();
                        repaint();

                    }
                }

                checkMate = ABChess.kingSafe();

                if(checkMate == false){
                   int yes = JOptionPane.showOptionDialog(null, "Do you want to make the first move?", "Who moves first?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, JOptionPane.YES_OPTION);

                   if (yes == JOptionPane.YES_OPTION){
                       ABChess.resetGame();
                        repaint();

                   } else if (yes == JOptionPane.NO_OPTION){
                              System.exit(0);  
                   }
                }

                legalMoves = ABChess.possibleMoves();

                if (legalMoves.size() == 0) {
                    ABChess.playAgain = JOptionPane.showOptionDialog(null, "Stalemate! Wanna play again?", "Draw!", JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE, null, ABChess.choice, ABChess.choice[1]);

                    if (ABChess.playAgain == 0) {
                        System.out.println("Yes I will");
                        ABChess.resetGame();
                        repaint();
                    } else {
                        System.exit(0);


                    }
                }
            }
        }
    }
public void mousePressed(MouseEvent事件){
if(event.getX()<8*sizeOfSquare&&event.getY()<8*sizeOfSquare){
mouseX=event.getX();
mouseY=event.getY();
重新油漆();
}
}
公共无效MouseEvent事件(MouseEvent事件){
if(event.getX()<8*sizeOfSquare&&event.getY()<8*sizeOfSquare){
newMouseX=event.getX();
newMouseY=event.getY();
if(event.getButton()==MouseEvent.BUTTON1){
//常规动作
Move-legalMovesMove=新移动(mouseY/sizeOfSquare、mouseX/sizeOfSquare、newMouseY/sizeOfSquare、newMouseX/sizeOfSquare、newMouseX/sizeOfSquare、Test6.board[newMouseY/sizeOfSquare][newMouseX/sizeOfSquare]);
java.util.List legalMoves=ABChess.possibleMoves();
对于(移动m:legalMoves){
如果(m.equals(legalMovesMove)){
ABChess.makeMove(legalMovesMove);
ABChess.flipBoard();
long startTime=System.currentTimeMillis();
Move autoMove=AlphaBeta.AlphaBeta(ABChess.gameDepth,1000000,-1000000,new Move(),0);
long-endTime=System.currentTimeMillis();
ABChess.makeMove(自动移动);
ABChess.flipBoard();
System.out.println(“计算机的移动花费了”+((结束时间开始时间)/1000.0)+“秒”);
ABChess.printBoard();
重新油漆();
}
}
将死=ABChess.kingSafe();
如果(将死==false){
int yes=JOptionPane.showOptionDialog(null,“您想做第一步吗?”,“谁先移动?”,JOptionPane.yes\u NO\u选项,JOptionPane.QUESTION\u消息,null,null,JOptionPane.yes\u选项);
if(yes==JOptionPane.yes\u选项){
ABChess.resetGame();
重新油漆();
}else if(yes==JOptionPane.NO_选项){
系统出口(0);
}
}
legalMoves=ABChess.possibleMoves();
如果(legalMoves.size()==0){
ABChess.playteach=JOptionPane.showOptionDialog(null,“僵局!想再玩吗?”,“平局!”,JOptionPane.YES\u NO\u选项,
JOptionPane.QUESTION_消息,null,ABChess.choice,ABChess.choice[1]);
如果(ABChess.playtore==0){
System.out.println(“是的,我会”);
ABChess.resetGame();
重新油漆();
}否则{
系统出口(0);
}
}
}
}
}

这就是每次释放鼠标时调用算法的地方。不知道如何编码到它使用白色棋子而不是我玩的地方。

我通常会将玩家与游戏分开,游戏会请求玩家对象的交互。玩家对象可以是一个人(因此所需的输入被委托给某个UI),也可以是一个AI,因此它将被委托给某个决定哪一步是最好的实现

我建议在ABChess游戏中使用对象,而不是静态方法

因此,通过一点重构,并将UI与逻辑分离,它可以如下所示:

interface Player {
    Move decide(List<Move> legalMoves);
}

class ChessGame {

    ABChess game;

    Player player1;
    Player player2;
    UIInterface ui;

    ChessGame(Player player1, Player player2, UIInterface ui) {
        this.player1 = player1;
        this.player2 = player2;
        this.ui = ui;

        game = ...
    }

    public void simulate() {

        // ... initial ui ...

        boolean player1Turn = true;

        do {
            Move move = null;

            if (player1Turn) {
                move = player1.decide(game.possibleMoves());
            } else {
                move = player2.decide(game.possibleMoves());
            }

            game.makeMove(move);
            // ... update ui ...

            player1Turn = !player1Turn;

        // check if somebody has won ...
        } while (game.isRunning());

        // ... update ui with the game result ...
    }
}
界面播放器{
移动决定(列出法律移动);
}
阶级棋局{
国际象棋比赛;
玩家1;
玩家2;
界面界面;
棋类游戏(玩家玩家1、玩家玩家2、ui界面){
this.player1=player1;
this.player2=player2;
this.ui=ui;
游戏=。。。
}
公共空间模拟(){
//…初始用户界面。。。
布尔player1Turn=true;
做{
Move=null;
如果(播放者1转){
move=player1.decision(game.possibleMoves());
}否则{
move=player2.decision(game.possibleMoves());
}
游戏。makeMove(移动);
//…更新用户界面。。。
player1Turn=!player1Turn;
//检查是否有人赢了。。。
}while(game.isRunning());
//…使用游戏结果更新ui。。。
}
}
一旦完成,模拟游戏就变得很容易了。您只需使用合适的玩家启动
棋盘游戏
,并调用simulate方法。在这一点上,您还可以决定完全跳过UI演示(这样学习速度会更快)