/* taquin.js   Jeu de Taquin
 
   (C) 2002-2005 Jean-Paul Davalan jpdvl@wanadoo.fr

   site   http://perso.wanadoo.fr/jean-paul.davalan/index.html
   page   http://perso.wanadoo.fr/jean-paul.davalan/jeux/solitaires/taquin
*/
var WIDTH=560/4, HEIGHT=492/4;

var lg="fr";
var pos0 = 0, tab = new Array(), histab=new Array(), khist=0;

for(i=0;i<16;i++) { tab[i] = i; }

function yval(x) { return x%4; } 
function xval(x) { return Math.floor(x/4); }
function indice(i, j) { return 4*i+j; }
function xzero() { return xval(pos0); } 
function yzero() { return yval(pos0); }

var duree, id, stopchrono=0;

function chrono() {
  var s="";
  nbsecondes = duree%60;
  nbminutes = Math.floor(duree/60)%60;
  nbheures = Math.floor(duree/3600);
  if(nbheures>0) s = s+nbheures+" h ";
  else s = s +"   ";
  if(nbheures>0 || nbminutes>0){
    if(nbminutes<10) s = s+" ";
    s = s+nbminutes+" min ";
  }
  if(nbsecondes<10) s = s+" "
  s = s +nbsecondes+" s";
  while(s.length<20) s=" "+s;
  document.frm2.but.value=s;
  duree++;
  if(!stopchrono) {
     if(id != null) clearTimeout(id);
     id = setTimeout('chrono()', 1000);
  }
}
function melange() {
  var p=0, k, km, i, a, b, u;
  range();
  for(i=0;i<16;i++)
    tab[i] = i;
  km = 10+Math.floor(30*Math.random());
  for(k=0;k<km;k++) {
    u=Math.floor(3*Math.random());
    a = xzero();
    b = yzero();
    if(k%2==0) {
      if(u>=a) u = u+1;
      effect(u, b);
    } else {
      if(u>=b) u = u+1;
      effect(a, u);
    }
  }
  duree=0; stopchrono = 0; chrono();
}
function teste() {
  var t=1, i;
  alerte("");
  for(i=0;t==1 && i<16; i++) 
    t = (tab[i] == i);
  if(t==1) {
    stopchrono = 1;
    alerte("Bravo, vous avez remis toutes les pièces en place !");
    if(id!=null) ClearTimeout(id);
  }
  return t;
}
function retourne() {
  if(khist>0) {
    var r = histab[khist-1];
    effect(xval(r),yval(r));
    khist -=2;
  }
}
function effect(x, y) {
  var test=1;
  if(indice(x,y)==pos0) return;
  var a = xzero(), b = yzero(), i;
  histab[khist]=indice(a,b); khist++;
  if(a==x && b!=y) {
    u = (b<y) ? 1 : -1;
    for(i=b; i!=y; i += u) {
      tab[4*a+i] = tab[4*a+i+u];
      document.images["tq"+a+"_"+i].src=Im[tab[4*a+i+u]].src;
    }
    document.images["tq"+a+"_"+y].src=Im[0].src;
    tab[4*a+y] = 0; pos0 = 4*a+y;
    teste();
  } else if (a != x && b==y) {
     u = (a<x) ? 1 : -1;
     for(i=a; i!=x; i += u) {
       tab[4*i+b] = tab[4*(i+u)+b];
       document.images["tq"+i+"_"+b].src=Im[tab[4*(i+u)+b]].src;
     }
     document.images["tq"+x+"_"+b].src=Im[0].src;
     tab[4*x+b] = 0; pos0=4*x+y; teste();
  } else return;
}
var Im=new Array();
for(var i=0; i<=15; i++) {
  Im[i] = new Image; Im[i].src="imgtaquin/tq"+i+".jpg";
}
function imginit() {
  alerte("");
  for(var i=0;i<4;i++) 
    for(var j=0;j<4;j++) 
      document.images["tq"+i+"_"+j].src=Im[4*i+j].src;
  stopchrono=1; 
  clearTimeout(id);
}
function range() {
  for(var i=0;i<16;i++) { tab[i] = i; }
  imginit(); pos0=0; khist=0;
}
function mktable() {
  for(var i=3;i>=0;i--) {
    document.write("<tr>\n");
    for(var j=0;j<4;j++) {
      document.write('<td><a href="javascript:effect('+i+','+j+')">');
      document.write('<img name="tq'+i+'_'+j+'" border=0 width="'+WIDTH+'" height="'+HEIGHT+'" alt=""></a></td>');
    }
    document.write("\n</tr>\n");
  }
}
function alerte(s) {
document.getElementById("texte").innerHTML=s;
}

