Advertisement
jsbsan

negamax y evalua

Oct 4th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GAMBAS 2.78 KB | None | 0 0
  1. ' Gambas module file
  2.  
  3. Public Function negamax(tablero As ClassTablero, jugador As Integer, profundidad As Integer, alfa As Integer, beta As Integer) As Variant[]
  4.  
  5.   Dim alfa_local, max_puntuacion, jugada, puntuacion As Integer
  6.   Dim tableroaux As ClassTablero
  7.   Dim jugada_max As Integer
  8.   Dim JugadasPosibles As Integer[]
  9.   Dim contador As Integer
  10.   Dim opcion As Integer
  11.   Dim valorjugadorContrario As Integer = 0
  12.   Dim valorposicionContrario As Integer = 0
  13.  
  14.   max_puntuacion = - main.maximoInteger - 1
  15.   alfa_local = alfa
  16.  
  17.   'hago un array de las jugadas que puedo hacer segun el tablero recibido.
  18.   jugadasPosibles = tablero.JugadasPosibles()
  19.   If jugadasPosibles.count = 0 Then Print "Cuidado juagdas posibles=0"
  20.   'repite por cada jugada posible
  21.   tableroaux = New ClassTablero(tablero)
  22.   For contador = 0 To jugadasPosibles.max
  23.    
  24.     tableroaux.insertaficha(JugadasPosibles[contador], jugador)
  25.     '  tableroaux.muestra
  26.     If tableroaux.GameOver() Or profundidad = 0 Then
  27.       Return [EvaluaJugadaMov(tableroaux, jugador), JugadasPosibles[contador]]
  28.     Else
  29.      
  30.       puntuacion = - negamax(tableroaux, jugador * (-1), profundidad - 1, - beta, - alfa_local)[0]
  31.       tableroaux.deshacer(jugadasPosibles[contador])
  32.       If puntuacion > max_puntuacion Then
  33.         max_puntuacion = puntuacion
  34.         jugada_max = JugadasPosibles[contador]
  35.        
  36.       Else If puntuacion = max_puntuacion Then
  37.         'de un modo aleatorio elijo una de las dos... (para que las partidas no sean iguales)
  38.        
  39.         opcion = Int(Rnd(0, 2))
  40.        
  41.         'Print opcion; " ";
  42.        
  43.         If opcion = 0 Then
  44.           max_puntuacion = puntuacion
  45.           jugada_max = JugadasPosibles[contador]
  46.         Endif
  47.         'poda alfa beta
  48.        
  49.         If max_puntuacion > alfa_local Then
  50.           alfa_local = max_puntuacion
  51.         Endif
  52.         'poda....
  53.         If alfa_local >= beta Then
  54.           'comprueba si hay lista de jugadas iguales...
  55.          
  56.           Return [alfa_local, jugada_max]
  57.          
  58.         Endif
  59.        
  60.       Endif
  61.      
  62.     Endif
  63.    
  64.   Next
  65.  
  66.   Return [max_puntuacion, jugada_max]
  67.  
  68. End
  69.  
  70. Public Sub EvaluaJugadaMov(tablero As ClassTablero, jugador As Integer) As Integer
  71.  
  72.   Dim valor_jugada, valor As Integer
  73.   Dim n2, n3, n4 As Integer
  74.   Dim j2, j3, j4 As Integer
  75.   Dim eva As Integer[]
  76.  
  77.   eva = tablero.comprueba_linea(4, jugador)
  78.   n4 = eva[1]
  79.   j4 = eva[0]
  80.  
  81.   If n4 = 0 Then
  82.     'mejora de rendimiento  (version 0.0.4),
  83.     eva = tablero.comprueba_linea(2, jugador)
  84.     n2 = eva[1]
  85.     j2 = eva[0]
  86.    
  87.     eva = tablero.comprueba_linea(3, jugador)
  88.     n3 = eva[1]
  89.     j3 = eva[0]
  90.    
  91.   Endif
  92.  
  93.   valor_jugada = (4 * n2 + 9 * n3 + 100000 * n4)
  94.  
  95.   Return valor_jugada
  96.  
  97. End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement