Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

C#
<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;
    }
Posted
Updated 7-Oct-16 7:16am
Comments
[no name] 7-Oct-16 11:35am    
Learning to use the debugger and using it to debug your code is a useful skill to develop.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900