/* nim game
   nim.js
   Jean-Paul Davalan © 2002 2003 jpdvl@wanadoo.fr
   maj : 23 avril 2003
*/
var Im=new Array(),ncols=5,npions=9;
Im[0]=new Image();Im[0].src="pbleu0.png";
Im[1]=new Image();Im[1].src="pbleu.png";
Im[2]=new Image();Im[2].src="pbleu2.png";
/* tb[i] nombre de pions dans le tas i
   ts = numero du tas choisi
   tv = nouvelle valeur
 */
var tb = new Array(), ordijoue=0;
var ntas, nparties=0, ngagne=0;
var ts, tv;

var primes = new Array(2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149);
var gr = new Array(0,1,1,2,0,3,  3,2,0,4,1,1,0,2,3,3,0,2,4,4,0,2,5,5,0,2,1,4,0,2,3,5,0,2,6,1,0,2,6,3,0,2,6,4,0,2,6,5,0,2,1,4,0,2,3,5,0,2,6,1,0,2,6,3,0,1,6,2,0,3,6,2,0,4,6,2,0,5,6,2,0,4,6,2,0,5,1,2,0,7,3,2,0,4,6,1,0,2,6,3,0,1,6,2,0,3,6,2,0,4,6,2,0,4,6,2,0,4,6,2,0,5,1,2,0,7,3,2,0,4,6,1,0,2,6,3,0,2,6,4,0,2,5,1,0,2,6,3,0,0);
function isprime(x) {
  for(var i=0;primes[i]<=x;i++) {
    if(primes[i]==x) return true;
  }
  return false;
}
function primemax(x) {
   for(var i=0;primes[i]<=x;i++) ;
   return primes[i-1];
}
   
function wtable() {
document.writeln("<table border=0 cellspacing=0 cellpadding=0>");

for(var i=0;i<npions;i++) {
document.writeln("<tr>");
for(var j=0; j<ncols; j++) {
document.writeln("<td align=\"center\"><a href=\"javascript:joue()\" onmouseover=\"pselect("+j+","+(npions-1-i)+")\"><img name=\"k"+j+"_"+(npions-1-i)+"\" src=\"pbleu0.png\" border=0 width=\"26\" height=\"10\" alt=\"\"></a></td>");
}
document.writeln("</tr>");
}
document.writeln("<tr><td colspan="+ncols+">&nbsp;</td></tr><tr>");
document.writeln("<td colspan=3 align=\"center\">");
document.writeln("<font size=\"-1\"><b>Parties&nbsp;jouées</b>&nbsp;:&nbsp;</font><input class=\"tt0\" type=\"text\" name=\"npart\" size=\"3\" value=\"0\"></td>");
document.writeln("<td colspan=\""+(ncols-3)+"\" align=\"center\">");
document.writeln("<font size=\"-1\"><b>&nbsp;&nbsp;Parties&nbsp;gagnées</b>&nbsp;:&nbsp;</font><input class=\"tt0\" type=\"text\" name=\"npartg\" size=\"3\" value=\"0\"></td>");
document.writeln("</tr></table>");
}

function commence() {
var i, j, k, s="";
document.frm.infos.value="";
  ntas=0;
  while(ntas==0) {
    for(i=0;i<ncols;i++) {
      tb[i] =(Math.random() < 0.7) ? 1 : 0;
      if (tb[i]==1) {
        tb[i]=Math.floor(Math.random()*(npions-1)+1);
      }
      s += " "+tb[i];
    }
    nbtas();
  }
  ts=-1;tv=-1;
  place();
}

function place() {
  var i, j;
  for(i=0;i<ncols;i++) {
    for(j=0;j<npions;j++) {
      if(j >= tb[i]) document.images["k"+i+"_"+j].src=Im[0].src;
      else {
        if(i==ts && j >= tv) document.images["k"+i+"_"+j].src=Im[2].src;
        else document.images["k"+i+"_"+j].src=Im[1].src;
      }
    }
  }
}

/* colonne x >= 0, piece y >= 0 
   col x :  | | | | | | | | | | | | | | | |  tb[x] = 16
            0         5   y
   tb[x]-y = 16 -7 = 9 il y a 9 pieces en partant de y
   le plus grand premier <= 9 est 7
   16 - 7 = 9
*/
function pselect(x,y) {
  var u;
  if(tb[x]>y+1 || (tb[x]==1 && y==0)) { 
    if(tb[x]>1 ) {
      u=tb[x]- primemax(tb[x]-y);
      ts=x; tv=u;
    } else if (tb[x] == 1 && y==0) {
      ts=x; tv=y;
    } else {
      ts=-1; tv=-1;
    }
  } else { 
    ts=-1; tv=-1;
  }
  place();
}
function nbtas() {
  var i ;
  ntas=0;
  for(i=0;i<ncols;i++) {
    if(tb[i]>0) ntas++;
  }
  return ntas;
}

function joue() {
  var i, j, tjeu, test=1, ok=false;
//  if(tb[ts]>0 && tv<tb[ts]) {
  if(tb[ts]>0) {
    tb[ts] = tv;
    ts=-1;tv=-1;
    ok=true;
  }
  if(ok==true) {
    tjeu = valeurjeu();
    if(ntas == 0) {
      document.frm.infos.value="Bravo, vous avez gagné.";
//      alert("Bravo, vous avez gagné");
      nparties++;
      ngagne++;
      document.frm.npart.value=nparties;
      document.frm.npartg.value=ngagne;
    } else {
      if(tjeu == 0) {
//        alert("Position Gagnante !");
        for(i=0;i<ncols;i++) {
	  if(tb[i]>0) {
	    tb[i] -= 1+ Math.floor(tb[i]*Math.random());
	    place();
	    i=ncols;
	  }
	}
      } else {
        for(i=0;i< ncols;i++) {
	  if(tb[i]>1) {
	    for(j=0; j<primes.length && primes[j] <= tb[i];j++) {
	      var a = sdig(sdig(gr[tb[i]],tjeu), gr[tb[i]-primes[j]]);
  	      if(a == 0) {
	        tb[i] = tb[i]-primes[j];
	        i=ncols;
	      }
	    }
	  } else  if(tb[i]==1) {
	    if(tjeu == 1) {
	      tb[i]=0;
	      i=ncols;
	    }
	  }
	}
     }
     place();
     nbtas();
     if(ntas == 0) {
       document.frm.infos.value="Désolé, vous avez perdu cette partie.";
//       alert("Désolé, vous avez perdu");
       nparties++;
       document.frm.npart.value=nparties;
       document.frm.npartg.value=ngagne;
     }
    }
  }
}
/* somme digitale
    13 = 1 1 0 1  en base 2
     6 =   1 1 0   "
   --------------
    11 = 1 0 1 1  somme digitale
*/

function sdig(a,b) {
  return a ^ b;
}

function grundy(x) {
  return gr[x];
}

function valeurjeu() {
  var i, v, s = 0;
  ntas=0;
  for(i=0; i<ncols; i++) {
    v = gr[tb[i]];
    if(v != 0 ) {
      ntas++;
      if(s != 0) s = sdig(s,v); // sdig( grundy(s), grundy(v));
      else s = v; // grundy(v);
    }
  }
  return s;
}

