/* bulg.js : bulgarian solitaire & carolina solitaire
   
   (C) 2005 Jean-Paul Davalan <jpdvl@wanadoo.fr>
 
    site  http://perso.wanadoo.fr/jean-paul.davalan/index.html 
    page location : 
    http://perso.wanadoo.fr/jean-paul.davalan/jeux/solitaires/bulg/index.html

*/

function bulgare(f) {
    var tas = new Array(),ordre,
    current=0, period=0, last=-1, texte=f.t, area = f.ar, radio = f.rd;
    this.f = f;

    this.tas = tas;
    this.ordre=ordre;
    this.current=current;
    this.period=period;
    this.last=last;
    this.texte=texte;
    this.area=area;
    this.radio = radio;
    this.alea = false;
    this.proba=1;

    function sort(tb) {
      var i, j;
      for(i=0;i<tb.length;i++) {
        for(j=i+1;j<tb.length;j++) {
          if(parseInt(tb[i])>parseInt(tb[j])) {
            u=tb[i]; tb[i]=tb[j];tb[j]=u;
          }
        }
      }
      return tb;
    }
    this.sort=sort;


    function tostr(a) {
      var s="", i;
      for(i=0;i<a.length;i++) {
        if(parseInt(a[i]) != 0) {
           s += a[i];
           if(i<a.length-1)
             s +=" ";
        }
      }
      return s;
    }
    this.tostr=tostr;


    function init() {
     // alert("init");
      var s, s=this.texte.value;
      if(this.radio[0].checked==true)
        this.ordre=true;
      else
        this.ordre=false;

      if(f.alea.cheched==true) {
		this.alea==true;
		this.proba = parseFloat(this.f.proba.value);
      }

      do { 
        t = s;
        s = s.replace(/[\(\)\*,;:+\s]+/g," ");
        s = s.replace(/^\s+/,"");
        s = s.replace(/\s+$/,"");
      } while(s!=t);
      
      var a = new Array();
      a = s.split(/\s/);
      
      if(this.alea==false && this.ordre==false) this.sort(a);
      

      this.tas[0] = this.tostr(a);
      this.current=1;
      
    }
    this.init=init;


    function suivant() {
      var i;
      var s = this.tas[this.current-1].replace(/\s+$/,"");
      s = s.replace(/^\s+/,"");
      var a = new Array();
      a = s.split(/\s/g);
      
      var l = a.length;
      
      if(this.alea==true) {
	a[l]=0;
	for(i=0;i<l;i++) {
		if(Math.random()<this.proba) {
		   a[i] = parseInt(a[i])-1;
                   a[l]++;
		}
	}
      } else {
      for(i=0;i<l;i++) a[i] = parseInt(a[i])-1;
      a[l] = l;
      var b=new Array() ;
      var j=0;
      for(i=0;i<l+1;i++) {
        u=parseInt(a[i]);
        if(u>0) {
          b[j]=u;
          j++;
        }
      }
      
      if(this.ordre==false) this.sort(b);
      }
      
      this.tas[this.current] = this.tostr(b);
      
      for(i=0;i<this.current;i++) {
        if(this.tas[i]==this.tas[this.current]) {
           this.period=this.current-i;
           this.last=i;
        }
      }
      this.current++;
      return this.period;
    }
    this.suivant=suivant;


    function calc() {
      var i, s="";

      this.init();
    
      while(this.suivant()==0) ;
      for(i=0;i<this.current;i++) {
         s += this.tas[i];
         if(i==this.last) 
         s += " (*)";
         s += "\n";
      }
      s += "période = "+this.period+"\n";
      s += "précédée de "+ this.last+" mouvements\n";
      this.area.value=s;
    }
    this.calc=calc;

}

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

function cherche() {
 
   var t = new bulgare(document.frm);
   t.calc();
}

var exo = new Array("1 3 5 7", "1 2 3 4 5 6 7", "7", "1 3 5 7");

function exemple(n) {
   document.frm.t.value=exo[n];
   document.frm.ar.value="";
}

function hasard() {
  var n = 3+Math.floor(7*Math.random());
  var s = "";
  for(i=0;i<n;i++) {
    t= 1+Math.floor(12*Math.random());
    s += t+" ";
  }
  document.frm.ar.value="";
  document.frm.t.value=s;
  cherche();
}


function comb() {
  var k=4+Math.floor(5*Math.random());
  var s="", u, v;
  var r=(k*(k-1))/2;
  var a = new Array();

  for(i=0;i<k;i++) 
    a[i]=1;


  while(r > 0) {
    u = Math.floor(k*Math.random());
    v = 1 + Math.floor(r/2*Math.random())
    if(v<=r) {
      a[u] += v;
      r -= v;
    }
  }


  for(i=0;i<k;i++)
    s += a[i]+" ";


  document.frm.ar.value="";
  document.frm.t.value=s;
  cherche();

}

function untas() {
  var n=6+Math.floor(100*Math.random());
  document.frm.ar.value="";
  document.frm.t.value=n;
  cherche();
}

function triang() {
  var k=5+Math.floor(20*Math.random());
  var n=(k*(k+1))/2;
  document.frm.ar.value="";
  document.frm.t.value=n;
  cherche();
}



