/*
	hex.js

	Copyright (C) 2007 Jean-Paul Davalan <jeux-et-mathematiques(AT)davalan.org>

	
               y
    O_z       /    x+y<=3n-5    z = n-1-y
     * . . . o o X o o . . . .
      . . . o o o o o X . . .    découvrez les erreurs ( plusieurs X sur une
       . . X o o o o o o . .     même ligne horizontale ou transversale 
x+y>=n-1. o o o o o o X o .      / NE-SW ou \ NW-SE)
         o o o X o o o o o       n=5   9 lignes de 5, 6, 7, 8, 9, 8, 7, 6, 5 cases
        . X o o o o o o o        n, n+1 ... 2n-1 ... n
       . . o o o o o o X         2*(n+ n+1+ ... +2n-2)+2n-1 = n(n-1)+2n-1
      . . . o X o o o o		 = n^2+n-1 = n(n+1)-1
 O_z * . . . o o o X o -> x      2n-1 lignes
    O_x        \
               z
             
les éléments sont codés de 0 à 2n-2 inclus sur une ligne
les lignes sont aussi codées de 0 à 2n-2
sur une ligne j, les éléments vont
j = 0     deb = n-1    fin = 2n-2
    1           n-2          2n-2
    j           n-j-1        2n-2
   n-1          0            2n-2
   n            0            2n-3
   n+1          0            2n-4
    j           0            3n-j-3
  2n-2          0            n-1

pour l'applet java (qui construit un stable maximum au hasard)

/usr/local/j2sdk1.4.2_05/bin/javac -classpath java40.jar hexalea.java

*/

var Im = new Array();

Im[0] = new Image(); Im[0].src="cercle1fade.png"
Im[1] = new Image(); Im[1].src="cercle1.png"
Im[2] = new Image(); Im[2].src="cercle2.png"

var radio=true;

function changeRadio() {
	radio= !radio;
	h.fade(radio)
}
var cellw=44, cellw2=cellw/2, cellh=38;
var cellimg0="cercle1.png", cellimg1="cercle2.png", cellvide="demi.png";
var commt = 'Choisissez éventuellement une autre dimension n puis cliquez sur le bouton [<span style=\'cursor:pointer;color:blue\' onclick="cherche()">Commence</span>] avant de commencer une nouvelle partie'
var blocage=[];

var solution = [], current = "", soltable = [], nsols=0, minitble=[];

for(var i=1; i<100; i++) blocage[i]=2*i-1;

function hex(n) {
	this.n=n;
	this.nn=2*n-1;
	this.nb=0;
	this.tb=[]
	this.pos=[]
	this.pos[0]=[]; this.pos[1]=[]
	this.ga=[];
	this.gb=[];
	this.gc=[];

        this.dessine = function() {
                var w, r, t, s=''
                for(var i=0; i<2*n-1; i++) {
			if(i<n) {		
                        	w = n-i;
                        	s += '<img src="demi.png" width="'+(1+w*cellw2)+'" height="'+(cellh)+'" alt="">';
                        	for(var j=w; j<w+n+i ;j++) {
					r=j-w
					t=i+" "+r
					this.pos[0][t]=i; this.pos[1][t]=r; // this.ga[i]=0;this.gb[r]=0;this.gc[2*n+i-r]=0;
                                	s += '<img name="'+i+' '+r+'" src="cercle1.png" + width="44" height="38" onclick="h.fu('+i+','+r+')"  alt="">'
					this.tb[i+' '+r]=false
                        	}
                
			} else {
				w=i-n+2
				s += '<img src="demi.png" width="'+(1+w*cellw2)+'" height="'+(cellh)+'" alt="">';
                        	for(var j=w; j<4*n-j-1 ;j++) {
					r=j-1
					t=i+" "+r
					this.pos[0][t]=i; this.pos[1][t]=r; // this.ga[i]=0;this.gb[r]=0;this.gc[2*n+i-r]=0;
                                	s += '<img name="'+i+' '+r+'" src="cercle1.png" + width="44" height="38" onclick="h.fu('+i+','+r+')" alt="">'
					this.tb[i+' '+r]=false
                        	}


			}
                        s += '<br />'
			
                }
		
                return s;
        }
	this.fu = function(a, b) {
	        var u=a+' '+b;
        	this.tb[u]= ! this.tb[u]
	        document.images[u].src=(this.tb[u])? "cercle2.png":"cercle1.png";
		//document.images[u].src=(this.tb[u])? Im[2].src : Im[1].src;
        	if(this.tb[u]==true && ! this.teste()) {
                	this.tb[u]= ! this.tb[u]
                	document.images[u].src=(this.tb[u])? "cercle2.png":"cercle1.png";
			//document.images[u].src=(this.tb[u])? Im[2].src : Im[1].src;
        	}
		if(this.tb[u]==false) {
			document.getElementById("oooh").innerHTML="Jeu en cours<br /><br />"
		}
		if(radio) {
			this.fade(true);
		} else {
			this.fade(false);
		}
	}
	this.efface = function() { 
		for(var x in this.tb) {
			if(this.tb[x]==true) {
				var w = x.split(/\s+/g);
				this.fu(w[0],w[1]);
			}
		}
	}

			
	this.teste = function() {
        	var n=this.n;
	        this.nb=0;

        	for(var i=0; i<=4*n; i++) {
                	this.ga[i]=this.gb[i]=this.gc[i]=0;
        	}
		for(var x in this.tb) 
                        if(this.tb[x]==true) 
                                this.nb++
		
	        for(var x in this.tb) {
                	if(this.tb[x]==true) {
				var a=this.pos[0][x]
				var b=this.pos[1][x]
                        	var c=2*n+a-b
                        	this.ga[a]++
                        	this.gb[b]++
                        	this.gc[c]++
                        	if(this.ga[a]>1 || this.gb[b]>1 || this.gc[c]>1)
                                	return false;
                	}
        	}
	        if(this.nb==2*n-1) {
			var nveau = this.code();
			var st='<span class="s1">Puzzle 1 :</span><br /><br />'
			if(nveau) {
				st += '<strong>Félicitations</strong>  (cette solution n° '+nsols+' est nouvelle)'
			} else {
				st += '<strong>Bravo</strong>  (cette solution n° '+(1+solution[current])+' avait déjà été trouvée)';
			}
			st += '<br /><br /><span class="s1">Puzzle 2 :</span>  Le record actuel est toujours de '+blocage[this.n]
			st += '<br /><br />'+commt
                	document.getElementById("oooh").innerHTML=st
		} else if(this.bloque()) {
			st='Jeu bloqué<br /><br />'+
				'<span class="s1">Puzzle 1 :</span>  modifiez un ou plusieurs sommets<br />'+
			        '<span class="s1">Puzzle 2 :</span> le nombre de sommets cochés est de '+this.nb+'<br />'
			if(this.nb<=blocage[this.n]) {
				if(this.nb ==blocage[this.n]) {
					st1 = 'L\'ancien record '+blocage[this.n]+' est égalé'
				} else {
					st1 = 'L\'ancien record '+blocage[this.n]+' est amélioré'
					blocage[this.n]=this.nb;
				}
                                minitble[this.n] += "\n"+this.codeStr();
			} else {
				st1 = 'Le record actuel est toujours de '+blocage[this.n]
			}
			document.getElementById("oooh").innerHTML=st+st1+"<br /><br />"+commt
		} else {
			document.getElementById("oooh").innerHTML="Jeu en cours<br /><br />"
		}
        	return true;
	}
	
        this.bloque = function() {
                var nb=0, n=this.n;
		for(var x in this.tb) {
			var a=this.pos[0][x]
                        var b=this.pos[1][x]
                        var c=2*n+a-b
			if(this.ga[a] ==0 && this.gb[b] ==0  && this.gc[c] ==0)
				return false
		}
                return true;
        }
	this.fade = function(md) {
                var n=this.n, nn=this.nn,nx=0,nb=0,a, b, c;
		for(var i=0; i<4*n; i++) {
			this.ga[i] = this.gb[i] = this.gc[i] =0;
		}
		for(var x in this.tb) {
			if(this.tb[x]==true) {
				nx++;
				a=this.pos[0][x]; this.ga[a]++;
				b=this.pos[1][x]; this.gb[b]++;;
				c=2*n+a-b; this.gc[c]++;
			}
		}
                for(var x in this.tb) {
			if(this.tb[x]==false) {
                        	a=this.pos[0][x]
	                        b=this.pos[1][x]
        	                c=2*n+a-b
                	        if(md==true && (this.ga[a]>0 || this.gb[b]>0  || this.gc[c]>0)) {
						document.images[x].src="cercle1fade.png";
				} else {
					document.images[x].src="cercle1.png";
				}
                        }
                }
        }
	this.positions = function() {
		var s1="", s=this.n+":";
		
		for(var x in this.tb) {
                        if(this.tb[x]==true) {
				var a = x.split(/\s+/g), z=this.n-1+parseInt(a[0])-parseInt(a[1])
				s +=s1+"("+x+" "+z+")"
				s1=" ; "
			}
		}
		return s;
	}
	this.codeStr = function () {
               var s=this.n+":"
                for(var x in this.tb) {
                        if(this.tb[x]==true) {
                                s += x+";"
                        }
                }
		return s;
	}

	this.code = function () {
        	var s=this.n+":"
		for(var x in this.tb) {
			if(this.tb[x]==true) {
                        	s += x+";"
                	}
        	}
		current = s
		if(solution[s]==null) {
                	soltable[nsols]=s
	                solution[s]=nsols
        	        nsols++
                	return true
	        }
        	return false
	}

	this.expose = function(s) {
		var v=s
		v = v.replace(/\;\s*$/,"")
		v = v.replace(/\s*[\(\)]\s*/g,"")
		var t=v.split(/\s*;\s*/g)
		for(var i=0; i< t.length; i++) {
			var w=t[i].split(/[\s\,]+/g);
			this.fu(w[0], w[1]);
		}
	}

}
// var solution = [], current = "", soltable = [], nsols=0, minitble=[];


var n=5

var h=null;

function cherche() {
	document.getElementById("oooh").innerHTML=""
	if(h!=null) delete h;
	n = parseInt(document.frm.n.value)
	if(n<=0) n=1;
	document.frm.n.value=n;
	document.getElementById("aaah").style.width=2*n*44+100;
	h = new hex(n);
	var s=h.dessine();
	document.getElementById("aaah").innerHTML=s;
	document.getElementById("oooh").innerHTML=commt;
}

function add(k) {
	n = parseInt(document.frm.n.value)+k
        if(n<=0) n=1;
        document.frm.n.value=n;
	cherche()
}

function revoir(k) {
	var s = soltable[k].replace(/\s*[\(\)]\s*/g,"")
	var u = s.split(/\s*:\s*/g);
	document.frm.n.value=parseInt(u[0]);
	cherche();
	h.expose(u[1]);
}

var histcur=0;

function hist(x) {
	histcur +=x
	if(histcur>=nsols) histcur=nsols-1
	if(histcur<0) histcur=0
	revoir(histcur);
}

function libre() {
	h.fade(true);
}

function prpr(s) {
	var s1 = s+"";
	s1=s1.replace(/\s*[\(\)]\s*/g,"")
	var u = s1.split(/\s*:\s*/g);
        document.frm.n.value=parseInt(u[0]);
        cherche();
        h.expose(u[1]);
	document.location.href="#APP";
}

function halea() {
	var n=parseInt(document.frm.n.value)
	document.R.hexalea(n);
}
function hnoyauxalea() {
	document.S.stop();
        var n=parseInt(document.frm.n.value)
        //alert(document.S+"\n"+document.S.hexanoyauxalea)
        //if(document.S && document.S.hexanoyauxalea)
                document.S.hexanoyauxalea(n);
	//document.location.href="#APP"
}

function lespositions() {
	var s = h.positions();
	document.getElementById("oooh").innerHTML="<pre>"+s+"</pre>";
}

//var solution = [], current = "", soltable = [], nsols=0, minitble=[];
function insere() {
	var s = document.frm.cde.value;
	s = s.replace(/^\n+/,"");
	s = s.replace(/\n+$/,"");
	s = s.replace(/\n+/g,"\n");
	document.frm.cde.value=s;
	var ss1=s.split(/\n+/g);
	var c = soltable.length;
	for(var i=0; i<ss1.length; i++) {
		s1=ss1[i].replace(/\s*[\(\)]\s*/g,"")
		soltable.push(s1);
	}
	nsols=soltable.length;
	current = c;
	s1=ss1[0].replace(/\s*[\(\)]\s*/g,"")
	var u = s1.split(/\s*:\s*/g);
	document.frm.n.value=parseInt(u[0]);
	cherche();
	h.expose(u[1]);
}

function insere2() {
        var s = document.frm.cde.value;
        s = s.replace(/^\n+/,"");
        s = s.replace(/\n+$/,"");
        s = s.replace(/\n+/g,"\n");
        document.frm.cde.value=s;
        var ss1=s.split(/\n+/g);
        for(var i=0; i<ss1.length; i++) {
                s1=ss1[i].replace(/\s*[\(\)]\s*/g,"")
                var u = s1.split(/\s*:\s*/g);
                document.frm.n.value=parseInt(u[0]);
                cherche();
                h.expose(u[1]);
        }

}

function efface() {
	document.frm.cde.value="";
}

function affichesols() {
	var u=[], s="";
	for(var i=0; i<soltable.length; i++) {
		var x = soltable[i];
		if(u[x] == null) {
			s += x+"<br />\n"
			u[x]=1
		}
	}
	if(s!="")
		s += "<br />\n";
	for(var x in minitble) {
		var str=minitble[x];
		str=str.replace(/[\s\n]+$/,"");
		str=str.replace(/^[\s\n]+/,"");
		if(str !=null &&  str !="" && str !="undefined") {
			var t= str.split(/\n+/g);
			for(var j=0; j<t.length; j++)  {
				if(t[j]!="undefined" && t[j]!=null && u[t[j]]==null) {
					s += t[j]+"<br />";
					u[t[j]]=1;
				}
			}
		}
	}

	document.getElementById("uuuh").innerHTML="<pre>"+s+"</pre>";
}



