Exemplo n.º 1
0
double MiniMax(const Environment & tablero,int jug,int prof,int limite,Environment::ActionType & accion){
    const double INF=1000000000000.0, mINF=-1000000000000000.0, gano=999999999999999, pierdo=-999999999999999999;
    Environment::ActionType accion_anterior;
    double mejor, aux;
    if(prof==limite or tablero.JuegoTerminado()){
            return ValoracionTest(tablero,jug);
    }
    else{
        if(prof%2==0)
            mejor=mINF;
        else
            mejor=INF;
        int ult_act=-1;
        Environment hijo=tablero.GenerateNextMove(ult_act);
        while(!(hijo==tablero)){
            aux=MiniMax(hijo,jug,prof+1,limite,accion_anterior);
            if(prof%2==0){
                    if(aux>mejor){
                    mejor=aux;
                    accion=static_cast <Environment::ActionType>(ult_act);
                    }
                }
            else{
                if(aux<mejor){
                    mejor=aux;
                    accion=static_cast<Environment::ActionType>(ult_act);
                }
            }
            hijo=tablero.GenerateNextMove(ult_act);
        }
        return mejor;

        }
    }
Exemplo n.º 2
0
double Player::Poda_AlfaBeta(const Environment &tablero, int jugador, int prof, const int MAX_PROF,
    Environment::ActionType &accion, double alfa, double beta){
    if(prof==MAX_PROF || tablero.JuegoTerminado())
        return Valoracion(tablero, jugador);
    else{
        int last_accion=-1;
        double  temporal;
        Environment::ActionType accion_previa;
        Environment hijo=tablero.GenerateNextMove(last_accion);

        //NODO ALFA
        if((prof%2)==0){
            while(!(hijo==tablero)){
                temporal=Poda_AlfaBeta(hijo, jugador, prof+1,MAX_PROF, accion_previa, alfa, beta);
                // Si existe una jugada mejor, actualizamos el alfa.
                if(temporal > alfa){
                    alfa=temporal;
                    accion=static_cast<Environment::ActionType>(last_accion);
                }
                if(beta<=alfa){
                    break;
                }
                hijo=tablero.GenerateNextMove(last_accion);
            }
            return alfa;
        }

        //NODO BETA
        else{
            while(!(hijo==tablero)){
                temporal=Poda_AlfaBeta(hijo, jugador, prof+1,MAX_PROF, accion_previa, alfa, beta);
                // Si existe una jugada peor, actualizamos el beta.
                if(temporal < beta){
                    beta=temporal;
                    accion=static_cast<Environment::ActionType>(last_accion);
                }
                if(beta<=alfa){
                    break;
                }
                hijo=tablero.GenerateNextMove(last_accion);
            }
            return beta;
        }
    }
}
Exemplo n.º 3
0
double Poda_alphaBetha(const Environment & tablero,int jug,int prof,int limite,Environment::ActionType & accion,double alpha,double betha){
    Environment::ActionType accion_anterior=accion;
    double aux;
    if(prof==limite or tablero.JuegoTerminado()){
            return Valoracion(tablero,jug);
    }
    else{
        int ult_act=-1;
        Environment hijo=tablero.GenerateNextMove(ult_act);
        while(!(hijo==tablero)){
            aux=Poda_alphaBetha(hijo,jug,prof+1,limite,accion_anterior,alpha,betha);
            if(prof%2==0){
                if(aux>alpha){
                    alpha=aux;
                        if(prof==0)
                    		accion=static_cast <Environment::ActionType>(ult_act);
                }

            }
            else{
                if(aux<betha){
                    betha=aux;
                    //if(prof==0)
                       // accion=static_cast<Environment::ActionType>(ult_act);
                }

            }
            if(betha<=alpha)
                return alpha;
            hijo=tablero.GenerateNextMove(ult_act);
        }
	if (prof%2==0)
		return alpha;
	else
		return betha;

        }
    }
Exemplo n.º 4
0
double PodaAlfaBeta(const Environment & tablero, int jugador, int profundidad, int profLimite, Environment::ActionType & accion, double alfa, double beta){
      Environment::ActionType ult_accion;
      

       //Profundidad máxima o fin del juego, se acabó
       if(profundidad == profLimite || tablero.JuegoTerminado())   
           return Valoracion(tablero, jugador);
       else{ // Hijos                                                                                                 
          int ult_act = -1;
          //Generar siguiente nodo tablero hijo
           Environment hijo = tablero.GenerateNextMove(ult_act);  

           //Profundidad par --> nodo MAX
          if(profundidad%2 == 0){                                                                             

           //Recorrido de nodos hijos
          while(!(hijo==tablero)){                                              

            double aux = PodaAlfaBeta(hijo, jugador, profundidad+1,profLimite,ult_accion,alfa,beta);

             // if (profundidad == 0)
                //cout << "Acción -> " << ult_act << endl;
              if(aux > alfa){
                alfa = aux;
                if (profundidad == 0)
                  accion = static_cast<Environment::ActionType> (ult_act);       
              } 

              if(beta <= alfa){                                                   
                return alfa; //poda
              } else {
             //Generación del siguiente nodo tablero hijo
                hijo = tablero.GenerateNextMove(ult_act); 
              }

          }

          return alfa; // no poda
          

          }else{                                                                     
            //Nivel impar -> nodo MIN
            while(!(hijo==tablero)){                                                

              double aux = PodaAlfaBeta(hijo, jugador, profundidad+1, profLimite, ult_accion, alfa, beta);

              if(aux < beta)
                beta = aux;       

              if(beta <= alfa){                                                 
                return beta;  //poda
              } else{
                hijo = tablero.GenerateNextMove(ult_act);          
              }                 

          }

          return beta; // no poda

      }


    }
}