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<Node> hijos;
public Node () {
score = -1;
move = -1;
hijos = new ArrayList<>();
}
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);
}
}
public void minimax()
{
Node arbol = new Node();
int v = 0;
for (int i = 0; i < 7; i++) {
Tablero tablero_copia = new Tablero(m_tablero);
tablero_copia.ponerFicha(i,2);
v = Max(tablero_copia, arbol, 1);
arbol.addHijo(new Node(v, i));
}
v=0;
for (Node item: arbol.hijos) {
System.out.println("Lista: Score->" + item.score + ", Move->" + item.move);
if (item.score > 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) {
return (f(tablero,2) - f(tablero,1));
} else if (tablero.cuatroEnRaya() == 2) {
devuelve = (Integer.MAX_VALUE) - f(tablero,1);
} else if (tablero.tableroLleno()) {
return 0;
} else {
for (int i = 0; i < 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 < 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;
}