/* 
        http://jeux-et-mathematiques.davalan.org/jeux/solitaires/dix/index.html
	(ou sur les pages équivalentes des sites miroirs)
*/
function jeu() {
	this.ncases=10
	this.cases=new Array()
	this.ncoups=0
	this.etat = "attente"
	this.hist=""
	this.demo = "X  <br />"
	

	this.initpartie = function() {
		document.getElementById("resultat").innerHTML=""
		this.ncoups=0
		this.hist=""
		this.demo="X <br />"
		for(i=0; i< this.ncases; i++) this.cases[i] = 0;
		this.cases[1] = 1
		this.affiche()
	}
	this.init = function() {
		
		this.initpartie()
	}
	this.set = function(k, sens) {
		if(k<0|| k>=this.ncases) return
		if(sens==1) {
			this.cases[k]++
		} else if(sens==-1) {
                        this.cases[k]--
			if(this.cases[k]<0)
				this.cases[k]=0
                } 
		this.demo = this.poly()
		this.ncoups=0
		this.hist=""
		this.affiche()
	}
	this.clic = function(k, sens) {
		var p = this.teste(k, sens)
		if(p==-1) {
			;
		} else if(p==1) {
			this.cases[k-1] += 1
			this.cases[k] -= 1
			this.cases[k+1] += 1
			this.hist += "+" + k+", "
		} else if(p==2) {
			this.cases[k-1] -= 1
			this.cases[k] += 1
			this.cases[k+1] -= 1
			this.hist += "-" + k+", "
		}
		this.ncoups++
		this.demo += this.poly()
		this.affiche()
	}
	this.teste= function(k, sens) {
		if(sens==1) {
			if(this.cases[k]==0|| k<=0 || k>=9) return -1
			else return 1
		} else if(sens==-1) {
			if(k==0 || k>=9 || this.cases[k-1]==0 || this.cases[k+1]==0) return -1
			else return 2
		}
	}
	this.poly = function() {
		function mono(a, e) {
			if(a==0) return ""
			var inconn = (e==0) ?"1":((e==1)? "X" : "X<sup>"+e+"</sup>")
			var s=""
			for(var i=0; i<a; i++) s += " + "+inconn
			
			return s
		}
		var pol=""
		for(var i=0; i<=this.cases.length;i++)
			pol += mono(this.cases[i], i)
		pol = pol.replace(/^\s\+/,"")
		pol = " &cong; "+ pol+"<br />"
		return pol
	}
		
	this.affiche = function() {
		for(var i=0; i<this.ncases; i++) {
			if(this.cases[i]<=10)
			document.images["im"+i].src = imge[this.cases[i]].src
			else
			document.images["im"+i].src = imge[10].src
		}			
		document.getElementById("resultat").innerHTML=""+this.ncoups+" coups <br /><br />sequence "+this.hist+
		"<br /><br /><ins>d&eacute;monstration :</ins><br /><br />"+this.demo
	}

}

// +1, +2, +3, +1, +1, +2, +3, -1, +4, -2, +5, -3, -2, +6, +5, -3, +6, -4, +7, -5, -6, -7, 

var imge = new Array();
var etat="jeu"

for(var i=0; i<=10; i++) {
	imge[i] = new Image()
	imge[i].src = "/data/images/a"+i+".jpg"
}
//alert(imge[1].src)
var x = new jeu()


function clk(k, s) {
	if(etat=="jeu") {
	x.clic(k, s)
	} else {
		x.set(k,s)
	}
}

function chgetat() {
	if(etat=="jeu")
		etat="x"
	else
		etat="jeu"
}

function partie() {
	etat="jeu"
	x.initpartie()
}
		
function reinit() {
	etat="jeu"
	x.init()
}


