
function evolution() {
	this.cols=100
	this.rows=60
	this.regle=100
	this.tabregle=[]
	this.ampl=3
	this.centre=1
	this.dec=new Array(-1,0,1);
	this.tab=[]
	this.txt="";
	this.min=-1;
	this.max=1;
	this.str="";
	this.mode="centre"
	this.proba=0.5;
	this.motif="X";
	this.car0="-"
	this.car1="X"
	this.setregle = function(r) {
		this.regle=parseInt(r)
		var u = this.regle;
		for(var i=0; i<8; i++) {
			this.tabregle[i]=0
		}
		for(var i=0; i<8; i++) {
			this.tabregle[i]= u & 1
			u= u>>1
		}
		//alert(this.tabregle)
	}
	this.reinit = function(rows,cols,regle,dec, mode, prob,motif,circ) {
		//alert("this.reinit")
		this.cols=cols;
		this.rows=rows;
		this.mode=mode;
		this.min=dec[0];
		this.max=dec[0];
		for(var i=0; i<3; i++) {
			this.dec[i]=parseInt(dec[i]);
			if(this.dec[i]<this.min) this.min=this.dec[i]
			if(this.dec[i]>this.max) this.max=this.dec[i]
		}
		this.txt="";
		this.setregle(regle)
		//alert("this.setregle "+regle)
		this.str=""
		this.proba = parseFloat(prob);
		this.motif=motif;
		this.circ=circ;
		//alert(this.motif)
		if(mode=="proba") {
			this.str = this.premierealea()
		} else if(mode=="centre") {
			this.str = this.premiere();
		} else if(mode=="motif") {
			this.str = this.premieremotif();
		} else if(mode=="repetition") {
			this.str = this.premiererepetition()
		}
        }
	this.uneligne = function() {
		var i, a, t=[];
		if(!this.circ || this.mode== "repetition") {
			for(i=this.min;i<0;i++)
				t[i]=0;
			for(i=this.cols;i<this.cols+this.max;i++)
				t[i]=0;
			for(i=0; i<this.cols; i++) 
				t[i]=parseInt(this.tab[i]);
		} else {
			for(i=this.min-1;i<0;i++)
                        	t[i]=parseInt(this.tab[this.cols+i]);
			for(i=this.cols;i<=this.cols+this.max+1;i++)
                        	t[i]=parseInt(this.tab[i-this.cols]);
			 for(i=0; i<this.cols; i++)
	                        t[i]=parseInt(this.tab[i]);
		} 
/*
                for(i=this.cols;i<this.cols+this.max;i++)
                        t[i]=0;
                for(i=0; i<this.cols; i++)
                        t[i]=parseInt(this.tab[i]);
*/
		
	if(this.mode== "repetition") {
		for(i=-1;i>=this.min;i--)
                        t[i]=parseInt(this.tab[i+this.lrep])
		for(i=this.cols;i<this.cols+this.max;i++)
			t[i]=parseInt(this.tab[i-this.lrep])
	}
		for(i=0;i<this.cols; i++) {
				a=4*t[i+this.dec[0]]+ 2*t[i+this.dec[1]] + t[i+this.dec[2]];
				this.tab[i]=this.tabregle[a];
		}
		
		this.str += this.ligne2str();
        }
	this.premierealea = function() {
		var s="";
		for(var i=0; i<this.cols; i++) {
			this.tab[i] = (Math.random()<=this.proba)? 1 : 0;
		}
		return this.ligne2str();
	}
	this.premieremotif = function() {
		var l=this.motif.length, d = Math.floor((this.cols-l)/2)
		
                for(var i=0; i<d; i++) {
                        this.tab[i] = 0;
                }
		for(var i=0; i<l; i++) {
			var c = this.motif.charAt(i)
			this.tab[i+d] = (c=="X" || c=="x") ? 1 : 0
		}
		for(var i=d+l; i<this.cols; i++) {
			this.tab[i] = 0
		}
//alert(this.tab)
                return this.ligne2str();
        }
	this.premiererepetition= function() {
                var l=this.motif.length;
		this.lrep = l
		for(var i=-l; i<this.cols+l; i++) {
			var c = this.motif.charAt(i%l)
                        this.tab[i] = (c=="X" || c=="x") ? 1 : 0
                }

                return this.ligne2str();
        }


	this.ligne2str = function () {
		var s=""
                for(var i=0; i<this.cols; i++) s += ((this.tab[i]==0)?this.car0:this.car1);
		s +="<br />\n"
		//this.str += s;
		return s
	}
	this.premiere= function() {
                for(var i=0; i<this.cols; i++) {
                        this.tab[i] = 0;
                }
		this.tab[Math.floor(this.cols/2)] = 1;
		//alert(this.ligne2str())
		return this.ligne2str()
        }

	this.evolue = function() {
		//alert("this.evolue")
		for(var i=1; i<this.rows; i++) {
			this.uneligne();
		}
		//alert(this.str)
		return this.str;
        }

	this.affiche=function() {
		return this.str;
	}
}

var evol = null

function calc() {
//alert("calc")
	if(evol==null) {
		calcinit();
	} else {
		evol.evolue()
	}
}
function totb(s) {
	var u = s;
	u = u.replace(/^\s+/,"");
	u = u.replace(/\s+$/,"");
	u = u.replace(/\s+/g," ");
	return u.split(/\s+/g);
}
// cols,rows,regle,dec, mode, prob
function setcirc() {
	var a=(document.frm.circ.checked)
	document.getElementById("spCirc").innerHTML=(a)?"<code>[X]</code> circulaire":"<code>[ ]</code> circulaire"
}
function litcar() {
	var s = document.frm.cars.value
	s=s.replace(/\s+/g," ")
	s=s.replace(/^\s+/,"")
	s=s.replace(/\s+$/,"")
	var a=s.split(/\s+/g)
	if(a.length==1) {
		a[1]=a[0]
		a[0]="."
	} else if(a.length==0) {
		a[1]="X"
		a[0]="-"
		document.frm.cars.value=" - X"
	}
	return a
}

function calcinit() {
//alert("calcinit")
	var mode;
	if(evol != null)
		delete(evol)
	evol = new evolution() 
	var cars = litcar()
	evol.car0=cars[0]
	evol.car1=cars[1]
	var t0 = totb(document.frm.lc.value);
	var t1 = totb(document.frm.v.value);
	if(document.frm.rd[1].checked==true) mode="proba"
	else if(document.frm.rd[2].checked==true) {
		mode="motif"
		if(document.frm.chk.checked==true)
			mode = "repetition"
	} else mode = "centre"
	var circul = (document.frm.circ.checked==true)
	
	var proba=parseFloat(document.frm.pr.value)
	var motif=document.frm.motif.value
	motif = motif.replace(/^\s+/,""); motif = motif.replace(/\s+$/,"");
	motif = motif.replace(/[\s0]/g,"-");motif = motif.replace(/[a-z1-9]/ig,"X");
//alert(motif)
	var rg=parseInt(document.frm.regle.value)
	//alert(t0+" "+t1+" "+mode+" "+proba+" "+rg)
	evol.reinit(parseInt(t0[0]), parseInt(t0[1]), rg,t1, mode,proba,motif,circul )
	var s=evol.evolue()
	//alert(s)
	document.getElementById("ac").innerHTML="<center><code>"+s+"</code></center>"
}
function add(x) {
        if(testid) {
                clearInterval(id);
		testid=false
        }
	continu();
	var r = parseInt(document.frm.regle.value)+x
	if(r<0) r=0
	else if(r>255) r=255
	document.frm.regle.value=r
	calcinit();
}
function efface(x) {
/*
        if(testid) {
                clearInterval(id);
		testid=false
        }
*/
	document.frm.lc.value="100 150"
	//document.frm.regle.value="17"
	document.frm.pr.value=0.5
	document.frm.v.value="-1 0 1"
	if(x==0) {
	document.frm.rd[1].checked=true;
	document.frm.chk.checked=false;
	} else if(x==1) {
	       document.frm.rd[0].checked=true;
	} else if(x==2) {
		document.frm.rd[2].checked=true;
	        document.frm.chk.checked=false;
		document.frm.motif.value="1101000110"
        }else if(x==3) {
                document.frm.rd[2].checked=true;
                document.frm.chk.checked=true;
                document.frm.motif.value="XX-X----X-"
        }else if(x==4) {
                document.frm.rd[2].checked=true;
                document.frm.chk.checked=true;
                document.frm.motif.value="100000000"
        }else if(x==5) {
                document.frm.rd[2].checked=true;
                document.frm.chk.checked=false;
                document.frm.motif.value="XXXXXXXXXXXXXXX"
        }else if(x==6) {
                document.frm.rd[2].checked=true;
                document.frm.chk.checked=true;
                document.frm.motif.value="XXXX-"
		document.frm.v.value="-1 1 2"
        }
	calcinit();
}
function rotate() {
	var r = parseInt(document.frm.regle.value)+1
        if(r>255) r=0
        document.frm.regle.value=r
        calcinit();
}

var id=null;
var testid=false;
function continu() {
	document.frm.cont.value=(testid)?"Arrête":"Continu"
	document.getElementById("spCont").innerHTML=((testid)?"Arrête":"Continu")
	document.getElementById("spCont0").innerHTML=((testid)?"Arrête":"Continu")
}
function auto() {
	if(testid) {
		clearInterval(id);
		testid = false
	} else {
		testid=true;
		id=setInterval("rotate()",3000);
	}
	continu()
}

function setmotif() {
	document.frm.rd[2].checked=true;
	a = (document.frm.chk.checked==true)
	document.getElementById("spChk").innerHTML=(a)?"<code>[X]</code> répété":"<code>[ ]</code> répété"
}

function voisins() {

	var b = Math.floor(7*Math.random())-3;
	var a = - Math.floor(4*Math.random())+b-1;
	var c = Math.floor(4*Math.random())+b+1;
	document.frm.v.value=a+" "+b+" "+c
}

function motifalea() {
	var ampl=Math.floor(40*Math.random())+2;
	var s=""
	for(var i=0; i<ampl; i++) {
		s += (Math.random()<0.5)?"X":"-"
	}
	document.frm.motif.value=s
	setmotif();
	calcinit();
}

