为什么错误的我的minimax算法连接4? [英] Why is wrong my minimax algorithm for connect 4?

查看:120
本文介绍了为什么错误的我的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;
    }

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]

http: //docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your -first-java-application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较do。

当代码没有达到预期效果时,你就接近了一个bug。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆