Minimax算法始终返回一个数字

时间:2015-01-30 13:46:40

标签: java algorithm jframe tic-tac-toe minimax

我遇到了 minimax算法的问题。我想用AI制作tic tac toe游戏。我使用JFrame来做到这一点。 所以我的minimax算法总是返回9 ,我不知道为什么。怎么了?

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Zadanie2 extends JFrame implements ActionListener {

    /**
     * 
     */
    JButton button0 = new JButton("");
    JButton button1 = new JButton("");
    JButton button2 = new JButton("");
    JButton button3 = new JButton("");
    JButton button4 = new JButton("");
    JButton button5 = new JButton("");
    JButton button6 = new JButton("");
    JButton button7 = new JButton("");
    JButton button8 = new JButton("");
    JButton button9 = new JButton("");

    int playerSign = 1;
    String computerMark, opponentMark;
    public static final long serialVersionUID = 1L;
    JButton[] buttonArray = { button0, button1, button2, button3, button4,
            button5, button6, button7, button8, button9 };
    Object[] options = { "GRAJ OD NOWA", "ZAKOŃCZ GRĘ" };
    Object[] startOptions = { "GRACZ", "KOMPUTER", "OPUŚĆ GRĘ" };
    boolean canMove;
    boolean computerStarted;
    int bb = 1;

    public Zadanie2() {
        super("KÓŁKO I KRZYŻYK");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 300);
        setResizable(false);
        setLocation(470,400);
        setLayout(new GridLayout(3,3));


        for(int i = 1; i <= 9; i++){

            add(add(buttonArray[i]));
            buttonArray[i].addActionListener(this);
        }

        setVisible(true);

        int y = JOptionPane.showOptionDialog(null,"WYBIERZ KTO MA ZACZĄĆ GRĘ","WIADOMOŚĆ", JOptionPane.INFORMATION_MESSAGE, 
                JOptionPane.INFORMATION_MESSAGE,
                null,
                startOptions,
                startOptions[0]);

        if (y == 2) {
            System.exit(1);
        }

        if (y == 1) { // COMPUTER

            computerStarted = true;
            computerMark = "X";
            opponentMark = "O";
            canMove = true;
            computerMove();


        }

        if (y == 0) { // PLAYER

            computerStarted = false;
            computerMark = "O";
            opponentMark = "X";

        }

    }

    public static void main(String[] args) {
        new Zadanie2();
    }

    public void close() {

        playerSign = 1;
        dispose();
    }

    private void computerMove() {
        if(canMove){
            System.out.println("AI: "+AI(buttonArray));
            buttonArray[AI(buttonArray)].doClick();
            }
        canMove = false;
    }

    private int AI(JButton[] buttonArray2){
        int ruch, i, m, mmx;
          ruch = 0;
          mmx = -10;
          for(i = 1; i < 9; i++)
            if(buttonArray[i].getText() == "");
            {
                buttonArray[i].setText(computerMark);
                m = minimax(buttonArray, computerMark);
                buttonArray[i].setText("");
            if (m > mmx) {
                mmx = m;
                ruch = i;
            }       
            }    
          return ruch;
    }

    public int minimax(JButton[] buttonArray,String gracz){
        int m, mmx;

        if(win(buttonArray,gracz)) return (gracz == computerMark) ? 1 : -1;

        if(tie(buttonArray)){
            return 0;}

        gracz = (gracz == computerMark) ? opponentMark : computerMark;

         mmx = (gracz == opponentMark) ? 10 : -10;

         for(int i = 1; i <= 9; i++)
                if(buttonArray[i].getText() == "")
                {
                   buttonArray[i].setText(gracz);
                   m = minimax(buttonArray,gracz);
                   buttonArray[i].setText("");
                   if(((gracz == opponentMark) && (m < mmx)) || ((gracz == computerMark) && (m > mmx))){mmx = m;}
                }
              return mmx;
    }



    private void checkWin() {
        if(win(buttonArray,"X")){
            winX();
        }

        if(win(buttonArray,"O")){
            winO();
        }
        if(tie(buttonArray)){
            sayTie();
        }

    }




    private void winX() {
        int n = JOptionPane.showOptionDialog(null, "WYGRAŁ GRACZ X ",
                "WIADOMOŚĆ", JOptionPane.INFORMATION_MESSAGE,
                JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);

        if (n == 0) {
            close();
            new Zadanie2();

        }

        if (n == 1) {
            System.exit(1);
        }
        dispose();
    }

    private void winO() {
        int n = JOptionPane.showOptionDialog(null, "WYGRAŁ GRACZ O ",
                "WIADOMOŚĆ", JOptionPane.INFORMATION_MESSAGE,
                JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
        if (n == 0) {
            close();
            new Zadanie2();
        }

        if (n == 1) {
            System.exit(1);
        }
        dispose();
    }

    private void sayTie() {
        int n = JOptionPane.showOptionDialog(null, "REMIS! ", "WIADOMOŚĆ",
                JOptionPane.INFORMATION_MESSAGE,
                JOptionPane.INFORMATION_MESSAGE, null, options, options[0]);
        if (n == 0) {
            close();
            new Zadanie2();
        }

        if (n == 1) {
            System.exit(1);
        }
        dispose();
    }

    public boolean tie(JButton[] buttonArray2){

        for(int i = 1; i <= 9; i++){
            if(buttonArray2[i].getText() == ""){
            return false;
            }
        }
        return true;
    }

    private boolean win(JButton[] buttonArray2, String g) {

        if (buttonArray2[1].getText() == buttonArray2[2].getText()
                && buttonArray2[1].getText() == buttonArray2[3].getText()
                && buttonArray2[2].getText() == buttonArray2[3].getText() &&  buttonArray2[1].getText() == g) {
            return true;
        }

        if (buttonArray2[3].getText() == buttonArray2[7].getText()
                && buttonArray2[3].getText() == buttonArray2[5].getText()
                && buttonArray2[7].getText() == buttonArray2[5].getText() &&  buttonArray2[3].getText() == g) {
            return true;
        }

        if (buttonArray2[7].getText() == buttonArray2[8].getText()
                && buttonArray2[7].getText() == buttonArray2[9].getText()
                && buttonArray2[8].getText() == buttonArray2[7].getText() &&  buttonArray2[7].getText() == g) {
            return true;
        }

        if (buttonArray2[4].getText() == buttonArray2[5].getText()
                && buttonArray2[4].getText() == buttonArray2[6].getText()
                && buttonArray2[5].getText() == buttonArray2[6].getText() && buttonArray2[4].getText() == g) {
            return true;
        }

        if (buttonArray2[1].getText() == buttonArray2[5].getText()
                && buttonArray2[1].getText() == buttonArray2[9].getText()
                && buttonArray2[5].getText() == buttonArray2[9].getText() && buttonArray2[1].getText() == g) {
            return true;
        }

        if (buttonArray2[1].getText() == buttonArray2[4].getText()
                && buttonArray2[1].getText() == buttonArray2[7].getText()
                && buttonArray2[4].getText() == buttonArray2[7].getText()  && buttonArray2[1].getText() == g) {
            return true;
        }

        if (buttonArray2[2].getText() == buttonArray2[8].getText()
                && buttonArray2[2].getText() == buttonArray2[5].getText()
                && buttonArray2[8].getText() == buttonArray2[5].getText() && buttonArray2[2].getText() == g) {
            return true;
        }

        if (buttonArray2[3].getText() == buttonArray2[6].getText()
                && buttonArray2[3].getText() == buttonArray2[9].getText()
                && buttonArray2[6].getText() == buttonArray2[9].getText() && buttonArray2[3].getText() == g) {
            return true;
        }

        return false;
    };


    @Override
    public void actionPerformed(ActionEvent e) {


        Object source = e.getSource();

        for (int i = 1; i <= 9; i++) {
            if (source == buttonArray[i]  ) {
                playerSign++;
                if (playerSign % 2 == 0 && buttonArray[i].getText() == "") {
                    buttonArray[i].setText("X");

                }
                if (playerSign % 2 != 0 && buttonArray[i].getText() == "") {
                    buttonArray[i].setText("O");

                }

                if (computerStarted && playerSign % 2 != 0) {
                    canMove = true;
                    computerMove();
                }

                if (!computerStarted && playerSign % 2 == 0) {
                    canMove = true;
                    computerMove();
                }

            }

        }
        System.out.println("PS: " + playerSign);
        if(playerSign > 3){
        checkWin();}
    }


}

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:1)

每当您比较两个.equals时,您应该使用==而不是Object

E.g。应该有computerMark.equals(gracz)而不是gracz == computerMark


修改

此外,您应该分别用10和-10替换1和-1:

if(win(buttonArray,gracz)) return (gracz == computerMark) ? 1 : -1;

Reference