Java Minimax井字游戏无法正常工作

时间:2019-06-25 17:48:55

标签: java artificial-intelligence minimax game-theory

因此,我一直在研究创建一个简单的井字游戏,其中人类玩家与通过minimax算法运行的ai对抗。最近几天,我试图找出这两个错误,但我似乎无法终生。首先,人工智能似乎是可以预见的并且不是很好。其次,似乎可行的唯一方法是首先让AI前进,如果我让人类玩家先进入,那么AI就会继续填补下一个可用位置。任何帮助将不胜感激。

这是我的代码:

import java.util.Scanner;
public class Game 
{

    static String player = "X";
    static String opponent = "O";
    int row;
    int col;
    public Game(int x, int y)
    {
        row = x;
        col = y;
    }
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        String [][] board = new String [3][3];
        fillBoard(board);
        while(true) //Infinite loop only for testing, will change back
        {
            getBestMove(board);
            printBoard(board);
            playerTurn(board, input);
            printBoard(board);
            //System.out.println("Best move: " + bestMove.row + " " + bestMove.col);
        }
        //input.close();
    }
    static int checkState(String [][] board) 
    { 

        for (int row = 0; row<3; row++) //Rows
        { 
            if (board[row][0] == board[row][1] && 
                board[row][1] == board[row][2]) 
            { 
                if (board[row][0]==player) 
                    return -10; 
                else if (board[row][0]==opponent) 
                    return +10; 
            } 
        } 


        for (int col = 0; col<3; col++) //Columns
        { 
            if (board[0][col]==board[1][col] && 
                board[1][col]==board[2][col]) 
            { 
                if (board[0][col]==player) 
                    return -10; 

                else if (board[0][col]==opponent) 
                    return +10; 
            } 
        } 


        if (board[0][0]==board[1][1] && board[1][1]==board[2][2]) //Diagonal
        { 
            if (board[0][0]==player) 
                return -10; 
            else if (board[0][0]==opponent) 
                return +10; 
        } 

        else if (board[0][2]==board[1][1] && board[1][1]==board[2][0]) //Diagonal
        { 
            if (board[0][2]==player) 
                return -10; 
            else if (board[0][2]==opponent) 
                return +10; 
        } 

        return 0; 
    } 

    public static void getBestMove(String[][] board)
    {
        int bestValue = -1000;
        Game bestMove = new Game(-1,-1);

        for(int i = 0; i < 3; i++)
        {
            for(int j = 0; j < 3; j++)
            {
                if(board[i][j] == "-")
                {
                    board[i][j] = player;
                    int currentValue = minimax(board, 0, false);
                    board[i][j] = "-";
                    if(currentValue > bestValue)
                    {
                        bestMove.row = i;
                        bestMove.col = j;
                        bestValue = currentValue;
                    }
                }


            }
        } 
        board[bestMove.row][bestMove.col]= opponent; 
    }
    public static int minimax(String [][] board, int depth, boolean isMaximizer)
    {
        if(checkState(board) != 0)
            return checkState(board);

        if(checkRemainingPlays(board) == false)
            return 0;

        if(isMaximizer)
        {
            int highest = -1000;
            for(int i = 0; i < 3; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(board[i][j] == "-")
                    {
                        board[i][j] = player;
                        highest = Math.max(highest,  minimax(board, depth + 1, !isMaximizer));
                        board[i][j] = "-";
                    }

                }
            }
            return highest;
        }
        else
        {
            int lowest = 1000;
            for(int i = 0; i < 3; i++)
            {
                for(int j = 0; j < 3; j++)
                {
                    if(board[i][j] == "-")
                    {
                        board[i][j] = opponent;
                        lowest = Math.min(lowest,  minimax(board, depth + 1, !isMaximizer));
                        board[i][j] = "-";
                    }
                }
            }
            return lowest;
        }
    }



    public static void playerTurn(String [][] board , Scanner input)
    {
        input = new Scanner(System.in);

        System.out.println("Player 1: ");


        System.out.println("Please enter the index of desired spot (I) ");
        int desiredIndexI = input.nextInt();
        System.out.println("Please enter the index of desired spot (J) ");
        int desiredIndexJ = input.nextInt();
        while(board[desiredIndexI][desiredIndexJ] != "-")
        {
            System.out.println("Please enter the index of desired spot (I) ");
            desiredIndexI = input.nextInt();
            System.out.println("Please enter the index of desired spot (J) ");
            desiredIndexJ = input.nextInt();
        }


        board[desiredIndexI][desiredIndexJ] = player;


    }

    public static boolean checkRemainingPlays(String [][] board)
    {
        for(int i = 0; i < board.length; i++) 
        {
            for(int j = 0; j < board[i].length; j++) 
            {
                if (board[i][j] == "-")
                    return true;
            }
        }
        return false;
    }

    public static void printBoard(String [][] board)
    {
        for(int i = 0; i < board.length; i++) 
        {
            if(i <= 2 && i > 0)
                System.out.println("----------");
            for(int j = 0; j < board[i].length; j++) 
            {
                if(j < 2)
                    System.out.print(board[i][j] + " | ");
                if(j == 2)
                    System.out.println(board[i][j]);
            }
        }
    }
    public static void fillBoard(String [][] board)
    {
        for(int i = 0; i < board.length; i++) 
        {
            for(int j = 0; j < board[i].length; j++) 
            {
                board[i][j] = "-";
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,我想从好消息开始。用minimax算法解决Tic Tac Toe是对人工智能初学者的一项很好的培训。问题并不容易,它很好地教了什么是AI计划。使用Java来实现游戏加求解器也是一个不错的选择,因为该语言随处可见,支持面向对象的编程并且执行起来非常合理。

现在,我想介绍关键方面。第一个问题是与AI有关的问题与正常的计算任务有很大不同。如果源代码是关于在屏幕上绘制AWT Java GUI,还是关于如何将参数发送到类,那么我相信这个问题可以轻松回答。在大多数情况下,AI主题不会因为编程问题而失败,这意味着如何使用某种编程语言,而是因为底层算法。

描述和解决AI问题的讨论空间不在编程领域内,而是在古腾堡(Gutenberg)星系内。这意味着,围绕TicTacToe的人工智能游戏和Minimax算法,至少有1000篇论文,书籍和PowerPoint演示文稿可用。新手的任务不是编写Java源代码,而是阅读和引用这些源代码。这有助于其他人了解问题并提供详细的反馈。

我知道,这个道德指导并没有回答最初的问题,但是目的是解释为什么我按下了“将问题迁移到https://ai.stackexchange.com/”按钮。在这个论坛中,问题将很快得到答案。