/*
 *  MinMax.cpp
 *  TicTaeToe
 *
 *  Created by Lionel BUCHELE on 03/05/11.
 *  Copyright 2011 EFREI. All rights reserved.
 *
 */

#include "Thebault-Buchele-MinMax.h"
#include "Thebault-Buchele-plateau.h"

int MinMax::profondeurDonnee = 0;

MinMax::MinMax()
{
}

MinMax::MinMax(Case *a, int prof)
{
	this->coup = a;
	this->profondeurActuelle = prof;
	this->victoire = false;
	this->feuille = true;
	this->alpha = 9999999;
	this->beta = -9999999;
}


// _________________________
// Date : 4 mai 2011
// Function : Parcours l'abre pour enclencher déclencher les coup
// 1 : enclenche 
// 0 : remet à zéro
// _________________________
void MinMax::parcourirArbre(int cas)
{
	
	if(this->predecesseur != NULL)this->predecesseur->parcourirArbre(cas);
}


// _________________________
// Date : 4 mai 2011
// Function : Remonte l'arbre et affecte la bonne valeur
// _________________________
void MinMax::AffectationHeuristique()
{
	cout<<"Profondeur :"<<this->profondeurActuelle<<endl<<"Cout : "<<this->heuristique<<endl<<endl;	
	//if(this->predecesseur != NULL){this->profondeurActuelle--;this->predecesseur->AffectationHeuristique();}
}


// _________________________
// Date : 4 mai 2011
// Function : Suppression d'une branche
// _________________________
void MinMax::suppressionBranche(MinMax *c)
{
	if(this == c && this->feuille == true)return;
	if(this->feuille == true)
	{
		MinMax *temp = this->predecesseur;
		for(int i = 0; i<this->successeur.size(); i++)delete this->successeur[i];
		temp->feuille = true;
		temp->suppressionBranche(c);
	}
	else 
	{
		for (int i = 0; i<this->successeur.size(); i++)
		{
			this->successeur[i]->suppressionBranche(c);
		}
	}
}

// _________________________
// Date : 5 mai 2011
// Function : Affectation d'une valeur à une feuille
// _________________________
void MinMax::AffectationHeuristiqueFeuille()
{	
	plateau *game = plateau::getInstance();
	this->heuristique = game->heuristique();
	this->alpha = this->heuristique;
	this->beta = this->heuristique;
	cout<<"Heuristique : "<<this->heuristique<<endl;
	game->affichage();
	cout<<endl<<endl;
}

void MinMax::suppression()
{
	if(this->feuille == false)for(int x = 0; x<this->successeur.size();x++)this->successeur[x]->suppression();
	delete this;
	
}