/* chp.js - game of chomp (the David Gale's game of chomp)
   
   Copyright 2003-2005 Jean-Paul Davalan jpdvl@wanadoo.fr
   
   site principal
   http://perso.wanadoo.fr/jean-paul.davalan/index.html
   

   http://perso.wanadoo.fr/jean-paul.davalan/jeux/nim/chomp/chp.js
   http://perso.wanadoo.fr/jean-paul.davalan/jeux/nim/chomp/index.html
   http://perso.wanadoo.fr/jean-paul.davalan/liens/liens_nim.html

   utilisation interdite sur tout autre site sans autorisation préalable

*/

var noyau = new Array();
noyau[0] = 1; // perdante
noyau[1] = 0; // gagnante


var suivt = new Array();
suivt[0] = 0;
suivt[2] = 1;

/*
Les colonnes sont numérotées de 0 à ncols-1
Les lignes sont numérotées de 1 à nligs = maxi-1

        0 1 2 3 4 ...  maxi-1

      1 X X X X X      X
      2 X X X X X      X

      6 X X X X X      X

Le jeu abcd...f  avec f non nul et a, b, ..., f <maxi
est codé par le naturel en base maxi : abcd...f(base maxi)
ce qui laisse plein de 'trous' (entiers ne correspondant 
pas à un jeu) et limite les dimensons admissibles
à maxi^ncols < 2^31=2147483648

7^11, 8^10, 9^9, 10^9, 11^8,14^8, 21^7, 35^6, 73^5
 */

var maxi=7;
var nligs=6;
var ncols=14;   

var Im = new Array();
Im[0] = new Image(); Im[0].src="bleu.png";
Im[1] = new Image(); Im[1].src="rouge.png";

var etat = new Array();
/*
for(var i=0;i < ncols;i++) {
etat[i] = nligs;
}
*/
function wtable(x,y) {
  var u=x,v=y;
  if(x<y) {
    u=y;v=x;
  }
  ncols=v;
  nligs=u;
  maxi=nligs+1;
  document.writeln("<center>");
  document.writeln('<table bgcolor="0700e0" border=0 cellspacing=0 cellpadding=0>');
  for(var j=0;j< ncols;j++) {
    document.writeln("<tr>");
    for(var i=1; i<= nligs; i++) {
    document.writeln('<td align="center"><a class=game href="javascript:joue('+i+','+j+')"><img name=k'+i+'_'+j+' border=0 src="rouge.png" width=25 height=25 alt=""></a></td>');
//    document.images["k"+i+"_"+j].src=Im[1].src;
    }
    document.writeln("</tr>");
  }
  document.writeln("</table>");
//  alert(ncols+" "+nligs);
  document.writeln("</center><br /><br />");
  document.writeln("<center>");
  document.writeln('<form name="frm" action="">');
  document.writeln("<table border=0 cellspacing=0 cellpadding=0>");
  document.writeln("<tr>");
  document.writeln('<td align="center">');
  document.writeln('<font size="-1"><b>Parties&nbsp;jou&eacute;es</b>&nbsp;:&nbsp;</font><input type="text" name="j" size="3" value="0"></td>');
  document.writeln('<td align="center">');
  document.writeln('<font size="-1"><b>&nbsp;&nbsp;Parties&nbsp;gagn&eacute;es</b>&nbsp;:&nbsp;</font><input type="text" name="g" size="3" value="0"></td>');
document.writeln("</tr>");
document.writeln("<tr>");
document.writeln("</tr>");
document.writeln("<tr><td  colspan=2>&nbsp;</td></tr>");
  document.writeln('<tr><td  colspan=2 class="liens"><input type="text" name="t" size="30"  value=""></td>');
  document.writeln("</tr></table>");
  document.writeln("</form>");
  document.writeln("</center><br />");
  nouveau();
}

function trace(n) {
  var i, j, u, r, k=n;
  for(i=0;i<ncols;i++) {
    u = k%maxi;
    n = (n-k)/maxi;
    for(j=1;j<=nligs;j++) {
      if(j<=u) {
         document.images["k"+j+"_"+i].src=Im[1].src;
      } else {
         document.images["k"+j+"_"+i].src=Im[0].src;
      }
    }
  }
}

function rafr() {
//alert("rafr");
  if(ncols==void(0)) alert("rafr");
  var i, j, u;
  for(i=1;i<=nligs;i++) {
    for(j=0;j<ncols;j++) {
      if(i<=etat[j])
         document.images["k"+i+"_"+j].src=Im[1].src;
      else
        document.images["k"+i+"_"+j].src=Im[0].src;
    }
  }
}

function nouveau() {
  var i, j, u;
  for(i=0;i<ncols;i++) {
    etat[i] = maxi-1;
  }
  for(j=0;j<ncols;j++) {
    for(i=1;i<=nligs;i++) {
      u=i*ncols+j;
      if(etat[j]>=i) 
        document.images["k"+i+"_"+j].src=Im[1].src;
      else 
        document.images["k"+i+"_"+j].src=Im[0].src;
    }
  }

}
function fini() {
  for(i=0;i<ncols;i++)
    if(etat[i]>0) return false
  return true;
}
function chrono() {
}
var nbparties=0, nbgagnees=0;

/* joue(l,c)  
      la ligne l   1<= l <= nligs
      la colonne c   0<= c < ncols
      etat[x] mis à l-1 aau maximum pour les colonnes c à ncols-1
 */
function joue(l,c) {
//var a = etat[c];
//alert("joue "+l+" "+c+" "+a);
  var u, t, j, n;
  if(l> etat[c]) {
    if(fini()) 
      nouveau();
    return;
  }
  if(l-1 < etat[c]) {
    for(j = c; j<ncols;j++) {
      if(etat[j]>l-1) etat[j]=l-1;
    }
  }
  rafr();
//  var id = setTimeout('chrono()', 3000);
  n=0;
  for(j=ncols-1;j>=0;j--) 
    n = n*maxi+etat[j]; 
//  a=ecrit(n);
//  alert(a);
  if(fini()) {
    document.frm.t.value="J'ai gagné";
    nbparties++;
    document.frm.j.value=nbparties;
    return;
  }
  var gn=gagne(n);
  if(gn == 0) {
    for(j=ncols-1;j>=0;j--) {
      if(etat[j]>0) {
        etat[j]=etat[j]-1;
	j=-1;
	break;
      }
    }

  } else {
    u=suivt[n];
//    alert(n+" "+u);
    for(j=0;j<ncols;j++) {
      etat[j] = u%maxi;
      u = (u-etat[j])/maxi;
    }
  }
    if(fini()) {
      document.frm.t.value="Vous avez gagné";
      nbparties++;
      nbgagnees++;
      document.frm.g.value=nbgagnees;
    }
  rafr();
  if(fini()==false) {
  var n2=0;
  for(j=ncols-1;j>=0;j--)
    n2 = n2*maxi + etat[j];
  gn=gagne(n2);
  s = "#"
  for(j=0;j<ncols;j++)
    s += " "+etat[j];
  if(gn==1) {
    s += " "+suivt[n2];
    document.frm.t.value =  s;
  } else {
    document.frm.t.value = s;
  }
  }
}

function gagne(pos) {
  
  if(noyau[pos] != void(0)) {
    return noyau[pos];
  }
  var i, j, r, nb, u, n, m;
  var t = new Array();
  for(i=0; i< ncols; i++) t[i] = 0;
  nb=pos;
  for(i=0;i<ncols && nb>0;i++) {
    u = nb % maxi;
    t[i] = u;
    nb= (nb-u)/maxi;
  }
    for(var k=0; k< ncols && t[k] >0; i++) k++;
    for(i=k-1;i>=0;i--) {
    for(j=0; j< t[i]; j++) {
      n=0;
      for(r=k-1; r>=0; r--) {
         n = n*maxi;
        if(r>=i && t[r]>j) n += j;
        else n += t[r];
      }
      if(gagne(n) == 0) {
        suivt[pos] = n;
        noyau[pos] = 1;
        return 1;
      }
    }
  }
  suivt[pos] = pos;
  noyau[pos] = 0;
  return 0;
}

function ecrit(nbre) {
 var n = parseInt(nbre), u, s="", s2;
  if(n==0) s="(0)";
  else {
    s = "(";
    while(n>0) {
      u = n%maxi;
      n = (n-u)/maxi;
      s2 = (n>0)? "," : ")";
      s += u+s2;
    }
  }
  return s;
}

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


