/* cav1.js

   Jean-Paul Davalan © 2003  (22/07/2003)
   jpdvl@wanadoo.fr

   échanger les cavaliers

   jeu décrit dans l'article de Jean-Paul Delahaye
   du numéro 310 Août 2003 de la revue Pour la Science
   (édition française de Scientific American)
   ---------------------------------------------------------
   Positions possibles (10)

    j=   0  1  2  3
      |--------------
i=0   |  1
  1   |  2  3  4  5
  2   |  6  7  8
  3   |  9 10

   Début : rouges en 1 et 6
           noirs  en 5 et 7

   Solution (40 coups) :

   6 ->  4 10  2 
   1 ->  4 10
   7 ->  1  4  6
  10 ->  4  1  7
   2 -> 10  4  1
   6 ->  4 10  2  8
   1 ->  4 10  2
   7 ->  1  4 10
   5 ->  7  1  4  6
  10 ->  4  1  7  5
   2 -> 10  4  1  7
   8 ->  2 10  4  1

*/
     
var Im=new Array();
Im[0]=new Image();Im[0].src="a0j.png";
Im[1]=new Image();Im[1].src="a1j.png";
Im[2]=new Image();Im[2].src="a2j.png";
Im[3]=new Image();Im[3].src="a0b.png";
Im[4]=new Image();Im[4].src="a1b.png";
Im[5]=new Image();Im[5].src="a2b.png";
Im[6]=new Image();Im[6].src="ablc.png";

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

var etat=0;
var posx, posy, coups;

function trace() {
  document.writeln('<center><table border=0 cellpadding=0 cellspacing=0>');
  for(var i=0;i<4;i++) {
    document.writeln('<tr>');
    for(var j=0;j<4;j++) {
       document.write('<td><a href="javascript:eff('+i+','+j+')"><img name="ima'+i+'_'+j+'" border=0 width=51 height=51 alt=""></a></td>');
    }
    document.writeln('</tr>');
  }
  document.writeln('</table></center>');
  init();
}

function init() {
etat = 0;
coups=0;
table[0][0]=1;table[0][1]=3;table[0][2]=3;table[0][3]=3;
table[1][0]=0;table[1][1]=0;table[1][2]=0;table[1][3]=2;
table[2][0]=1;table[2][1]=2;table[2][2]=0;table[2][3]=3;
table[3][0]=0;table[3][1]=0;table[3][2]=3;table[3][3]=3;
  for(var i=0;i<4;i++) {
    for(var j=0;j<4;j++) {
      var u = 3*((i+j+1) % 2);
      if(table[i][j]==3) {
        document.images["ima"+i+"_"+j].src = Im[6].src;
      } else {
         document.images["ima"+i+"_"+j].src = Im[table[i][j]+u].src;
      }
    }
  }
  document.images["ima0_0"].src = Im[4].src;
  document.images["ima1_3"].src = Im[5].src;
  document.images["ima2_0"].src = Im[4].src;
  document.images["ima2_1"].src = Im[2].src;
  document.frm.messg.value = "Jeu en cours";
}

function eff(x,y) {
  if(table[x][y]==1 || table[x][y]==2) {
      couleur = table[x][y];
      posx = x;
      posy = y;
      etat = 1;
  } else {
    if(etat==1 && table[x][y]==0) {
      var u = Math.abs(posx-x); 
      var v=Math.abs(posy-y);
      if((u==2&&v==1)||(u==1&&v==2)) {
         var k=table[posx][posy];
         table[posx][posy]=0;
         document.images["ima"+posx+"_"+posy].src = Im[3*((posx+posy+1)%2)].src;
         table[x][y]=k;
         document.images["ima"+x+"_"+y].src = Im[3*((x+y+1)%2)+k].src;
         etat=0;
         coups = coups+1;
         document.frm.messg.value= "nb de coups : "+coups;
         ok();
      }
    }
    etat = 0;
  }
}

function ok() {
  if(table[0][0]==2 &&table[1][3]==1 &&table[2][0]==2 &&table[2][1]==1) {
   document.frm.messg.value="Bravo, réussi en "+coups+" coups";
   return true;
  }
  return false();
}

