/*
 *  structure.cpp
 *  Untitled
 *
 *  Created by Lionel BUCHELE on 30/03/11.
 *  Copyright 2011 EFREI. All rights reserved.
 *
 */

#include "Buchele-Thebault-structure.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>


// Initialisation des couleurs sous forme de chiffre

void Initialisation(pcube rubi, int i)
{
	if(i==0)
	{
		int index = 0;
		for(index = 0; index<6;index++)
			rubi->face[index] = (pcote)malloc(sizeof(cote));
		rubi->suivant = NULL;
		rubi->cost = 0;
	}
	if(i>5)return;
	if(i<4)
	{
		rubi->face[i]->droite = rubi->face[4];
		rubi->face[i]->gauche = rubi->face[5];
		if(i<3)rubi->face[i]->haut = rubi->face[i+1];
		else 
		{
			rubi->face[i]->haut = rubi->face[0];
			rubi->face[0]->bas = rubi->face[i];
		}
		if(i != 0)rubi->face[i]->bas = rubi->face[i-1];
	}
	else {
		if(i==4)
		{
			rubi->face[i]->droite = rubi->face[2];
			rubi->face[i]->gauche = rubi->face[0];
			rubi->face[i]->haut = rubi->face[1];
			rubi->face[i]->bas = rubi->face[3];
		}
		if(i==5)
		{
			rubi->face[i]->droite = rubi->face[0];
			rubi->face[i]->gauche = rubi->face[2];
			rubi->face[i]->haut = rubi->face[1];
			rubi->face[i]->bas = rubi->face[3];
		}
	}
	
	rubi->face[i]->cases[0][0] = i;
	rubi->face[i]->cases[0][1] = i;
	rubi->face[i]->cases[0][2] = i;
	rubi->face[i]->cases[1][0] = i;
	rubi->face[i]->cases[1][1] = i;
	rubi->face[i]->cases[1][2] = i;
	rubi->face[i]->cases[2][0] = i;
	rubi->face[i]->cases[2][1] = i;
	rubi->face[i]->cases[2][2] = i;
	i++;
	rubi->face[i-1]->name = i;
	
	Initialisation(rubi,i);
	
}

// Destrutction du rubix cube
void Destruction(pcube rubi)
{
	int index = 0;
	pcube temp;
	while(rubi != NULL)
	{
		for(index = 0; index<6;index++)
			free(rubi->face[index]);
		temp = rubi;
		rubi = rubi->suivant;
		free(temp);
	}
}

// Destrutction d'un noeud
void DestructionNode(pcube rubi)
{
	int index = 0;

	for(index = 0; index<6;index++)
		free(rubi->face[index]);
	free(rubi);
}


pcube copy(pcube copier, int index)
{
	if(index == 18)return NULL;
	pcube solution;
	solution = (pcube)malloc(sizeof(cube));
	
	int i = 0;
	for(i = 0; i<6;i++)
		solution->face[i] = (pcote)malloc(sizeof(cote));
	solution->suivant = NULL;
	
	copyFace(solution->face[0], copier->face[0], solution);
	copyFace(solution->face[1], copier->face[1],solution);
	copyFace(solution->face[2], copier->face[2],solution);
	copyFace(solution->face[3], copier->face[3],solution);
	copyFace(solution->face[4], copier->face[4],solution);
	copyFace(solution->face[5], copier->face[5],solution);
	
	index++;
	solution->suivant = copy(copier, index);
	return solution;
}

void copyFace(pcote a, pcote b, pcube c)
{
	a->cases[0][0] = b->cases[0][0];
	a->cases[0][1] = b->cases[0][1];
	a->cases[0][2] = b->cases[0][2];
	a->cases[1][0] = b->cases[1][0];
	a->cases[1][1] = b->cases[1][1];
	a->cases[1][2] = b->cases[1][2];
	a->cases[2][0] = b->cases[2][0];
	a->cases[2][1] = b->cases[2][1];
	a->cases[2][2] = b->cases[2][2];
	
	a->name = b->name;
	
	a->droite = c->face[b->droite->name - 1];
	a->gauche = c->face[b->gauche->name - 1];
	a->haut = c->face[b->haut->name - 1];
	a->bas = c->face[b->bas->name - 1];
}



//Affiche le cube
void affichageCube(pcube rubi, int i)
{
	if(i>3)return;
	int index = 0;
	//printf("Face n° %d\n",rubi->face[3 - i]->name);
	if(i<3)
	{
		for(index = 0; index<3;index++)
		{
			printf("               ------------\n              | %d | %d | %d |\n",rubi->face[3 - i]->cases[index][0],rubi->face[3- i]->cases[index][1],rubi->face[3 - i]->cases[index][2]);
		}
		printf("               ------------\n");
	}
	else {
		{
			for(index = 0; index<3;index++)
			{
				printf("------------   ------------  ------------\n| %d | %d | %d | | %d | %d | %d | | %d | %d | %d |\n",rubi->face[4]->cases[index][0],rubi->face[4]->cases[index][1],rubi->face[4]->cases[index][2],rubi->face[3 - i]->cases[index][0],rubi->face[3- i]->cases[index][1],rubi->face[3 - i]->cases[index][2],rubi->face[5]->cases[index][0],rubi->face[5]->cases[index][1],rubi->face[5]->cases[index][2]);
			}
			printf("------------   ------------  ------------\n");
			
		}
	}

	i++;
	affichageCube(rubi, i);
}
