本文介绍了为什么错误的我的minimax算法连接4?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是新来的。

自上周以来,我一直冻结这些,我不知道为什么我的程序不起作用。真的,我觉得我知道为什么,我想因为极小极大功能。但我该怎么办。在这里你有与minimax相关的代码。

我认为每个函数都在代码中解释。如果您无法理解,请告诉我。



感谢您的帮助!



我尝试过:



Hi everybody, I'm new here.
I have been since last week freezed with these, I don't know why my program doesn't works. Really I think I know why, and I think because of the minimax function.But how should I do. Here you have the code related with minimax.
I think every function is explained in the code. If you can't understand something let me know.

Thank you for helping!

What I have tried:

<pre lang="java">

    public class Node {

        public int score;
        public int move;
        public ArrayList&lt;Node&gt; hijos;

        public Node () {
            score = -1;
            move = -1;
            hijos = new ArrayList&lt;&gt;();
        }

        public Node(int s, int m) {
            score = s;
            move = m;
        }

        public Node (Node n) {
            score = n.score;
            move = n.move;
        }

        public int getScore() {
            return score;
        }

        public void addHijo (Node n) {
            hijos.add(n);
        }
    }

    /**
     * The board of the game is in m_tablero
     * When function ends the move chosen will be at m_columna
     */
    public void minimax()
    {

        Node arbol = new Node();
        int v = 0;

        for (int i = 0; i &lt; 7; i++) {
            Tablero tablero_copia = new Tablero(m_tablero);
            //ponerFicha puts a token in a column for a player
            tablero_copia.ponerFicha(i,2);
            v = Max(tablero_copia, arbol, 1);
            arbol.addHijo(new Node(v, i));
        }

        v=0;
        for (Node item: arbol.hijos) {
            //It prints the moves and their scores
            System.out.println(&quot;Lista: Score-&gt;&quot; + item.score + &quot;, Move-&gt;&quot; + item.move);

            //Choose the best
            if (item.score &gt; v) {
                v = item.score;
                m_columna = item.move;
            }
        }



    }

    public int Max(Tablero tablero, Node n, int depth ) {
        int devuelve = -1;
        Tablero tablero_anterior = new Tablero (tablero);

        if (depth == NIVEL_DEFECTO) {
            //f function is the evaluation function, for getting a score given a board and a player
            return (f(tablero,2) - f(tablero,1));
          //cuatroEnRaya, returns if a player has won
        } else if (tablero.cuatroEnRaya() == 2) {
            devuelve = (Integer.MAX_VALUE) - f(tablero,1);
        } else if (tablero.tableroLleno()) {
            return 0;
        } else {
            for (int i = 0; i &lt; 7; i++) {
                tablero = tablero_anterior;
                tablero.ponerFicha(i, 1);
                devuelve = max(Min(tablero, n, depth+1), devuelve);
                n.addHijo(new Node(devuelve, i));
            }


        }
        return devuelve;
    }

    public int Min(Tablero tablero, Node n, int depth) {
        int devuelve = -1;

        Tablero tablero_anterior = new Tablero (tablero);

        if (depth == NIVEL_DEFECTO) {
            return f(tablero,1) - f(tablero,2);
        } else if (tablero.cuatroEnRaya() == 1) {
            devuelve = (Integer.MAX_VALUE) - f(tablero,2 );
        } else if (tablero.tableroLleno()) {
            return 0;
        } else {
            for (int i = 0; i &lt; 7; i++) {
                tablero = tablero_anterior;
                tablero.ponerFicha(i, 2);
                devuelve = min(devuelve, Max(tablero, n, depth+1));
                n.addHijo(new Node(devuelve, i));
            }

        }
        return devuelve;
    }

推荐答案


这篇关于为什么错误的我的minimax算法连接4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 00:45