/* pile.js
  jeu de Pile ou Face (2 joueurs compétition)
  Jean-Paul Davalan © 2002-2005

*/
//var Imn=new Array("","","","","pile1.jpg","pile2.jpg","face1.jpg","face2.jpg","","","u00.png","u01.png","u10.png","u11.png","efface0.png","efface1.png","efface2.png");
var Imn=new Array("","","","","pile2Euro0.png","pile2Euro1.png","face2Euro0.png","face2Euro1.png","","","u00.png","u01.png","u10.png","u11.png","efface0.png","efface1.png","efface2.png");
var LZWConst=60
var Im=new Array();
for(i=0;i<17;i++) {
 if(Imn[i]!=null && Imn[i]!="") {
   Im[i]=new Image; Im[i].src=Imn[i]; 
 }
}
var hrang,mode=0,gtotal=0,gjoueur=0,gordi=0;
var nparties, ngagnees, nperdues,limite=50;
var chx=new Array(),chx2=new Array(),ohist=new Array(), jhist=new Array();
var sthist = ""

function init() {
  gtotal=0; gjoueur=0; gordi=0;rpart=0;
  document.frm.ftotal.value=gtotal;
  document.frm.fordi.value=gordi;
  document.frm.fjoueur.value=gjoueur;
  document.images["rface"].src=Im[10].src;
  document.images["rpile"].src=Im[10].src;  
}

function commence() {
  limite=25,nparties=0;ngagnees=0;nperdues=0;
  hrang=0;
  mode=0;
  chx[0]=1;chx[1]=1;
  document.frm.nlim.value=limite;
  document.frm.nper.value=0;
  document.frm.npar.value=0;
  document.frm.ngag.value=0;
  init();
}

function ordi() {
  var a, b, u, j, k;
  chgstrat();
  if(mode==0) {
    return Math.floor(2*Math.random());
  } else if(mode==1) {
    if(hrang<8) {
      b=Math.floor((chx[0]+chx[1])*Math.random());
      if(b<chx[0]) return 0;
      else return 1;
    } else {
      b, u=0, j;
      for(j=hrang-8;j<hrang;j++) u += jhist[j];
      b=Math.floor(8*Math.random());
      if(b<u) return 1;
      else return 0;
    }
  } else if (mode==2) {
    if(hrang<10) {
      b=Math.floor((chx[0]+chx[1])*Math.random());
      if(b<chx[0]) return 0;
      else return 1;
    } else {
      k=21;
      for(j=hrang-10;j<hrang;j++) {
        u += Math.floor(k/2)*jhist[j];
        k -=1;
      }
      b=Math.floor(30*Math.random());
      if(b<u) return 1;
      else return 0;
    }
  } else if (mode==3) {
    if(hrang<21) {
      b=Math.floor(2*Math.random());
      if(b<1) return 0;
      else return 1;
    } else {
      u=0;
      a=1;
      if(hrang>52) a=hrang-50;
      for(j=1;j<hrang;j++) {
       if(jhist[j-1]==jhist[j]) u +=1;
      }
      b= jhist[hrang-1];
      if(u>(hrang-a)/2) return b;
      else return 1-b;
    }
 }   else if (mode==4) {
      if (moy(6)< moy(15)) return 1;
      else return 0;
  } else if (mode==5) {
      if (moy(4)< moy(10)) return 1;
      else return 0;
  } else if (mode==6) {
      if (etude()) return 1;
      else return 0;
  }
}
/*
function etude() {
	var a=lzw("00"+sthist)+lzw("10"+sthist), b= lzw("01"+sthist) + lzw("11"+sthist)
	if(a==b) {
		return (Math.random()<0)
	} else 	return (a > b) 

}
*/
function etude() {
        var a=lzw("0"+sthist), b= lzw("1"+sthist)
        if(a==b) {
                return (Math.random()<0)
        } else  return (a < b)
}

function moy(n) {
   var u=0;
   var d = (n<hrang) ? n : hrang;
   for(var j=hrang-d;j<hrang;j++) {
        u += jhist[j];
   }
   return u/d;
}

function joue(x) {
  var u;
  u=ordi();
  chx[x] +=1;
  ohist[hrang]=u;
  jhist[hrang]=x;
  hrang +=1;
  if(u==0 && x==0) {
    document.images["rpile"].src=Im[13].src;
    document.images["rface"].src=Im[10].src;
  } else if(u==0 && x==1) {
    document.images["rpile"].src=Im[11].src;
    document.images["rface"].src=Im[12].src;
  }if(u==1 && x==0) {
    document.images["rpile"].src=Im[12].src;
    document.images["rface"].src=Im[11].src;
  }if(u==1 && x==1) {
    document.images["rpile"].src=Im[10].src;
    document.images["rface"].src=Im[13].src;
  }
	sthist = sthist.substr(0,LZWConst)
	sthist = x+''+sthist
  if(u==x) gordi += 1;
  else gjoueur += 1;
  gtotal += 1;
  document.frm.ftotal.value=gtotal;
  document.frm.fordi.value=gordi;
  document.frm.fjoueur.value=gjoueur;
  if(gordi >=limite || gjoueur>=limite) {
     if(gjoueur >gordi) {
      ngagnees +=1;
      document.frm.ngag.value = ngagnees;
    }else {
      nperdues +=1;
      document.frm.nper.value = nperdues
    }
    nparties +=1;
    document.frm.npar.value = nparties;
    gtotal=0;gordi=0;gjoueur=0;
  } 
}
function chglimite() {
  var a=1*document.frm.nlim.value;
  if(a>=2 && a<=1000) {
    limite=a;
  }
}
function chgstrat() {
  if(document.frm.stratg[0].checked==1) {
    mode=0;
  } else if(document.frm.stratg[1].checked==1) {
    mode=1;
  } else if(document.frm.stratg[2].checked==1) {
    mode=2;
  } else if(document.frm.stratg[3].checked==1) {
    mode=3;
  } else if(document.frm.stratg[4].checked==1) {
    mode=4;
  } else if(document.frm.stratg[5].checked==1) {
    mode=5;
  } else if(document.frm.stratg[6].checked==1) {
    mode=6;
  } else mode=7;
}

function lzw(s) {
  var i, c, caract, table=new Array(), rang_chaine=new Array(), ncodes=0, chaine;
  for(i=0;i<256;i++) { // on place les 256 caract. dans la table
    c =String.fromCharCode(i);
    rang_chaine[c]=ncodes; table[ncodes]=c; ncodes++;
  }
  chaine=s.charAt(0);     //STRING = get input character
  stu="";
  sth="";
  for(i=1;i<s.length;i++) {
    caract=s.charAt(i);
    if(rang_chaine[chaine+caract]==void(0)) { //
      stu += rang_chaine[chaine]+" ";         // output the code for STRING
      sth += hexa(rang_chaine[chaine])+" ";
      if(ncodes<4096) {
        rang_chaine[chaine+caract]=ncodes; //add STRING+CHARACTER to the string table
        table[ncodes]=chaine+caract;
        ncodes++;
      }
      chaine=caract; //STRING = CHARACTER
    } else {
      chaine=chaine+caract; // STRING = STRING+character
    }
  }
  // output the code for STRING
   stu += rang_chaine[chaine];
   sth += hexa(rang_chaine[chaine]);
  ncodes++; // on veut juste connaître la taille après compression

/*
  var coef;
  if(ncodes<512) coef=9/8;             // codé sur 9 bits
  else if(ncodes<1024) coef=10/8;      //         10
  else if(ncodes<2048) coef=11/8;      //         11
  else  coef=1.5;                        //         12
  return Math.ceil(coef*ncodes); // on suppose codes de 12 bits=1.5 octets
*/
	return ncodes
}

function hexa(x) {
  var k,
      u=parseInt(x);
  var a="0123456789ABCDEF";
  var h="";
  for(i=0;i<3;i++) {
      k = u%16
      h = a.charAt(k) + h;
      u = (u-k)/16;
  }
  return h;
}


