Public Function negamax(tablero As ClassTablero, jugador As Integer, profundidad As Integer, alfa As Integer, beta As Integer) As Variant[]
Dim alfa_local, max_puntuacion, jugada, puntuacion As Integer
Dim tableroaux As ClassTablero
Dim jugada_max As Integer
Dim JugadasPosibles As Integer[]
Dim contador As Integer
Dim opcion As Integer
Dim valorjugadorContrario As Integer = 0
Dim valorposicionContrario As Integer = 0
max_puntuacion = - main.maximoInteger - 1
alfa_local = alfa
\'hago un array de las jugadas que puedo hacer segun el tablero recibido.
jugadasPosibles = tablero.JugadasPosibles()
\'repite por cada jugada posible
tableroaux = New ClassTablero(tablero)
For contador = 0 To jugadasPosibles.max
tableroaux.insertaficha(JugadasPosibles[contador], jugador)
\' tableroaux.muestra
If tableroaux.GameOver() Or profundidad = 0 Then
Return [EvaluaJugadaMov(tableroaux, jugador), JugadasPosibles[contador]]
Else
puntuacion = - negamax(tableroaux, jugador * (-1), profundidad - 1, - beta, - alfa_local)[0]
tableroaux.deshacer(jugadasPosibles[contador])
If puntuacion > max_puntuacion Then
max_puntuacion = puntuacion
jugada_max = JugadasPosibles[contador]
Else If puntuacion = max_puntuacion Then
\'de un modo aleatorio elijo una de las dos... (para que las partidas no sean iguales)
opcion = Int(Rnd(0, 2))
\'Print opcion; " ";
If opcion = 0 Then
max_puntuacion = puntuacion
jugada_max = JugadasPosibles[contador]
Endif
\'poda alfa beta
If max_puntuacion > alfa_local Then
alfa_local = max_puntuacion
Endif
\'poda....
If alfa_local >= beta Then
\'comprueba si hay lista de jugadas iguales...
Return [alfa_local, jugada_max]
Endif
Endif
Endif
Next
Return [max_puntuacion, jugada_max]
End